Ubuntu/Debian:
sudo apt update
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
CentOS/RHEL:
sudo yum install epel-release
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
Ubuntu/Debian (以PHP8.1为例):
sudo apt install php-fpm php-cli php-mysql php-curl php-gd php-mbstring php-xml php-zip
sudo systemctl start php8.1-fpm
sudo systemctl enable php8.1-fpm
CentOS/RHEL:
sudo yum install php php-fpm php-mysqlnd
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
sudo nano /etc/nginx/sites-available/default
server {
listen 80;
server_name localhost;
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/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
注意:根据你的PHP版本调整php8.1-fpm.sock
路径
sudo nginx -t
sudo systemctl reload nginx
sudo nano /var/www/html/info.php
<?php phpinfo(); ?>
http://localhost/info.php
查看PHP信息sudo systemctl status php-fpm
fastcgi_pass
指令是否正确/etc/php/8.1/fpm/pool.d/www.conf
)sudo apt install php-opcache
sudo apt install mysql-server
sudo mysql_secure_installation
sudo apt install phpmyadmin
# 配置Nginx指向phpMyAdmin目录
配置本地域名:
/etc/hosts
添加 127.0.0.1 mydev.local
mydev.local
启用错误显示(仅开发环境):
php.ini
: display_errors = On
sudo systemctl restart php-fpm
安装Xdebug进行调试:
sudo apt install php-xdebug
# 配置xdebug.ini
通过以上步骤,你应该已经成功搭建了Nginx+PHP-FPM的开发环境。根据你的具体项目需求,可能还需要安装其他PHP扩展或配置数据库连接。