在Ubuntu系统中,PHP-FPM(FastCGI Process Manager)与Nginx配合工作可以提供高效的PHP处理能力。以下是配置PHP-FPM与Nginx配合的步骤:
1. 安装PHP-FPM和Nginx
首先,确保你已经安装了PHP-FPM和Nginx。你可以使用以下命令来安装它们:
sudo apt update
sudo apt install nginx php-fpm
2. 配置PHP-FPM
PHP-FPM的配置文件通常位于 /etc/php/版本号/fpm/pool.d/www.conf。你需要确保PHP-FPM监听在正确的socket或端口上。
使用Unix Socket
如果你选择使用Unix Socket,编辑 /etc/php/版本号/fpm/pool.d/www.conf 文件,找到以下行并取消注释:
listen = /run/php/php7.4-fpm.sock
确保Nginx配置文件中使用相同的socket路径。
使用TCP端口
如果你选择使用TCP端口,编辑 /etc/php/版本号/fpm/pool.d/www.conf 文件,找到以下行并取消注释:
listen = 127.0.0.1:9000
确保Nginx配置文件中使用相同的端口。
3. 配置Nginx
编辑Nginx的默认站点配置文件 /etc/nginx/sites-available/default 或创建一个新的配置文件。
使用Unix Socket
如果你使用Unix Socket,Nginx配置文件中的 location ~ \.php$ 部分应该如下所示:
server {
listen 80;
server_name your_domain.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
使用TCP端口
如果你使用TCP端口,Nginx配置文件中的 location ~ \.php$ 部分应该如下所示:
server {
listen 80;
server_name your_domain.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
4. 重启服务
完成配置后,重启Nginx和PHP-FPM服务以应用更改:
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
5. 测试配置
创建一个简单的PHP文件(例如 info.php)放在你的网站根目录下,内容如下:
phpinfo();
?>
访问 http://your_domain.com/info.php,如果看到PHP信息页面,说明配置成功。
通过以上步骤,你就可以在Ubuntu系统中成功配置PHP-FPM与Nginx配合工作。
以上就是关于“ubuntu中php-fpm如何与Nginx配合”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm