在Ubuntu系统下配置PHP与Nginx,你需要按照以下步骤操作:
首先,更新你的包列表并安装Nginx:
sudo apt update
sudo apt install nginx
安装完成后,启动Nginx服务并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
你可以通过访问服务器的IP地址或域名来验证Nginx是否安装成功。如果看到Nginx的默认欢迎页面,说明安装成功。
接下来,安装PHP及其相关模块:
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-soap
编辑Nginx的默认站点配置文件:
sudo nano /etc/nginx/sites-available/default
找到以下部分并进行修改:
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; # 根据你的PHP版本调整路径
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
确保fastcgi_pass指向正确的PHP-FPM套接字路径。如果你使用的是PHP 8.0或更高版本,请将路径更改为:
fastcgi_pass unix:/run/php/php8.0-fpm.sock;
保存并退出编辑器。
为了使配置生效,重启Nginx服务:
sudo systemctl restart nginx
在/var/www/html目录下创建一个名为info.php的文件:
sudo nano /var/www/html/info.php
在文件中添加以下内容:
<?php
phpinfo();
?>
保存并退出编辑器。
现在,访问http://your_server_ip/info.php(将your_server_ip替换为你的服务器IP地址),你应该能看到PHP的详细信息页面,这表明Nginx已经成功配置为使用PHP-FPM。
如果你启用了防火墙,确保允许HTTP和HTTPS流量:
sudo ufw allow 'Nginx Full'
通过以上步骤,你已经成功在Ubuntu系统下配置了Nginx和PHP。现在你可以开始开发你的Web应用程序了。