安装 pear 后 Nginx 不是 运行 php 文件
Nginx not running php files after pear install
我安装了 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 文件而不是 运行 下载它。
如何在使用域名而不是 IP 的情况下制作 php 个文件 运行?
你试过编辑吗
/etc/nginx/sites-enabled/default ?
然后需要编辑如下
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
之后,你必须重新启动php-fpm和nginx
service nginx restart && service php-fpm restart
尝试使用以下位置配置,
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
并事先清除缓存。
对于您的第二次更新,请在下面的服务器块中尝试,
listen 127.0.0.1:8000; #your ip with port
server_name example.com; #domain name
最近我也需要搭建linux和php环境,所以我严格按照你的步骤搭建。
我在设置 LEMP 环境时遇到了这两个问题,
b。 php-fpm 问题,post and answer
我没有遇到你遇到的问题,所以我把环境展示给你,你可以对比一下。
Centos 版本
vi /etc/centos-release
CentOS Linux release 7.3.1611 (Core)
Mysql版本
mysql -u root -p
//enter password
Server version: 5.5.52-MariaDB MariaDB Server
Php版本
php -v
PHP 5.4.16 (cli) (built: Nov 6 2016 00:29:02)
梨资讯
pear list
// 参考这个 post
Installed packages, channel pear.php.net:
=========================================
Package Version State
Archive_Tar 1.3.11 stable
Console_Getopt 1.3.1 stable
PEAR 1.9.4 stable
Structures_Graph 1.0.4 stable
XML_Util 1.2.1 stable
Nginx 配置文件
vi /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name 192.168.236.129;
# 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;
}
}
info.php vi /usr/share/nginx/html/info.php
<?php phpinfo(); ?>
根据您的第二次更新,您的 Listen 指令有问题。 nginx
只监听 Listen 指令中提到的 IP 地址或域名。马克西米利安给你正确的答案。如果你把 listen 80
放在你的配置文件中,你就会解决你的问题,但是只有一个站点,用这个听。如果您想在您的域上配置 PHP,您可以配置 listen yourdomain.com:80
.
我安装了 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 文件而不是 运行 下载它。
如何在使用域名而不是 IP 的情况下制作 php 个文件 运行?
你试过编辑吗
/etc/nginx/sites-enabled/default ?
然后需要编辑如下
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
之后,你必须重新启动php-fpm和nginx
service nginx restart && service php-fpm restart
尝试使用以下位置配置,
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
并事先清除缓存。
对于您的第二次更新,请在下面的服务器块中尝试,
listen 127.0.0.1:8000; #your ip with port
server_name example.com; #domain name
最近我也需要搭建linux和php环境,所以我严格按照你的步骤搭建。
我在设置 LEMP 环境时遇到了这两个问题,
b。 php-fpm 问题,post and answer
我没有遇到你遇到的问题,所以我把环境展示给你,你可以对比一下。
Centos 版本
vi /etc/centos-release
CentOS Linux release 7.3.1611 (Core)
Mysql版本
mysql -u root -p
//enter password
Server version: 5.5.52-MariaDB MariaDB Server
Php版本
php -v
PHP 5.4.16 (cli) (built: Nov 6 2016 00:29:02)
梨资讯
pear list
// 参考这个 post
Installed packages, channel pear.php.net: ========================================= Package Version State Archive_Tar 1.3.11 stable Console_Getopt 1.3.1 stable PEAR 1.9.4 stable Structures_Graph 1.0.4 stable XML_Util 1.2.1 stable
Nginx 配置文件
vi /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name 192.168.236.129;
# 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;
}
}
info.php vi /usr/share/nginx/html/info.php
<?php phpinfo(); ?>
根据您的第二次更新,您的 Listen 指令有问题。 nginx
只监听 Listen 指令中提到的 IP 地址或域名。马克西米利安给你正确的答案。如果你把 listen 80
放在你的配置文件中,你就会解决你的问题,但是只有一个站点,用这个听。如果您想在您的域上配置 PHP,您可以配置 listen yourdomain.com:80
.