在Ubuntu系统中,Nginx和PHP-FPM通常一起使用来提供高性能的PHP网站和应用服务。以下是Nginx与PHP-FPM配合的基本步骤:
首先,确保你的系统是最新的:
sudo apt update
sudo apt upgrade
然后安装Nginx和PHP-FPM:
sudo apt install nginx php-fpm
PHP-FPM的配置文件通常位于 /etc/php/7.x/fpm/pool.d/www.conf
(其中 7.x
是你的PHP版本)。你可以根据需要修改这个文件,例如设置监听地址和端口。
默认情况下,PHP-FPM监听Unix套接字 /run/php/php7.x-fpm.sock
。你可以检查并确认这一点。
编辑Nginx的默认站点配置文件 /etc/nginx/sites-available/default
或创建一个新的配置文件。
以下是一个基本的Nginx配置示例,用于处理PHP请求:
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; # 确保这里的路径与PHP-FPM配置一致
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 # 替换为你的PHP版本
sudo systemctl enable php7.x-fpm
确保Nginx和PHP-FPM都在运行,并且配置正确:
sudo nginx -t
sudo systemctl status nginx
sudo systemctl status php7.x-fpm
访问你的网站,确保PHP文件能够正确解析和执行。
如果遇到问题,可以查看Nginx和PHP-FPM的日志文件进行调试:
/var/log/nginx/error.log
/var/log/php7.x-fpm.log
(替换为你的PHP版本)根据需要调整配置,例如增加PHP内存限制、执行时间等。
通过以上步骤,你应该能够在Ubuntu系统上成功配置Nginx和PHP-FPM,提供高效的PHP服务。