问题描述:
开启apache或nginx,访问php文件要么直接显示php文件内容,要么直接下载,这都是php文件无法解析的问题,需要nginx+php-fpm解决
1、安装NGINX
查看是否安装,如下图则已安装
#apt-cache policy nginx
未安装则执行安装nginx
#apt-get install nginx
补充说明
Nginx 网站目录是 /var/www/html
将项目代码放在这个文件夹下(类似apache/htdocs)
Nginx 网站配置文件位置是 /etc/nginx/sites-available/default
启动Nginx服务
# systemctl start nginx
2、安装PHP8.2和PHP扩展
查看是否安装,如下图则已安装
# apt-cache policy php php-fpm
未安装则执行安装
# apt-get install php8.2 php8.2-fpm
3、配置NGINX和PHP-FPM
配置nginx
# vim /etc/nginx/sites-available/default
location ~ \.php$ { include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): fastcgi_pass unix:/run/php/php8.2-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
配置php-fpm(默认不用配置)
# vim /etc/php/8.2/fpm/pool.d/www.conf
listen = /run/php/php8.2-fpm.sock
重启nginx和php-fpm
# systemctl restart nginx.service
# systemctl start php8.2-fpm.service
4、测试是否能正常解析php
# cd /var/www/html
# vim 11.php
浏览器访问,出现以下页面表示能正常解析php文件