nginx - 无法打开主脚本

时间:2014-02-28 08:52:54

标签: php wordpress nginx

我收到错误消息:

FastCGI sent in stderr: "Unable to open primary script: /home/messi/web/wordpress/index.php (No such file or directory)" while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: www.domain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "www.domain.com

here are my configuration files:

/etc/php5/fpm/php.ini

cgi.fix_pathinfo=0
doc_root =
user_dir =
....

/etc/php5/fpm/php-fpm.conf

[global]
pid = /var/run/php5-fpm.pid
error_log = /var/log/php5-fpm.log
include=/etc/php5/fpm/pool.d/*.conf

/etc/php5/fpm/pool.d/www.conf

[www]
user = www-data
group = www-data
listen = /var/run/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
security.limit_extensions = .php .php3 .php4 .php5
php_flag[display_errors] = on
php_admin_value[error_log] = /var/log/fpm-php.www.log
php_admin_flag[log_errors] = on

/etc/nginx/nginx.conf

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    server_tokens off;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/sites-enabled/*;
}

的/ etc / nginx的/启用的站点 - / WordPress的

server {
    listen   80;
    server_name www.domain.com;
    root /home/messi/web/wordpress;
    error_log /var/log/nginx/err.wordpress.log;
    index index.php;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    location ~ /\. {
        deny all;
    }
    location ~* /(?:uploads|files)/.*\.php$ {
        deny all;
    }
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
    }
}

设置用户权限:

#adduser www-data messi
#chown -R www-data:www-data /home/messi/web
#chmod -R 664 /home/messi/web/wordpress

我该如何解决这个问题? 感谢

4 个答案:

答案 0 :(得分:32)

SELinux默认会在CentOS / RHEL 7+上导致此错误:(

要测试SELinux是否是您的困境的来源,请执行

setenforce 0

......看看一切是否有效。如果修复它,你可以关闭SELinux(弱,你比它更好),或者你可以用

重新打开它。
setenforce 1

...然后正确解决问题。

如果你这样做

tail -f /var/log/audit/audit.log

...您将看到SELinux问题。就我而言,它拒绝PHP-FPM访问Web文件。您可以运行以下指令来修复它:

setsebool -P httpd_can_network_connect_db 1
setsebool -P httpd_can_network_connect 1

这实际上一开始并没有为我修复它,但是后来恢复SELinux上下文了

restorecon -R -v /var/www

希望有所帮助。

答案 1 :(得分:7)

这可能是权限问题。

  1. 确保每个父目录对用户(nginx用户和/或php-fpm用户)具有+ x权限。

    您可以使用以下代码检查这些权限:namei -om /path/to/file

  2. 如果您有符号链接,请确保它们指向有效路径。

  3. 确保chroots可以访问正确的路径。

  4. 确保SELinux(例如Fedora / Centos)或AppArmor(例如Ubuntu)或任何其他MAC安全系统不会干扰文件访问。

    对于SeLinux: 检查/var/log/audit/audit.log或/ var / log / messages

    对于AppArmor: 我不是Ubuntu用户,据我所知,AppArmor的日志记录并不容易弄清楚。您可以在此处查看信息:http://ubuntuforums.org/showthread.php?t=1733231

答案 2 :(得分:1)

在我看来,这也是SELinux。我阅读了一些在这里找到的文档:

https://wiki.centos.org/HowTos/SELinux
https://linux.die.net/man/1/chcon

并以以下命令结束:

chcon -R -v --type=httpd_sys_content_t html/

....这将文件的上下文更改为httpd类型,这是我的Web服务器(Nginx)运行时的状态。

您可以使用以下命令找到您的Web服务器运行的上下文:

ps axZ | grep nginx

....在我看来,这给了我

system_u:system_r:**httpd_t**:s0      6246 ?        Ss     0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
system_u:system_r:**httpd_t**:s0      6249 ?        S      0:00 nginx: worker process

看到正在运行的服务的上下文是httpd_t,我(递归地)将网站根文件夹的上下文更改为

SELinux的重点是仅允许服务和进程访问与它们相同类型的文件。由于Web服务器以httpd_t的身份运行,因此将站点中文件/文件夹的上下文设置为相同是合理的。

顺便说一下,我是新手。...但这似乎是我最好的方法。它使SELinux保持启用状态,并没有降低其工作的安全性,也没有将文件的上下文与进程/服务进行匹配。

答案 3 :(得分:-3)

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; -> 
fastcgi_param SCRIPT_FILENAME/home/messi/web/wordpress$fastcgi_script_name;