【发布时间】:2017-09-29 08:15:39
【问题描述】:
我在这里使用本指南安装了 Nginx 和 PHP:
yum install php php-mysql php-fpm
已编辑 /etc/php-fpm.d/www.conf
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nobody
listen.group = nobody
listen.owner = nobody
listen.group = nobody
跑:
systemctl start php-fpm
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name <my servers IP here - removed>;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
然后我测试了
http://<my server ip>/phpinfo.php
成功了!
然后我安装了 pear
yum install php-pear
但现在 php 文件不起作用,浏览器会尝试下载它们而不是运行它们。
我试过了:
- 重启服务器
- 重启nginx
- 重启 php-fpm
- 检查所有配置文件以确保它们与上述相同。
我完全被困住了。我不知道要检查什么才能让 php 再次工作。这是我第一次安装 Nginx。我在网上四处寻找答案并在此处查看。
我正在运行 Centos 7
帮助:)
更新:
我尝试了一个更短的配置文件:
server {
listen 80;
server_name <my servers IP here - removed>;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
但这也没有用。
我也试过了:
fastcgi_pass 127.0.0.1:9000;
代替:
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
但这也没有用。
更新 2
我发现如果我去:
http://my ip/phpinfo.php - 有效!
但如果我去:
http://domainname/phpinfo.php - 它尝试下载 PHP 文件而不是运行它。
如何让php文件在使用域名而不是IP的情况下运行?
【问题讨论】:
-
我试图卸载 pear - yum uninstall php-pear 但它没有解决问题
-
我尝试卸载重新安装 nginx & php 但它仍然不处理 php 文件
-
日志是怎么说的?
-
如果我没记错的话,我前段时间遇到过这个问题。我不记得我是如何解决的,但我认为日志对我有帮助。尝试在您的日志文件(nginx + php)上执行
tail -f,以查看调用页面时发生的确切情况:这可能是来自 php-fpm 的问题。 -
我发现使用服务器的IP而不是域名浏览到phpinfo文件时会运行php文件。关于如何使用域名而不是 IP 来实现这项工作的任何想法?
标签: php linux nginx centos pear