本文目录导读:

其实你想问的是“PHP项目完成后该怎么做”吧?这个问题很实际,我来给你梳理一下完整的上线流程和后续操作。
上线前的准备工作
代码审查
// 检查常见安全问题 - SQL注入:确保使用了预处理语句 - XSS攻击:输出时使用htmlspecialchars() - CSRF:检查是否添加了Token验证 - 文件上传:限制类型和大小
环境检查清单
□ PHP版本兼容性
□ 扩展是否安装(PDO、GD、mbstring等)
□ 错误报告关闭(display_errors = Off)
□ 时区设置正确
□ 文件权限设置(文件夹755,文件644)
项目打包与部署
手动部署
# 1. 导出代码(排除不需要的文件)
rsync -avz --exclude='.git' \
--exclude='node_modules' \
--exclude='tests' \
--exclude='.env' \
./ your-server:/var/www/project/
# 2. 配置Nginx/Apache
# Nginx示例
server {
listen 80;
server_name your-domain.com;
root /var/www/project/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
使用Composer自动化
{
"scripts": {
"deploy": [
"@php artisan optimize",
"@php artisan migrate --force",
"@php artisan config:cache"
]
}
}
数据库操作
// 执行迁移 php artisan migrate --force // 填充初始数据 php artisan db:seed --class=ProductionSeeder // 备份生产数据库 mysqldump -u root -p yourdb > backup_$(date +%Y%m%d).sql
性能优化
缓存配置
// 开启OPcache
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
// 配置Redis/Memcached
'default' => env('CACHE_DRIVER', 'redis'),
// 生成配置缓存
php artisan config:cache
php artisan route:cache
php artisan view:cache
前端资源优化
# 压缩合并CSS/JS npm run production # 使用CDN https://cdn.example.com/static/
监控与日志
日志配置
// config/logging.php
'channels' => [
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'error'),
'days' => 14,
],
],
错误监控
- Sentry集成
- 自定义错误页面(50x.html)
- 设置健康检查端点
安全加固
// 强制HTTPS
if (!$request->secure() && env('APP_ENV') === 'production') {
return redirect()->secure($request->getRequestUri());
}
// 安全头
header('X-Frame-Options: DENY');
header('X-Content-Type-Options: nosniff');
header('Strict-Transport-Security: max-age=31536000');
持续部署(推荐)
Git工作流
# 使用GitHub Actions
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.KEY }}
script: |
cd /var/www/project
git pull origin main
composer install --no-dev
php artisan migrate --force
php artisan optimize
上线检查清单
□ 数据库连接正常
□ 邮件发送功能正常
□ 定时任务已配置(crontab)
□ 队列服务已启动
□ 备份策略已实施
□ SSL证书有效
□ SEO配置(robots.txt, sitemap.xml)
□ 404/500页面自定义
□ 联系管理员功能正常
□ 监控报警已配置
常见问题处理
报错排查
// 开启调试模式(临时)
'debug' => env('APP_DEBUG', false),
// 查看错误日志
tail -f storage/logs/laravel-$(date +%Y-%m-%d).log
回滚方案
# 代码回滚 git revert HEAD git push origin main # 数据库回滚 php artisan migrate:rollback --step=1 # 使用备份恢复 mysql -u root -p yourdb < backup.sql
总结建议
- 第一次上线:建议手动部署,熟悉流程
- 重要项目:使用容器化(Docker)部署
- 访问量大:配置负载均衡、CDN
- 持续维护:建立版本发布规范和回滚机制
- 文档记录:记录部署步骤、配置变更、常见问题
这样一套流程走下来,你的PHP项目才能稳定、安全地运行在生产环境,上线不是终点,而是运维的开始。