在Debian系统上,Laravel项目的自动化部署可通过Git钩子、**CI/CD工具(GitHub Actions/Jenkins)或专用部署工具(Laravel Forge/Envoyer)**实现。以下是具体步骤:
在部署前,需确保Debian服务器安装必要的软件包:
sudo apt update && sudo apt upgrade -y
sudo apt install -y git nginx php8.2-fpm php8.2-cli php8.2-mysql php8.2-curl php8.2-xml php8.2-mbstring php8.2-zip unzip composer mariadb-server
配置Nginx虚拟主机(以example.com为例):
sudo nano /etc/nginx/sites-available/example.com
添加以下内容(替换example.com为你的域名):
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$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
启用配置并重启Nginx:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo systemctl restart nginx
创建Laravel项目并配置环境:
cd /var/www
composer create-project laravel/laravel example.com
cd example.com
cp .env.example .env
php artisan key:generate
设置目录权限:
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
Git钩子可在代码推送时自动触发部署脚本,适合小型项目。
cd /var/repo
sudo mkdir your-laravel-repo.git
cd your-laravel-repo.git
sudo git init --bare
hooks目录下创建post-receive文件:sudo nano /var/repo/your-laravel-repo.git/hooks/post-receive
添加以下内容(替换/var/www/example.com为项目部署路径):#!/bin/bash
TARGET="/var/www/example.com"
GIT_DIR="/var/repo/your-laravel-repo.git"
BRANCH="main"
while read oldrev newrev ref
do
if [[ $ref = refs/heads/$BRANCH ]];
then
echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
cd $TARGET
composer install --optimize-autoloader --no-dev
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:clear
chown -R www-data:www-data $TARGET/storage
chown -R www-data:www-data $TARGET/bootstrap/cache
systemctl restart nginx
echo "Deployment completed."
else
echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
fi
done
sudo chmod +x /var/repo/your-laravel-repo.git/hooks/post-receive
git remote add production ssh://user@your-server-ip/var/repo/your-laravel-repo.git
推送代码时自动触发部署:git push production main
GitHub Actions适合需要版本控制集成的项目,无需在服务器上安装额外工具。
.github/workflows/deploy.yml:name: Deploy Laravel to Debian
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: mbstring, xml, curl, zip
- name: Install dependencies
run: composer install --optimize-autoloader --no-dev
- name: Generate application key
run: php artisan key:generate
- name: Deploy to server via SSH
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /var/www/example.com
git pull origin main
composer install --optimize-autoloader --no-dev
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:clear
systemctl restart nginx
Settings > Secrets and variables > Actions,添加以下变量:
SERVER_HOST: Debian服务器IP或域名SERVER_USER: 服务器SSH用户名(如ubuntu)SSH_PRIVATE_KEY: 服务器SSH私钥(生成ssh-keygen -t rsa -b 4096,将公钥添加到服务器~/.ssh/authorized_keys)main分支,GitHub Actions会自动执行部署流程。Laravel Forge是Laravel官方提供的服务器管理工具,支持一键部署、Git集成、数据库备份等功能。
composer install、migrate等命令)。Laravel Envoyer专为Laravel设计,支持零停机部署、实时监控、回滚功能。
composer install、migrate)。以上方法覆盖了不同场景下的自动化部署需求,可根据项目规模和团队习惯选择合适的方式。