部署上线 #
一、服务器要求 #
1.1 系统要求 #
| 要求 | 版本 |
|---|---|
| PHP | >= 8.2 |
| Composer | >= 2.0 |
| 扩展 | OpenSSL, PDO, Mbstring, Tokenizer, XML, Ctype, JSON, BCMath |
1.2 安装PHP扩展 #
bash
# Ubuntu/Debian
sudo apt install php8.2-fpm php8.2-mysql php8.2-mbstring php8.2-xml php8.2-curl php8.2-zip php8.2-bcmath
# CentOS/RHEL
sudo yum install php8.2-fpm php8.2-mysql php8.2-mbstring php8.2-xml php8.2-curl php8.2-zip php8.2-bcmath
二、Nginx配置 #
2.1 基本配置 #
nginx
server {
listen 80;
server_name example.com;
root /var/www/example.com/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
2.2 HTTPS配置 #
nginx
server {
listen 443 ssl http2;
server_name example.com;
root /var/www/example.com/public;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers off;
add_header Strict-Transport-Security "max-age=31536000" always;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
三、环境配置 #
3.1 环境文件 #
env
APP_NAME=Laravel
APP_ENV=production
APP_KEY=your-app-key
APP_DEBUG=false
APP_URL=https://example.com
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=your-password
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
SESSION_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
3.2 生成应用密钥 #
bash
php artisan key:generate
四、部署步骤 #
4.1 基本部署 #
bash
# 1. 克隆代码
git clone your-repository /var/www/example.com
# 2. 安装依赖
composer install --no-dev --optimize-autoloader
# 3. 配置环境
cp .env.example .env
php artisan key:generate
# 4. 运行迁移
php artisan migrate --force
# 5. 配置权限
chown -R www-data:www-data /var/www/example.com
chmod -R 755 /var/www/example.com
chmod -R 775 /var/www/example.com/storage
chmod -R 775 /var/www/example.com/bootstrap/cache
4.2 零停机部署脚本 #
bash
#!/bin/bash
APP_PATH="/var/www/example.com"
RELEASES_PATH="$APP_PATH/releases"
CURRENT_LINK="$APP_PATH/current"
RELEASE_NAME=$(date +%Y%m%d%H%M%S)
mkdir -p "$RELEASES_PATH/$RELEASE_NAME"
git clone your-repository "$RELEASES_PATH/$RELEASE_NAME"
cd "$RELEASES_PATH/$RELEASE_NAME"
composer install --no-dev --optimize-autoloader
cp "$APP_PATH/.env" "$RELEASES_PATH/$RELEASE_NAME/.env"
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
ln -sfn "$RELEASES_PATH/$RELEASE_NAME" "$CURRENT_LINK"
sudo systemctl restart php8.2-fpm
cd "$RELEASES_PATH"
ls -t | tail -n +6 | xargs rm -rf
五、性能优化 #
5.1 配置缓存 #
bash
php artisan config:cache
5.2 路由缓存 #
bash
php artisan route:cache
5.3 视图缓存 #
bash
php artisan view:cache
5.4 优化自动加载 #
bash
composer install --optimize-autoloader --no-dev
5.5 优化命令 #
bash
php artisan optimize
php artisan optimize:clear
六、队列Worker #
6.1 Supervisor配置 #
ini
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/example.com/current/artisan queue:work redis --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/example.com/worker.log
stopwaitsecs=3600
6.2 启动Supervisor #
bash
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
七、定时任务 #
7.1 配置Cron #
bash
* * * * * cd /var/www/example.com/current && php artisan schedule:run >> /dev/null 2>&1
八、监控 #
8.1 日志监控 #
bash
tail -f /var/www/example.com/current/storage/logs/laravel.log
8.2 使用Telescope #
bash
composer require laravel/telescope
php artisan telescope:install
php artisan migrate
8.3 使用Horizon #
bash
composer require laravel/horizon
php artisan horizon:install
九、持续部署 #
9.1 GitHub Actions #
yaml
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
- name: Install Dependencies
run: composer install --no-dev --optimize-autoloader
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /var/www/example.com
./deploy.sh
十、安全配置 #
10.1 文件权限 #
bash
chown -R www-data:www-data /var/www/example.com
find /var/www/example.com -type f -exec chmod 644 {} \;
find /var/www/example.com -type d -exec chmod 755 {} \;
chmod -R 775 /var/www/example.com/storage
chmod -R 775 /var/www/example.com/bootstrap/cache
10.2 隐藏敏感信息 #
nginx
location ~ /\.(?!well-known).* {
deny all;
}
location ~ /storage/.*\.php {
deny all;
}
十一、总结 #
11.1 部署清单 #
| 步骤 | 命令 |
|---|---|
| 安装依赖 | composer install --no-dev |
| 生成密钥 | php artisan key:generate |
| 运行迁移 | php artisan migrate --force |
| 配置缓存 | php artisan config:cache |
| 路由缓存 | php artisan route:cache |
| 视图缓存 | php artisan view:cache |
| 重启服务 | sudo systemctl restart php8.2-fpm |
11.2 恭喜完成 #
恭喜你完成了Laravel完全指南的学习!现在你已经掌握了从基础到高级的Laravel开发技能,可以开始构建自己的Laravel应用了!
最后更新:2026-03-28