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

首先,确保你的Ubuntu系统上安装了以下软件:
sudo apt updatesudo apt install git php php-cli php-fpm composer nginx mysql-server supervisor创建一个新的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登录到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;将你的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确保Laravel的storage和bootstrap/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如果你使用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:*以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工具可以大大简化部署过程,并确保每次部署的一致性和可靠性。