mishfaq
By:
mishfaq

404 Not Found nginx/1.10.0 (Ubuntu)

August 11, 2017 280 views
WordPress Nginx Ubuntu 16.04

Hi guys,

I install wordpress on Lemp on 16.04 ( this was installed using the via DO droplet) following this tutorial https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lemp-on-ubuntu-16-04 everything went well, but when I changed the permalinks to post-name since then apart from the home page anything else returns with this error ( 404 Not Found nginx/1.10.0 (Ubuntu) ) now I did of course followed the instruction for making changes in the /etc/nginx/sites-available/default take a look at my file below


GNU nano 2.5.3 File: /etc/nginx/sites-available/default

Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.

Default server configuration

server {
listen 80 defaultserver;
listen [::]:80 default
server;
# SSL configuration
#
# listen 443 ssl defaultserver;
# listen [::]:443 ssl default
server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    http {
index index.html index.htm index.php index.nginx-debian.html;

    server_name _;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            #try_files $uri $uri/ =404;
             try_files $uri $uri/ /index.php$is_args$args;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #       include snippets/fastcgi-php.conf;
    #
    #       # With php7.0-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
    #       # With php7.0-fpm:
    #       fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #       deny all;
    #}
    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
    expires max;
    log_not_found off;

}


I didn't follow the instructions for installing ssl because this is test and I m did made another user with sudo privileges but there was nothing in the tutorial about switching to non root user at any point, so I continued using the root user for all the process.
I m completely new to this so can someone please help me with this. Also I have a sub domain that has it's own WordPress installation but would be placed in the same dictionary and I m also planing on installing Vanilla forum as a subdomain or example.com/forum. For now I m just doing this on test environment using DO droplet and the ip address as the domain.
Thanks in advance for any help.

4 Answers
xMudrii August 11, 2017
Accepted Answer

Make sure you have your WordPress site stored in /var/www/html and set correct permissions on /var/www/html and it's content. To check, the following simple ls command will do the job:

  • ls -ld /var/www/html

Also, for content:

  • ls -l /var/www/html

Recommended permissions are sammy:www-data, where sammy is your non-root user. If it's not like that, you can set them by running:

  • sudo chown -R sammy:www-data /var/www/html

Also, make sure you have WordPress unpacked in /var/www/html and not in /var/www/html/wordpress or anything similar.
You can also post result of ls -l /var/www/html, so we're sure you did it right.

If nothing helps, best bet are to check logs. Check for latest entries in /var/log/nginx/error.log and /var/log/nginx/access.log. The first one is the most relevant and will probably tell you the reason of error. If you're not sure how to debug it, you can post it here (make sure to redact any sensible information).

As of forum and another WP site. You can't do it like you explained.
First, you can't have two WP sites in one directory. The best way would be to create appropriate directories in /var/www for each site. For example in /var/www/example.com store WordPress site running on main domain - example.com, and in /var/www/subdomain.example.com store site running on the subdomain.
As for forum, I wouldn't recommend using example.com/forum as it can break WordPress permalinks. Do it with subdomains, on the same way I explained above - create directory for it, e.g. /var/www/forum.example.com and store it there.
All this would require you to create appropriate Nginx server blocks and the How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 16.04 will show you how to do it.

When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server. In this guide, we'll discuss how to configure server blocks in Nginx on an Ubuntu...
  • Hi,

    Thank for reply I did follow the instructions but it still didn't resolved the issue regarding the 404 error I think it might be the /etc/nginx/sites-available/default file could you please take a look above at the question and see if you can find errors there because I m not very sure if I had done things the way it would be be. and I will give a try to way you explained above regarding the sub domain and forum. Thank you

Greetings!

Sounds like the redirects needed for WordPress are not present in the Nginx configuration. This would explain why changing the permalinks resulted in those nasty 404s. Generally you can find the exact coding needed at this WordPress codex page.

  • Hi,

    Thanks for replying mate, I had a look into the WordPress codex page I m just not familiar with hardly any coding just know how to run a blog but sick of paying high amounts for hosting and want to setup a server for my WordPress sites in DO. So could you please help me with the /etc/nginx/sites-available/default file basically I need to know what code exactly needs to go where sorry to sound stupid just don't know anything about coding really. Thank you again.

    • No worries, the WordPress codex page is a bit confusing. This might actually help clear it up a bit more:

      http://nginxlibrary.com/wordpress-permalinks/

      Essentially you need the $args bit in your coding:

          try_files $uri $uri/ /index.php?$args;
      

      You can see how yours is a tad different:

                   try_files $uri $uri/ /index.php$is_args$args;
      

      It can be a bit different to go from Apache (a norm for a lot of hosts) to Nginx, but keep your chin up! You are learning new ways, and learning is always a good thing!

      Oh, do remember to reload/restart Nginx when you are done!

    • I'm looking at the config, but I don't understand something. Maybe @jcolyer could correct me as well. But isn't PHP actually disabled. As far as I know.
      This part:

      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
          #
          #location ~ \.php$ {
          #       include snippets/fastcgi-php.conf;
          #
          #       # With php7.0-cgi alone:
          #       fastcgi_pass 127.0.0.1:9000;
          #       # With php7.0-fpm:
          #       fastcgi_pass unix:/run/php/php7.0-fpm.sock;
          #}
      

      Should like this:

      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      
      location ~ \.php$ {
               include snippets/fastcgi-php.conf;
               fastcgi_pass unix:/run/php/php7.0-fpm.sock;
      }
      

      That will ensure PHP is actually working. Maybe will not fix your problem, but I just saw that.

      • @xMudrii You are completely right, I overlooked that. That is going to cause some problems. Basically you want this configuration:

        https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/

        But tweaked for your installation.

      • I did change that @xMudrii I m sure he didn't realise how stupid I m with coding after using Cpanel for such a long time. lol but I would love to learned @Jcolyer.

        What I need is someone to basically give me a copy of the perfect copy of the /etc/nginx/sites-available/default so I can copy it to the file and also compare it to the current one and see where I went wrong. sorry if I m asking for too much. Thank you

        here is the copy of my current ( .default )file.


        You should look at the following URL's in order to grasp a solid understanding of Nginx configuration files in order to fully unleash the power of Nginx. http://wiki.nginx.org/Pitfalls http://wiki.nginx.org/QuickStart http://wiki.nginx.org/Configuration Generally, you will want to move this file somewhere, and start with a clean file but keep this around for reference. Or just disable in sites-enabled. Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. Default server configuration

        server {
        listen 80 defaultserver;
        listen [::]:80 default
        server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;
        
        root /var/www/html;
        
        # Add index.php to the list if you are using PHP
        http {
        index index.html index.htm index.php index.nginx-debian.html;
        
        server_name _;
        
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            #try_files $uri $uri/ =404;
                     try_files $uri $uri/ /index.php?$args;        
        }
        
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #   include snippets/fastcgi-php.conf;
        #
        #   # With php7.0-cgi alone:
        #   fastcgi_pass 127.0.0.1:9000;
        #   # With php7.0-fpm:
        #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        #}
        
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #   deny all;
        #}
            location = /favicon.ico { log_not_found off; access_log off; }
            location = /robots.txt { log_not_found off; access_log off; allow all; }
            location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
        

        }


        • Basically you are going to want to copy and paste https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/ to get going here. Hard to be 100% sure though, so if the coding from that link doesn't work, let us know the output of netstat -plnt so we can help determine the port your php-fpm is using and the like.

          • I just copied the code from the link you provided but no luck I ll paste the code here .
            .........................................................................................................

            Upstream to abstract backend connection(s) for php

            upstream php {
            server unix:/tmp/php-cgi.socket;
            server 127.0.0.1:9000;
            }

            server {
            ## Your website name goes here.
            server_name domain.tld;
            ## Your only path reference.
            root /var/www/wordpress;
            ## This should be in your http block and if it is, it's not needed here.
            index index.php;

                location = /favicon.ico {
                        log_not_found off;
                        access_log off;
                }
            
                location = /robots.txt {
                        allow all;
                        log_not_found off;
                        access_log off;
                }
            
                location / {
                        # This is cool because no php is touched for static content.
                        # include the "?$args" part so non-default permalinks doesn't break when using query string
                        try_files $uri $uri/ /index.php?$args;
                }
            
                location ~ \.php$ {
                        #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                        include fastcgi.conf;
                        fastcgi_intercept_errors on;
                        fastcgi_pass php;
                }
            
                location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                        expires max;
                        log_not_found off;
                }
            

            }

            ...........................................................................................

        • Looking at your most recent reply, it seems like some of it was not quite modified to fit your site. Example:

          server_name domain.tld;

          You should replace domain.tld with your website's domain name.

          root /var/www/wordpress;

          The path there should be wherever your WordPress files are. If they are there, no worries.

          Once you got those changed over, restart the Nginx server and then try loading the site in your browser.

        • Joining @jcolyer once again. When you make sure everything is correct, and if it's still not a working, you should take a look at the logs.
          They could be very helpful and maybe it will give us some insight on where to look for the error.

          As I said in the first answer - /var/log/nginx/error.log will be probably the best point to start, so start there.

          • I got the error file just can't post it in the comment it's coming up as a spam when try to include it in the comment.

            here are some lines
            (myip) - - [11/Aug/2017:20:30:41 +0000] "POST /wp-admin/admin-ajax.php HTTP/1.1" 200 58 "http://46.101.36.158/wp-admin/plugins.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
            (myip) - - [11/Aug/2017:20:32:42 +0000] "POST /wp-admin/admin-ajax.php HTTP/1.1" 200 58 "http://46.101.36.158/wp-admin/plugins.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
            (myip) - - [11/Aug/2017:20:34:42 +0000] "POST /wp-admin/admin-ajax.php HTTP/1.1" 200 58 "http://46.101.36.158/wp-admin/plugins.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36

          • @mishfaq maybe you should just post here like last 10-15 lines at most. that will do the job and should allow you to do it

