在Ubuntu系统中,PHP-FPM(FastCGI Process Manager)与Nginx配合使用可以实现高效的PHP网站和应用服务。以下是配置PHP-FPM与Nginx配合的基本步骤:
1. 安装PHP-FPM和Nginx
首先,确保你的系统是最新的,然后安装PHP-FPM和Nginx。
sudo apt update
sudo apt upgrade
sudo apt install php-fpm nginx
2. 配置PHP-FPM
PHP-FPM的配置文件通常位于 /etc/php/7.x/fpm/pool.d/www.conf(这里的 7.x 是你的PHP版本)。你需要确保以下配置项正确:
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = www-data
group = www-data
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php/php7.x-fpm.sock
; Set listen(2) backlog.
; Default: 128 - Max: 65535
listen.backlog = 128
; Listen for IPv6 connections on the unix socket.
; Default: "no"
listen.allowed_clients = 127.0.0.1
3. 配置Nginx
编辑Nginx的默认站点配置文件 /etc/nginx/sites-available/default 或创建一个新的配置文件。
sudo nano /etc/nginx/sites-available/default
在 server 块中添加或修改以下内容:
server {
listen 80;
server_name example.com www.example.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.x-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
确保将 example.com 替换为你的域名,并将 /var/www/html 替换为你的网站根目录。
4. 启动和启用服务
启动PHP-FPM和Nginx服务,并设置它们在系统启动时自动运行。
sudo systemctl start php7.x-fpm
sudo systemctl enable php7.x-fpm
sudo systemctl start nginx
sudo systemctl enable nginx
5. 测试配置
最后,测试Nginx配置是否正确,并重新加载Nginx服务。
sudo nginx -t
sudo systemctl reload nginx
现在,你应该能够通过浏览器访问你的网站,并且PHP文件应该由PHP-FPM处理。
注意事项
- 确保防火墙允许HTTP(端口80)和HTTPS(端口443)流量。
- 如果你使用的是HTTPS,请确保配置SSL证书并修改Nginx配置以监听443端口。
- 根据需要调整PHP-FPM和Nginx的配置参数,以优化性能和安全性。
通过以上步骤,你应该能够在Ubuntu系统上成功配置PHP-FPM与Nginx的配合使用。
以上就是关于“ubuntu php-fpm与nginx如何配合”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm