在Ubuntu系统上,PHP-FPM(FastCGI Process Manager)与Nginx的配合使用可以有效地处理PHP请求。以下是详细的步骤来配置它们:
首先,更新你的包列表并安装Nginx和PHP-FPM:
sudo apt update
sudo apt install nginx 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 ; for a unix socket
listen = 127.0.0.1:9000 ; for a tcp socket
编辑Nginx的默认站点配置文件 /etc/nginx/sites-available/default
:
sudo nano /etc/nginx/sites-available/default
确保配置文件中有以下内容来处理PHP请求:
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 ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.x-fpm.sock; # 使用unix socket
# fastcgi_pass 127.0.0.1:9000; # 或者使用tcp socket
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
启动Nginx和PHP-FPM服务,并设置它们在系统启动时自动运行:
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php7.x-fpm
sudo systemctl enable php7.x-fpm
创建一个简单的PHP文件来测试配置是否正确:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
然后在浏览器中访问 http://your_server_ip/info.php
,你应该能看到PHP信息页面。
为了提高安全性,你可以考虑以下几点:
通过以上步骤,你应该能够在Ubuntu系统上成功配置Nginx和PHP-FPM来处理PHP请求。