在Ubuntu系统中,PHP-FPM(FastCGI Process Manager)与Nginx的配合使用可以有效地处理PHP请求。以下是配置步骤:
1. 安装Nginx和PHP-FPM
首先,确保你的系统是最新的:
sudo apt update
sudo apt upgrade
然后,安装Nginx和PHP-FPM:
sudo apt install nginx php-fpm
2. 配置PHP-FPM
PHP-FPM的默认配置文件通常位于 /etc/php/7.x/fpm/pool.d/www.conf(这里的 7.x 是你的PHP版本)。你可以根据需要进行调整。
编辑配置文件:
sudo nano /etc/php/7.x/fpm/pool.d/www.conf
找到并修改以下行,以允许Nginx通过Unix套接字连接:
listen = /run/php/php7.x-fpm.sock
保存并退出编辑器。
3. 配置Nginx
编辑Nginx的默认站点配置文件:
sudo nano /etc/nginx/sites-available/default
找到 server 块,并进行如下修改:
3.1 配置PHP处理
在 location ~ \.php$ 块中,添加以下内容:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.x-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
3.2 配置静态文件和目录
确保你有适当的配置来处理静态文件和目录:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.x-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
4. 重启服务
保存并退出编辑器后,重启Nginx和PHP-FPM服务以应用更改:
sudo systemctl restart nginx
sudo systemctl restart php7.x-fpm
5. 测试配置
创建一个简单的PHP文件来测试配置:
echo "" | sudo tee /var/www/html/info.php
然后在浏览器中访问 http://your_server_ip/info.php,你应该能看到PHP信息页面。
6. 安全性考虑
- 确保你的Nginx和PHP-FPM配置文件中没有不必要的权限设置。
- 定期更新你的系统和软件包以保持安全。
通过以上步骤,你应该能够在Ubuntu系统中成功配置Nginx和PHP-FPM来处理PHP请求。
以上就是关于“ubuntu中php-fpm与nginx如何配合”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm