0

I kept getting this error when I upload a photo to my site.

Unable to create directory wp-content/uploads/2016/06

The configures are like this . For /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;
    }
}

For /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 process

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 

The permission of wp-upload is chmod -R as 775, and chown -R as 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

What is wrong that I can't upload any photos to the server?

2

Your problem will likely be SELinux.

/usr/share/nginx/html;

This is a non-standard place to put web related files (on CentOS), which would normally go in /var/www/html. As such the default policy will not allow httpd_t (nginx)to write to usr_t files and directories.

You can move your files into /var/www/html... and run restorecon or you can add a new regex to the SELinux database to set the file contexts for your root path

semanage fcontext -a -t httpd_sys_content_t "/usr/share/nginx/html(/.*)?"
restorecon -rv /usr/share/nginx/html
  • The /usr/share seems to be some kind of a default, but I do actually have all my files in /var/www as I find it easier to type. You could be onto something. – Tim Jun 18 '16 at 21:08
1

It's a file permissions issue, probably in conjunction with the user/group your web server is running as. Read my guide to Wordpress which has a permissions section.

Here's the core of what I do to set up permissions. I have it as a script I can run any time.

Set one, setting up users and groups

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/

Set two, setting up file permissions for 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
  • it still doesn't work @Tim, same error occurs :( – Loi.Luu Jun 18 '16 at 3:50
  • Permissions are the problem. "It still doesn't work" doesn't help solve the issue. You need to understand file ownership and groups, users that software runs as, and then set things up based on what you learn. If you post the newest exact error message you're getting along with information about what it's complaining about we may be able to help, but if you do that you'll probably work it out yourself. – Tim Jun 18 '16 at 4:40
  • thanks for the reply. It confuses me because even when I do chmod -R 777 /usr/share/nginx/html. The problem still persists. – Loi.Luu Jun 18 '16 at 4:52
  • Look at the other answer, I could be incorrect. Note that 777 doesn't always work - making permissions too wide can stop some things working. – Tim Jun 18 '16 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.