适用人群:完成项目开发、想部署上线的开发者
学习时长:约1-2天
部署环境:宝塔面板 + Linux服务器
重要程度:★★★★☆(必须掌握)
一、部署前准备
1.1 服务器选择
| 云服务商 | 入门服务器 | 价格参考 |
|---|
| 阿里云 | ECS 2核2G | ~50元/月 |
| 腾讯云 | CVM 2核2G | ~50元/月 |
| 华为云 | ECS 2核2G | ~50元/月 |
| Sealos | 云容器 | 按量计费 |
推荐配置:2核4G内存,50G SSD硬盘,CentOS 7+或Ubuntu 20+
1.2 域名与备案
1. 购买域名(阿里云/腾讯云)
2. 域名备案(国内服务器必须,约1-2周)
3. 域名解析到服务器IP
4. 申请SSL证书(免费:Let's Encrypt)
1.3 宝塔面板安装
# CentOS
yum install -y wget && wget -O install.sh https://download.bt.cn/install/install_6.0.sh && sh install.sh
# Ubuntu
wget -O install.sh https://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh
# 安装后会显示:
# 外网面板地址: http://your-ip:8888/xxxx
# 内网面板地址: http://localhost:8888/xxxx
# username: xxxxx
# password: xxxxx
二、LNMP 环境搭建
2.1 宝塔一键安装
登录宝塔面板 → 软件商店 → 一键安装:
✅ Nginx 1.24
✅ MySQL 5.7 或 8.0
✅ PHP 8.2
✅ Redis 7.0
✅ phpMyAdmin 5.2
2.2 环境验证
# 检查Nginx
nginx -v
systemctl status nginx
# 检查PHP
php -v
systemctl status php-fpm
# 检查MySQL
mysql -V
systemctl status mysqld
# 检查Redis
redis-cli ping # 应返回PONG
三、PHP 项目部署
3.1 创建站点
宝塔面板 → 网站 → 添加站点:
- 域名:your-domain.com
- 根目录:/www/wwwroot/your-domain.com
- PHP版本:PHP-8.2
- 数据库:MySQL,设置密码
3.2 上传代码
# 方式1:宝塔文件管理器上传
# 方式2:Git克隆
cd /www/wwwroot/your-domain.com
git clone https://github.com/your/repo.git .
# 安装依赖
composer install --no-dev
# 设置权限
chown -R www:www /www/wwwroot/your-domain.com
chmod -R 755 /www/wwwroot/your-domain.com
chmod -R 777 /www/wwwroot/your-domain.com/storage
chmod -R 777 /www/wwwroot/your-domain.com/bootstrap/cache
3.3 Nginx 配置
# Laravel项目Nginx配置
server {
listen 80;
server_name your-domain.com;
root /www/wwwroot/your-domain.com/public;
index index.php index.html;
# URL重写(Laravel必须)
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP处理
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
# 禁止访问隐藏文件
location ~ /\. {
deny all;
}
}
3.4 Laravel 部署
# 进入项目目录
cd /www/wwwroot/your-domain.com
# 安装依赖
composer install --optimize-autoloader --no-dev
# 环境配置
cp .env.example .env
php artisan key:generate
# 编辑.env
vim .env
# APP_ENV=production
# APP_DEBUG=false
# APP_URL=https://your-domain.com
# DB_DATABASE=your_db
# DB_USERNAME=your_user
# DB_PASSWORD=your_pass
# 数据库迁移
php artisan migrate --force
# 缓存优化
php artisan config:cache
php artisan route:cache
php artisan view:cache
# 队列处理(如果有)
# 宝塔 → 计划任务 → 添加:
# 任务类型:Shell脚本
# 执行周期:每1分钟
# 脚本内容:cd /www/wwwroot/your-domain.com && php artisan queue:work --stop-when-empty
四、Python 项目部署
4.1 Flask 部署
# 安装Python环境
apt install python3 python3-pip python3-venv
# 创建虚拟环境
cd /www/wwwroot/your-domain.com
python3 -m venv venv
source venv/bin/activate
# 安装依赖
pip install -r requirements.txt
pip install gunicorn
# 测试运行
gunicorn -w 4 -b 0.0.0.0:5000 app:app
4.2 Gunicorn 配置
# gunicorn.conf.py
import multiprocessing
bind = "0.0.0.0:5000"
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = "sync"
timeout = 30
keepalive = 2
errorlog = "/var/log/gunicorn/error.log"
accesslog = "/var/log/gunicorn/access.log"
4.3 Supervisor 进程管理
# /etc/supervisor/conf.d/myapp.conf
[program:myapp]
directory=/www/wwwroot/your-domain.com
command=/www/wwwroot/your-domain.com/venv/bin/gunicorn -c gunicorn.conf.py app:app
user=www
autostart=true
autorestart=true
stdout_logfile=/var/log/myapp/stdout.log
stderr_logfile=/var/log/myapp/stderr.log
# Supervisor命令
supervisorctl reread
supervisorctl update
supervisorctl start myapp
supervisorctl status myapp
4.4 Nginx 反向代理
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /static/ {
alias /www/wwwroot/your-domain.com/static/;
expires 30d;
}
}
五、Node.js 项目部署
5.1 PM2 进程管理
# 安装PM2
npm install -g pm2
# 启动应用
cd /www/wwwroot/your-domain.com
pm2 start ecosystem.config.js
# ecosystem.config.js
module.exports = {
apps: [{
name: 'myapp',
script: 'server.js',
instances: 'max',
exec_mode: 'cluster',
env: {
NODE_ENV: 'production',
PORT: 3000
}
}]
};
# PM2命令
pm2 list # 查看进程
pm2 logs myapp # 查看日志
pm2 restart myapp # 重启
pm2 stop myapp # 停止
pm2 delete myapp # 删除
pm2 save # 保存
pm2 startup # 开机自启
5.2 Nginx 配置
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
六、Go 项目部署
6.1 编译与运行
# 编译
cd /www/wwwroot/your-domain.com
CGO_ENABLED=0 GOOS=linux go build -o server .
# 直接运行
./server
# 或使用systemd管理
6.2 Systemd 服务
# /etc/systemd/system/myapp.service
[Unit]
Description=My Go Application
After=network.target
[Service]
Type=simple
User=www
WorkingDirectory=/www/wwwroot/your-domain.com
ExecStart=/www/wwwroot/your-domain.com/server
Restart=always
RestartSec=5
Environment=GIN_MODE=release
[Install]
WantedBy=multi-user.target
# 启用服务
systemctl daemon-reload
systemctl enable myapp
systemctl start myapp
systemctl status myapp
七、Java 项目部署
7.1 Spring Boot 部署
# 打包
cd /www/wwwroot/your-domain.com
mvn clean package -DskipTests
# 运行
java -jar target/myapp-0.0.1-SNAPSHOT.jar
# 后台运行
nohup java -jar target/myapp-0.0.1-SNAPSHOT.jar > app.log 2>&1 &
7.2 Systemd 服务
# /etc/systemd/system/myapp.service
[Unit]
Description=My Spring Boot Application
After=network.target
[Service]
User=www
WorkingDirectory=/www/wwwroot/your-domain.com
ExecStart=/usr/bin/java -jar target/myapp-0.0.1-SNAPSHOT.jar
SuccessExitStatus=143
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
八、SSL证书配置
8.1 宝塔申请免费证书
宝塔面板 → 网站 → 设置 → SSL → Let's Encrypt
- 选择域名
- 点击申请
- 开启强制HTTPS
8.2 Nginx HTTPS 配置
server {
listen 443 ssl http2;
server_name your-domain.com;
ssl_certificate /www/server/panel/vhost/cert/your-domain.com/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/your-domain.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
# ... 其他配置
}
# HTTP跳转HTTPS
server {
listen 80;
server_name your-domain.com;
return 301 https://$server_name$request_uri;
}
九、常见问题排查
# Nginx配置测试
nginx -t
# 查看Nginx错误日志
tail -f /www/wwwlogs/your-domain.com.error.log
# 查看PHP错误日志
tail -f /www/server/php/82/var/log/php-fpm.log
# 查看MySQL错误日志
tail -f /www/server/data/*.err
# 检查端口占用
netstat -tlnp | grep :80
netstat -tlnp | grep :443
# 检查防火墙
firewall-cmd --list-all
# 或宝塔面板 → 安全 → 防火墙
十、部署检查清单
✅ 服务器环境安装(Nginx/PHP/MySQL)
✅ 域名解析和备案
✅ 代码上传和依赖安装
✅ 数据库创建和迁移
✅ .env环境配置
✅ Nginx配置和URL重写
✅ 文件权限设置
✅ SSL证书申请和配置
✅ 防火墙放行端口
✅ 缓存配置(Redis/OPcache)
✅ 日志配置
✅ 备份策略
✅ 监控告警
学习建议
- 先在本地用Docker模拟部署,熟悉流程
- 购买一台便宜的云服务器,动手实践
- 使用宝塔面板,降低部署难度
- 配置好备份策略,防止数据丢失
- 学会查看日志,排查问题的关键