I installed Nginx Helper WP Plugin as well and I see some error related to the plugin as well So I m going to setup a fresh server and install everything again and will then update this page to get some help if still facing issue which I think I will. Thank you @jcolyer and @xMudrii, Thank you for your precious time.

So I reinstalled everything and setup a new server but still getting the same error below is the error.log and etc/nginx/sites-available/default. @xMudrii @jcolyer

error.log

2017/08/11 21:32:45 [notice] 2139#2139: signal process started
2017/08/11 22:03:34 [notice] 23682#23682: signal process started

etc/nginx/sites-available/default.

You should look at the following URL's in order to grasp a solid understanding of Nginx configuration files in order to fully unleash the power of Nginx. http://wiki.nginx.org/Pitfalls http://wiki.nginx.org/QuickStart http://wiki.nginx.org/Configuration Generally, you will want to move this file somewhere, and start with a clean file but keep this around for reference. Or just disable in sites-enabled. Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. Default server configuration

server {
listen 80 defaultserver;
listen [::]:80 default
server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name _;

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    #try_files $uri $uri/ =404;
             try_files $uri $uri/ /index.php?$args;
}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

     location ~ \.php$ {
     include snippets/fastcgi-php.conf;
     fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#   deny all;
#}

}

Virtual Host configuration for example.com You can move that to a different file under sites-available/ and symlink that to sites-enabled/ to enable it. server { listen 80; listen [::]:80; server_name example.com; root /var/www/example.com; index index.html; location / { try_files $uri $uri/ =404; }
Have another answer? Share your knowledge.