在CentOS上实现ThinkPHP项目的自动部署,可以借助一些自动化工具和脚本来简化流程。以下是一个基本的步骤指南,使用Git进行版本控制,并结合Shell脚本和Supervisor来管理应用进程。
sudo yum update -y
sudo yum install -y epel-release
sudo yum install -y git nginx php-fpm php-mysql mariadb-server supervisor
创建一个新的Nginx配置文件:
sudo vi /etc/nginx/conf.d/your_project.conf
添加以下内容:
server {
listen 80;
server_name your_domain.com;
root /path/to/your/project;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
重启Nginx:
sudo systemctl restart nginx
编辑PHP-FPM配置文件:
sudo vi /etc/php-fpm.d/www.conf
修改以下内容:
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
重启PHP-FPM:
sudo systemctl restart php-fpm
创建一个新的Supervisor配置文件:
sudo vi /etc/supervisor/conf.d/your_project.conf
添加以下内容:
[program:your_project]
command=/usr/bin/php /path/to/your/project/index.php start
autostart=true
autorestart=true
stderr_logfile=/var/log/your_project.err.log
stdout_logfile=/var/log/your_project.out.log
user=nginx
environment=PHPRC="/path/to/your/project/php.ini"
更新Supervisor配置并启动进程:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start your_project
在你的项目目录下初始化Git仓库:
cd /path/to/your/project
git init
git remote add origin <your-repository-url>
git pull origin master
创建一个自动部署脚本:
sudo vi /usr/local/bin/deploy.sh
添加以下内容:
#!/bin/bash
# Clone the latest code from Git
cd /path/to/your/project
git pull origin master
# Install dependencies
composer install --no-interaction --prefer-dist --optimize-autoloader
# Run migrations or other setup scripts
php artisan migrate --force
# Restart PHP-FPM and Supervisor
sudo systemctl restart php-fpm
sudo supervisorctl restart your_project
赋予脚本执行权限:
sudo chmod +x /usr/local/bin/deploy.sh
使用cron
设置定时任务来定期运行部署脚本:
sudo crontab -e
添加以下内容:
0 * * * * /usr/local/bin/deploy.sh >> /var/log/deploy.log 2>&1
这会每小时运行一次部署脚本。
通过以上步骤,你可以在CentOS上实现ThinkPHP项目的自动部署。关键在于使用Git进行版本控制,结合Shell脚本和Supervisor来管理应用进程。你可以根据实际需求进一步优化和扩展这些步骤。