在Debian系统上,Nginx与PHP-FPM配合使用可以提供高效的PHP网站服务。以下是详细的步骤和配置指南:
首先,确保你的系统是最新的,然后安装Nginx:
sudo apt update
sudo apt install nginx
接下来,安装PHP-FPM及其常用扩展:
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip
编辑PHP-FPM的配置文件 /etc/php/7.4/fpm/pool.d/www.conf(注意:根据你安装的PHP版本,路径可能会有所不同):
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
找到以下行并进行修改:
;listen = /run/php/php7.4-fpm.sock
listen = /var/run/php/php7.4-fpm.sock
确保PHP-FPM监听在正确的socket文件上。
编辑Nginx的默认站点配置文件 /etc/nginx/sites-available/default:
sudo nano /etc/nginx/sites-available/default
修改或添加以下内容:
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:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
确保将 your_domain.com 替换为你的实际域名,并根据需要调整 root 目录。
启动Nginx和PHP-FPM服务,并设置它们在系统启动时自动运行:
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
创建一个简单的PHP文件来测试配置是否正确:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
然后在浏览器中访问 http://your_domain.com/info.php,你应该能看到PHP信息页面。
sudo ufw allow 'Nginx Full'
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain.com
通过以上步骤,你应该能够在Debian系统上成功配置Nginx与PHP-FPM,提供高效的PHP网站服务。