在CentOS上实现ThinkPHP的热更新,可以遵循以下步骤:
首先,确保你的CentOS系统已经安装了PHP和Composer。如果没有安装,可以使用以下命令进行安装:
sudo yum install php php-cli php-fpm composer
使用Composer安装ThinkPHP框架:
composer create-project topthink/think your_project_name cd your_project_name
编辑Nginx配置文件(通常位于/etc/nginx/nginx.conf
或/etc/nginx/conf.d/default.conf
),添加以下内容:
server { listen 80; server_name your_domain.com; root /path/to/your_project_name; 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; } }
编辑Apache配置文件(通常位于/etc/httpd/conf/httpd.conf
或/etc/apache2/sites-available/your_project_name.conf
),添加以下内容:
<VirtualHost *:80> ServerName your_domain.com DocumentRoot /path/to/your_project_name <Directory /path/to/your_project_name> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> <FilesMatch \.php$> SetHandler "proxy:fcgi://localhost:9000" </FilesMatch> </VirtualHost>
启动PHP-FPM服务:
sudo systemctl start php-fpm sudo systemctl enable php-fpm
ThinkPHP支持通过Composer进行热更新。你可以使用以下命令来更新项目依赖:
composer update
为了实现热更新,你可以使用inotifywait
工具来监视文件变化并自动重启PHP-FPM服务。首先安装inotify-tools
:
sudo yum install inotify-tools
然后创建一个脚本来监视文件变化并重启PHP-FPM:
#!/bin/bash inotifywait -m -r -e modify,attrib,close_write,move,create,delete /path/to/your_project_name --format '%w%f' | while read FILE do echo "File $FILE has been modified." sudo systemctl restart php-fpm done
将上述脚本保存为watch_and_restart.sh
,并赋予执行权限:
chmod +x watch_and_restart.sh
最后,运行该脚本:
./watch_and_restart.sh
现在,当你修改项目中的文件时,inotifywait
会检测到变化并自动重启PHP-FPM服务,从而实现热更新。
通过以上步骤,你可以在CentOS上实现ThinkPHP的热更新。