在Ubuntu系统中,PHP-FPM(FastCGI Process Manager)与Nginx配合使用可以实现高效的PHP网站和应用程序的运行。以下是配置PHP-FPM与Nginx配合使用的步骤:
首先,确保你已经安装了PHP-FPM。你可以使用以下命令来安装:
sudo apt update
sudo apt install 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
找到以下行并进行修改:
;listen = /run/php/php7.x-fpm.sock
listen = 127.0.0.1:9000
启动PHP-FPM服务并设置开机自启:
sudo systemctl start php7.x-fpm
sudo systemctl enable php7.x-fpm
编辑Nginx的默认站点配置文件或创建一个新的站点配置文件。通常位于 /etc/nginx/sites-available/ 目录下。
sudo nano /etc/nginx/sites-available/default
在 server 块中添加或修改以下内容:
server {
listen 80;
server_name your_domain.com www.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.x-fpm.sock; # 或者使用 127.0.0.1:9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
创建一个符号链接以启用新的站点配置:
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
测试Nginx配置文件是否有语法错误:
sudo nginx -t
如果没有错误,重新加载Nginx服务:
sudo systemctl reload nginx
现在,你应该能够通过浏览器访问你的网站,并看到PHP文件被正确处理。你可以创建一个简单的PHP文件(例如 info.php)来验证:
<?php
phpinfo();
?>
将这个文件放在 /var/www/html 目录下,然后在浏览器中访问 http://your_domain.com/info.php,你应该能看到PHP的信息页面。
通过以上步骤,你已经成功配置了PHP-FPM与Nginx的配合使用。