问题

在安装ngnix与相应的php服务后发现访问php文件是直接下载下来并没有解析。

原因:

因为nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端
nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx
而PHP-FPM就是一个PHP FastCGI管理器
简单来说php-fpm把nginx与php服务联系到一起。

解决方法

1,配置nginx。conf文件

在server{中添加如下代码:
php-fpm默认端口是9000

  1. location ~ \.php(.*)$ {
  2. fastcgi_pass 127.0.0.1:9000;
  3. fastcgi_index index.php;
  4. fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
  5. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  6. fastcgi_param PATH_INFO $fastcgi_path_info;
  7. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  8. include fastcgi_params;
  9. }

这样配置好后重新启动nginx服务。
systemctl restart nginx

3,开放端口

发现虽然不下载php文件了,但还是访问不了。
是因为php-fpm服务没有安装好
查看端口
netstat -tunlp | grep 9000
发现没有回显
安装php fpm依次执行下面命令:

yum install php72w-fpm
systemctl start php-fpm.service #启动php
systemctl enable php-fpm.service#设置开机自启动
#其他命令
systemctl restart php-fpm.service #重新启动php-fpm服务
systemctl status php-fpm.service #查看启动php-fpm服务

这样便可以访问php网页了。