在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版本)。你可以根据需要进行一些基本配置,例如监听地址和端口。
; Default settings for FPM
; Start servers
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
; Listen on TCP socket or Unix socket
listen = /run/php/php7.x-fpm.sock ; for Linux system
;listen = 127.0.0.1:9000 ; for MacOS/Linux system
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
编辑Nginx的默认站点配置文件 /etc/nginx/sites-available/default,或者创建一个新的站点配置文件。
server {
listen 80;
server_name 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; # 使用Unix socket
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
启动PHP-FPM和Nginx服务,并设置它们在系统启动时自动运行。
sudo systemctl start php7.x-fpm
sudo systemctl enable php7.x-fpm
sudo systemctl start nginx
sudo systemctl enable nginx
确保Nginx和PHP-FPM都在正常运行,并且PHP文件能够正确处理。
sudo nginx -t
sudo systemctl restart nginx
访问你的域名或服务器IP地址,应该能够看到PHP文件被正确解析和执行。
如果你使用的是UFW防火墙,确保允许HTTP和HTTPS流量。
sudo ufw allow 'Nginx Full'
通过以上步骤,你就可以在Ubuntu系统下成功配置PHP-FPM与Nginx协同工作,提供高效的PHP网站服务。