要在Ubuntu上使用Nginx集成PHP环境,你需要安装Nginx、PHP-FPM(FastCGI Process Manager)以及相关的PHP扩展。以下是详细的步骤:
首先,确保你的系统包列表是最新的:
sudo apt update
安装Nginx:
sudo apt install nginx
启动Nginx并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
安装PHP-FPM以及常用的PHP扩展:
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
找到并修改以下行,确保PHP-FPM监听Unix socket:
listen = /run/php/php7.4-fpm.sock
编辑Nginx的默认站点配置文件 /etc/nginx/sites-available/default:
sudo nano /etc/nginx/sites-available/default
在 server 块中添加以下内容:
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.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
重启Nginx和PHP-FPM服务以应用更改:
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
在 /var/www/html 目录下创建一个PHP文件来测试配置:
sudo nano /var/www/html/index.php
添加以下内容:
<?php
phpinfo();
?>
打开浏览器并访问 http://your_server_ip,你应该能看到PHP信息页面,这表明Nginx和PHP-FPM已经成功集成。
通过以上步骤,你已经成功在Ubuntu上使用Nginx集成了PHP环境。