ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Nginx配置php留档

2026/9/21 3:29:20 拓冰建站 浏览量
Nginx配置php留档

好久没有用过php了,近几日配置nginx+php,留档。

安装

ubunt下nginx和php都可以使用apt安装:

sudo apt install nginx php8

如果想安装最新的php8.2,则需要运行下面语句:

sudo dpkg -l | grep php | tee packages.txt
sudo add-apt-repository ppa:ondrej/php # Press enter when prompted.
sudo apt update
sudo apt install php8.2 php8.2-cli php-8.2{bz2,curl,mbstring,intl}sudo apt install php8.2-fpm
# OR
# sudo apt install libapache2-mod-php8.2sudo a2enconf php8.2-fpm# When upgrading from older PHP version:
sudo a2disconf php8.1-fpm## Remove old packages
sudo apt purge php8.1*

安装好后,可以使用php -v来查看当前的php版本。

有时候需要重启nginx或者php-fpm服务,使用指令为:

sudo service nginx restart
sudo systemctl restart php8.2-fpm

配置:

配置也很简单,只需要在/etc/nginx/conf.d目录中写一个配置文件,如wow.conf

server {listen 80;server_name wow.airoot.org;root /var/www/;  index index.php index.html index.htm;location / {  try_files $uri $uri/ =404;}  location ~ \.php$ {  fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;  # 根据你的 PHP-FPM 配置修改  fastcgi_index index.php;  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;  }  location ~ /\.ht {deny all;  }  
}

测试

在php root目录 /var/www/中,写一个test.php文件:

 <?php
phpinfo();?>

浏览http://wow.airoot.org

若能出现php信息页面,则证明nginx+php配置成功。

后面就可以装比如wordpress套件了。