在 Ubuntu 22.04 上配置 Nginx 和 PHP 可以按照以下步骤进行:
1. 安装 Nginx
首先,使用 APT 包管理器安装 Nginx:
sh
sudo apt update
sudo apt install nginx
安装完成后,启动 Nginx 服务并检查其状态:
sh
sudo systemctl start nginx
sudo systemctl status nginx
2. 安装 PHP 和 PHP-FPM
接下来,安装 PHP 和 PHP-FPM:
sh
sudo apt install php php-fpm
安装完成后,检查 PHP 版本:
sh
php --version
3. 配置 Nginx 使用 PHP-FPM
编辑 Nginx 的默认站点配置文件:
sh
sudo nano /etc/nginx/sites-available/default
在文件中找到 server 块,并添加以下配置:
nginx
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
保存并关闭文件后,测试 Nginx 配置是否正确:
sh
sudo nginx -t
如果没有错误,重新加载 Nginx 配置:
sh
sudo systemctl reload nginx
4. 测试 PHP
创建一个测试 PHP 文件:
sh
sudo nano /var/www/html/info.php
在文件中添加以下内容:
php
<?php
phpinfo();
?>
保存并关闭文件后,在浏览器中访问 http://your_server_ip/info.php,你应该能看到 PHP 信息页面。