在自己的服务器配置过nginx以后发现访问我的网址,自动下载了一个文件,就是说没有php环境,我很纳闷,我使用lnmp已经配置过php环境了。可是这里说我没有php的环境。去论坛问了有大佬说我这是php的环境和目录不匹配。我也不太懂,毕竟没怎么接触过php.
于是到网上查资料发现:即使已经配置过php环境,但是通过nginx访问到目录之后,.php 文件没有被php解释器解析;所以就需要nginx和php之间进行通信。
具体做法就是在nginx的配置文件中的server中添加代码,这是我的代码:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
server {
default_type
'text/html'
;
charset utf-8;
listen 80;
autoindex off;
server_name blog.test.com;
root /home/wwwroot/blog;
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;
#error_page 404 /404.html;
location / {
add_header Access-Control-Allow-Origin *;
}
#主要添加这里:
location ~ \.php(.*)$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
#关键代码
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME
$document_root
$fastcgi_script_name
;
fastcgi_param PATH_INFO
$fastcgi_path_info
;
fastcgi_param PATH_TRANSLATED
$document_root
$fastcgi_path_info
;
include fastcgi_params;
}
}
|
这里使用的是其中的一种方式;你也许会看到别的方式配置;想要了解的话,这里介绍的很详细;
这里需要注意上面是在nginx的配置文件中配置;你需要检查一下php的配置确保正确;具体做法:
一般来说你安装的php-fpm.conf在
1
2
|
cd
/usr/local/php/etc
vim php-fpm.conf
#打开php-fpm.conf文件
|
可以看到配置文件中listen = /tmp/php-cgi.sock,就是对应Location中fastcgi_pass的内容;
按照以上配置nginx,配置不正确可能会出现502错误。
配置正确后,又显示Access Denied;查看了日志之后发现403错误;
如果出现No input file specified;可以参考博客
在此处我的做法就是在Location的代码中添加一行代码:
1
|
include fastcgi.conf;
|
即可成功运行。
注意:配置nginx以及php-fpm文件后需要重启nginx或php-fpm;