在 Ubuntu 下实现 ThinkPHP 的分布式部署,你可以使用 Nginx 作为 Web 服务器,并结合 PHP-FPM 来处理 PHP 请求。以下是一个基本的步骤指南:
首先,更新你的包列表并安装 Nginx 和 PHP-FPM:
sudo apt update
sudo apt install nginx php-fpm
编辑 Nginx 的默认配置文件 /etc/nginx/sites-available/default,或者创建一个新的配置文件:
sudo nano /etc/nginx/sites-available/thinkphp
添加以下配置:
server {
listen 80;
server_name your_domain.com; # 替换为你的域名或 IP 地址
root /path/to/your/thinkphp/project; # 替换为你的 ThinkPHP 项目路径
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/thinkphp /etc/nginx/sites-enabled/
测试 Nginx 配置是否正确:
sudo nginx -t
如果没有错误,重新加载 Nginx:
sudo systemctl reload nginx
编辑 PHP-FPM 的配置文件 /etc/php/7.4/fpm/pool.d/www.conf(根据你的 PHP 版本调整路径):
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
找到 listen 行,将其修改为:
listen = /var/run/php/php7.4-fpm.sock
保存并退出编辑器,然后重启 PHP-FPM:
sudo systemctl restart php7.4-fpm
将你的 ThinkPHP 项目上传到服务器上的指定目录(例如 /path/to/your/thinkphp/project)。
确保项目的目录权限正确:
sudo chown -R www-data:www-data /path/to/your/thinkphp/project
sudo chmod -R 755 /path/to/your/thinkphp/project
确保 Nginx 和 PHP-FPM 服务正在运行:
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
如果你需要实现分布式部署,可以使用 Nginx 的负载均衡功能。编辑 Nginx 配置文件 /etc/nginx/nginx.conf,添加负载均衡配置:
http {
upstream thinkphp_cluster {
server unix:/var/run/php/php7.4-fpm.sock;
server unix:/var/run/php/php7.4-fpm2.sock; # 添加更多的 PHP-FPM 实例
}
server {
listen 80;
server_name your_domain.com;
root /path/to/your/thinkphp/project;
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 thinkphp_cluster;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
}
保存并退出编辑器,然后重新加载 Nginx:
sudo systemctl reload nginx
确保每个 PHP-FPM 实例都在运行:
sudo systemctl start php7.4-fpm
sudo systemctl start php7.4-fpm2
通过以上步骤,你可以在 Ubuntu 上实现 ThinkPHP 的分布式部署。根据你的实际需求,可能还需要进行更多的配置和优化。