Laravel在Ubuntu上怎样实现自动化部署

作者:袖梨 2026-07-20

在Ubuntu上实现Laravel项目的自动化部署可以通过多种方式来完成,其中一种常见的方法是使用Git进行版本控制,并结合CI/CD工具(如Jenkins、GitLab CI/CD、GitHub Actions等)来实现自动化部署。以下是一个基本的步骤指南:

Laravel在Ubuntu上如何实现自动化部署

1. 安装必要的软件

首先,确保你的Ubuntu系统上安装了以下软件:

  • Git
  • PHP
  • Composer
  • Nginx 或 Apache
  • MySQL 或 PostgreSQL
  • Supervisor(可选,用于管理队列进程)
sudo apt updatesudo apt install git php php-cli php-fpm composer nginx mysql-server supervisor

2. 配置Nginx

创建一个新的Nginx配置文件来托管你的Laravel应用。

sudo nano /etc/nginx/sites-available/yourdomain.com

添加以下内容:

server {listen 80;server_name yourdomain.com www.yourdomain.com;root /var/www/yourdomain.com/public;index index.php index.html index.htm;location / {try_files $uri $uri/ /index.php?$query_string;}location ~ .php$ {include snippets/fastcgi-php.conf;fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}location ~ /.ht {deny all;}}

启用配置:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/sudo nginx -tsudo systemctl restart nginx

3. 配置数据库

登录到MySQL并创建一个新的数据库和用户:

sudo mysql -u root -p

在MySQL shell中:

CREATE DATABASE yourdatabase;CREATE USER 'youruser'@'localhost' IDENTIFIED BY 'yourpassword';GRANT ALL PRIVILEGES ON yourdatabase.* TO 'youruser'@'localhost';FLUSH PRIVILEGES;EXIT;

4. 配置Laravel

将你的Laravel项目克隆到服务器上:

cd /var/wwwgit clone https://github.com/yourusername/your-laravel-project.git yourdomain.comcd yourdomain.comcomposer install --no-dev --optimize-autoloaderphp artisan key:generatephp artisan migratephp artisan config:cache

5. 设置文件权限

确保Laravel的storagebootstrap/cache目录是可写的:

sudo chown -R www-data:www-data /var/www/yourdomain.comsudo chmod -R 755 /var/www/yourdomain.comsudo chmod -R 777 /var/www/yourdomain.com/storagesudo chmod -R 777 /var/www/yourdomain.com/bootstrap/cache

6. 配置Supervisor(可选)

如果你使用Laravel的队列功能,可以配置Supervisor来管理队列进程:

sudo nano /etc/supervisor/conf.d/laravel-worker.conf

添加以下内容:

[program:laravel-worker]process_name=%(program_name)s_%(process_num)02dcommand=php /var/www/yourdomain.com/artisan queue:work --sleep=3 --tries=3autostart=trueautorestart=trueuser=www-datanumprocs=8redirect_stderr=truestdout_logfile=/var/log/supervisor/%(program_name)s.log

更新Supervisor配置并启动进程:

sudo supervisorctl rereadsudo supervisorctl updatesudo supervisorctl start laravel-worker:*

7. 设置CI/CD自动化部署

以GitHub Actions为例,创建一个.github/workflows/deploy.yml文件:

name: Deploy to Productionon:push:branches:- mainjobs:deploy:runs-on: ubuntu-lateststeps:- name: Checkout codeuses: actions/checkout@v2- name: Install dependenciesrun: |sudo apt updatesudo apt install -y git php php-cli php-fpm composer nginx mysql-server supervisorsudo apt install -y software-properties-commonsudo add-apt-repository ppa:ondrej/phpsudo apt updatesudo apt install -y php7.4 php7.4-fpm php7.4-mysqlsudo apt install -y nginxsudo apt install -y mysql-serversudo apt install -y supervisor- name: Configure Nginxrun: |sudo nano /etc/nginx/sites-available/yourdomain.com# 添加Nginx配置内容- name: Deploy Laravelrun: |cd /var/wwwgit clone https://github.com/yourusername/your-laravel-project.git yourdomain.comcd yourdomain.comcomposer install --no-dev --optimize-autoloaderphp artisan key:generatephp artisan migratephp artisan config:cachesudo chown -R www-data:www-data /var/www/yourdomain.comsudo chmod -R 755 /var/www/yourdomain.comsudo chmod -R 777 /var/www/yourdomain.com/storagesudo chmod -R 777 /var/www/yourdomain.com/bootstrap/cache- name: Restart Nginxrun: sudo systemctl restart nginx- name: Restart Supervisorrun: sudo supervisorctl restart laravel-worker:*

这样,每次你向main分支推送代码时,GitHub Actions都会自动执行部署流程。

总结

以上步骤提供了一个基本的自动化部署流程,你可以根据自己的需求进行调整和扩展。使用CI/CD工具可以大大简化部署过程,并确保每次部署的一致性和可靠性。

相关文章

精彩推荐