Nginx wordpress无法在CentOS 6.8上上传文件

Nginx wordpress无法在CentOS 6.8上上传文件

当我上传照片到我的网站时我一直收到此错误。

无法创建目录 wp-content/uploads/2016/06

配置如下。/etc/nginx/conf.d/default.conf

server {
    server_name vnfintech.com www.vnfintech.com;
    root /usr/share/nginx/html;
    index index.php;

    access_log /var/log/nginx/vnfintech.com.access.log;
    error_log  /var/log/nginx/vnfintech.com.error.log debug;
    client_max_body_size 20M;
    set $cache_uri $request_uri;

    # POST requests and URLs with a query string should always go to PHP
    if ($request_method = POST) {
        set $cache_uri 'null cache';
    }

    if ($query_string != "") {
        set $cache_uri 'null cache';
    }   

    # Don't cache URIs containing the following segments
    if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php |sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {

        set $cache_uri 'null cache';
    }  

    # Don't use the cache for logged-in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+
                    |wp-postpass|wordpress_logged_in") {
        set $cache_uri 'null cache';
    }

    # Use cached or actual file if it exists, otherwise pass request to WordPress
    location / {
        try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html
                  $uri $uri/ /index.php;
    }    

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        log_not_found off;
        access_log off;
    }

    location ~ .php$ {
        try_files $uri /index.php;
    fastcgi_param   SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
    include        fastcgi_params;
        #include fastcgi_params;
        #fastcgi_pass 127.0.0.1:7777;

    }

    # Cache static files for as long as possible
    location ~*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        expires max;
        log_not_found off;
        access_log off;
    }
}

为了/etc/nginx/nginx.conf

user              nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    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;
    client_max_body_size 20M;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    }

php-fpm过程

ps aux | grep php-fpm
root      9616  0.0  0.0 283908  5828 ?        Ss   03:40   0:00 php-fpm: master process (/etc/php-fpm.conf)
nginx     9646  1.3  0.1 349956 64200 ?        S    03:40   0:10 php-fpm: pool www            
nginx     9647  1.3  0.1 349956 64100 ?        S    03:40   0:10 php-fpm: pool www            
nginx     9656  1.0  0.1 348928 62884 ?        S    03:40   0:08 php-fpm: pool www 

的权限wp-uploadchmod -R775,chown -R并且nginx:nginx

ls -l /usr/share/nginx/html/wp-content/uploads/ 
total 60 
drwxrwxr-x. 6 nginx nginx  4096 Jun 18 01:57 2015 
drwxrwxr-x. 3 nginx nginx  4096 Jun 18 01:57 2016

我无法将任何照片上传到服务器,这是怎么回事?

答案1

你的问题很可能是 SELinux。

在 /usr/share/nginx/html 中;

这是放置 Web 相关文件(在 CentOS 上)的非标准位置,这些文件通常会放在 /var/www/html 中。因此,默认策略不允许 httpd_t (nginx) 写入 usr_t 文件和目录。

您可以将文件移动到 /var/www/html... 并运行 restorecon,或者您可以向 SELinux 数据库添加新的正则表达式来设置根路径的文件上下文

semanage fcontext -a -t httpd_sys_content_t "/usr/share/nginx/html(/.*)?"
restorecon -rv /usr/share/nginx/html

答案2

这是文件权限问题,可能与你的 Web 服务器运行的用户/组有关。阅读我的指南具有权限部分的 Wordpress

这是我设置权限的核心。我将其作为脚本,可以随时运行。

设置一个,设置用户和组

useradd tim   (NB: you can name the user something else if you like!)
passwd tim    (NB: give them a secure password, but you'll never need to use it)
groupadd www-data
usermod -a -G www-data nginx   (add the nginx user to the www-data group)
chown -R tim /usr/share/nginx/
chgrp -R www-data /usr/share/nginx/
chmod -R 750 /usr/share/nginx/
chmod -R g+s /usr/share/nginx/

第二组,设置 Wordpress 文件权限

chown -R tim /usr/share/nginx/
chgrp -R www-data /usr/share/nginx/
find /usr/share/nginx/html/wordpress -type d -exec chmod 755 {} \;
find /usr/share/nginx/html/wordpress -type f -exec chmod 644 {} \;
find /usr/share/nginx/html/wordpress/wp-content/uploads -type f -exec chmod 664 {} \;
find /usr/share/nginx/html/wordpress/wp-content/plugins -type f -exec chmod 664 {} \;
find /usr/share/nginx/html/wordpress/wp-content/themes -type f -exec chmod 644 {} \;
chmod 440 /usr/share/nginx/html/wordpress/wp-config.php (NB: restrict access to this information as much as possible)
chmod -R g+s /usr/share/nginx/html/wordpress/ NB: this should make new directories take on permissions already defined

相关内容