Vanker
By:
Vanker

Change default web root for nginx on one-click install lemp 14.04

May 10, 2017 2.7k views
LEMP PHP Nginx Ubuntu

https://www.digitalocean.com/community/tutorials/how-to-install-laravel-with-an-nginx-web-server-on-ubuntu-14-04

All of these steps were taken, no effect at all.

accessing the droplet via http all it does is show the default web page "ssh to configure your lemp installation"

Even when accessing via putty it says "default web root is" /var/www/html even after all the changes.

I'm just trying to deploy my Laravel app to the vps and the setting up part is taking longer than the coding itself.

7 Answers
jtittle1 May 10, 2017
Accepted Answer

@Vanker

The way the repository packages are setup, files in:

/etc/nginx/sites-available

are symlinked to:

/etc/nginx/sites-enabled

i.e

/etc/nginx/sites-available/default => /etc/nginx/sites-enabled/default

The NGINX configuration file located at /etc/nginx/nginx.conf then loads the files from:

/etc/nginx/sites-enabled

You can confirm this by looking at the bottom of the http block for a line that shows:

include /etc/nginx/sites-enabled/*

It may also look like:

include sites-enabled/*

Changes should reflect regardless of whether you modify the link or the actual file.

...

Changes to the web root that NGINX uses are reflected by modifying root in the server block. If it's current using /var/www/html, i.e.

root /var/www/html;

You'd simply change that line and reload/restart NGINX.

...

That being said, you may find it more beneficial to clean up the server block and remove the clutter so that you're not sifting through commented lines that have no bearing on your configuration.

The server block you posted would look like the following, once cleaned up:

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

    server_name domain.com www.domain.com;

    root /var/www/html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    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 fastcgi_params;
    }
}

I removed try_files $uri /index.php =404; from the location ~ \.php$ block as it really shouldn't be needed.

I also changed $query_string to $args as it's more commonly used.

That said, you may need the index line if it's not defined in nginx.conf, so we can add that back line so:

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

    server_name domain.com www.domain.com;

    root /var/www/html;

    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    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 fastcgi_params;
    }
}

...

That being said, the one-click images are meant to be a starting point. You still need to know how to manage a server, setup the server, etc. Server/System administration, much like coding, isn't a one-time deal that requires no further setup, configuration, or tweaking/tuning -- it's on-going.

  • Thanks for the clean file and for your explanations, I have a better idea of what's going on now.

    • @Vanker

      No problem, happy to help :-).

      There's quite a bit that repository packages leave out and in turn, guides do the same -- mainly because we'd see 5-10 page guides on configuration if they covered every single detail about nginx.conf or server block configuration (they'd each be a mini-book of their own).

      There's also plenty of ways to reduce redundancies in configuration -- for example, there's no need to include that index line in every server block. Remove it from server blocks and place it in nginx.conf, reload/restart, and don't worry about it. This sets it globally.

      If you need to work with a variety of indexes, you can expand the line:

      index index.php index.html index.html index.js, index.shtml;
      

      etc. -- I'd only include the ones you know you'll be using, but that's an example of what you could do.

      If you have any other questions / issues, feel free to reply -- I'll be more than happy to help!

      • Yeah, turns out that the server was not using the Default file it was using one called digitalocean to manage, so no matter what changes i did to the default file (or even create a new one for that matter) it would not work.

        Now i'm facing another issue, I deleted the digitalocean file and now its not handling requests, how do i go about selecting the default file or the other that I created to handle said requests?

        • @Vanker

          Open up /etc/nginx/nginx.conf and find the include line at the bottom. That'll tell you where NGINX is pulling files from. In most all cases, for repositories, that'll be /etc/nginx/sites-enabled.

          You can change that to any directory. Personally, I don't like having to bother with symlinks, so I change sites-enabled to sites-available and then stick all configuration files in that directory.

          As a general note, you're not confined to using either of those directories. You could create a new directory, /etc/nginx/sites and stick your configuration files in there and change the include line to use that directory instead.

          ...

          I mainly suggest using sites-available or a new directory and changing the include line so you don't have to bother with symlinks. They're a pain and really offer no real benefit -- even on servers with hundreds of them.

          ..

          At the end of the day, NGINX will load files from wherever you tell it to :-). As long as the file exists in the directory you tell NGINX to use, it'll load that file.

          We use * as a wild card, meaning load all files in a given directory.

          • I see, I changed to the sites-available as you suggested still browser throws me
            "This page isn't working, can't handle requests"

            It seems that the server isn't using the files that i want (or using any for that matter) :/.

What's the output of cat /etc/nginx/sites-available/default?

  • You may add here your server { } statements for each of your virtual hosts to this 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.

    server {
    listen 80 defaultserver;
    listen [::]:80 default
    server ipv6only=on;

        root /var/www/mb2/mb2webrcon/public;
        index index.php index.htm;
    
        # Make site accessible from http://localhost/
        server_name {droplet ip};
    
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
        location ~ \.php$ {
        try_files $uri /index.php =404;
        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 fastcgi_params;
    }
        # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
        #location /RequestDenied {
        #       proxy_pass http://127.0.0.1:8080;
        #}
    
        #error_page 404 /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        #error_page 500 502 503 504 /50x.html;
        #location = /50x.html {
        #       root /usr/share/nginx/html;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
        #       fastcgi_index index.php;
        #       include fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
    

    }

    another virtual host using mix of IP-, name-, and port-based configuration server { listen 8000; listen somename:8080; server_name somename alias another.alias; root html; index index.html index.htm; location / { try_files $uri $uri/ =404; } HTTPS server server { listen 443; server_name localhost; root html; index index.html index.htm; ssl on; ssl_certificate cert.pem; sslcertificatekey cert.key; sslsessiontimeout 5m; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES"; sslpreferserver_ciphers on; location / { try_files $uri $uri/ =404; }

@Vanker
Okay, which sites do you have defined in /etc/nginx/sites-enabled. You need to look through every configuration and make sure none of them are overlapping.
If you don't know what to do, then just post each server block, but please use the code-button in this editor.

  • I do not have anything in sites enabled but a file called digitalocean.

    The tutorial didn't mention that.

    • The tutorial actually says:
      Because we modified the default server block file, which is already enabled, we simply need to restart Nginx for our configuration changes to be picked up

      Have a look at this tutorial (specially step 3 and 4):
      https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

      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...
      • So by default it was supposed to work after I made these changes.

        Also, your link did not worked as well, it says to create a new "block-file" in the sites-enabled but if the default was already enabled and if i correctly set that up isn't supposed to be working anyway?

        One other thing, ~~/sites-enabled/"example.com" (this isn't supposed to be the domain right?) just a name for you to better find what you are looking for

@Vanker

If you only use one site, then just edit whatever file is located in /etc/nginx/sites-enabled

If you want to run multiple sites on the one-click installation, then I would recommend that you remove all files in /etc/nginx/sites-enabled/* and /etc/nginx/sites-available/*, and then create a new configurations by following the tutorial.

In the tutorial, everything with the color red are something you should modify.

@Vanker

Please post the contents of /etc/nginx/nginx.conf and also, check the directory to make sure that the file containing your server block data actually exists at the location you are using.

So if nginx.conf is set to:

include /etc/nginx/sites-available/*;

Then your server blocks need to be in /etc/nginx/sites-available, i.e.

/etc/nginx/sites-available/default
/etc/nginx/sites-available/site01
/etc/nginx/sites-available/site02
/etc/nginx/sites-available/site03

etc.

  • [deleted]
  • nginx.conf per request:
    And yes the file is the default file that I posted earlier on this question and that file is located in /etc/nginx/sites-available (did not yet changed to your cleaner version)

    user www-data;
    worker_processes 4;
    pid /run/nginx.pid;
    
    events {
            worker_connections 768;
            # multi_accept on;
    }
    
    http {
    
            ##
            # Basic Settings
            ##
    
            sendfile on;
            tcp_nopush on;
            tcp_nodelay on;
            keepalive_timeout 65;
            types_hash_max_size 2048;
            # server_tokens off;
    
            # server_names_hash_bucket_size 64;
            # server_name_in_redirect off;
    
            include /etc/nginx/mime.types;
            default_type application/octet-stream;
    
            ##
            # Logging Settings
            ##
    
            access_log /var/log/nginx/access.log;
            error_log /var/log/nginx/error.log;
    
            ##
            # Gzip Settings
            ##
    
            gzip on;
            gzip_disable "msie6";
    
            # gzip_vary on;
            # gzip_proxied any;
            # gzip_comp_level 6;
            # gzip_buffers 16 8k;
            # gzip_http_version 1.1;
            # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    
            ##
            # nginx-naxsi config
            ##
            # Uncomment it if you installed nginx-naxsi
            ##
    
            #include /etc/nginx/naxsi_core.rules;
    
            ##
            # nginx-passenger config
            ##
            # Uncomment it if you installed nginx-passenger
            ##
    
            #passenger_root /usr;
            #passenger_ruby /usr/bin/ruby;
    
            ##
            # Virtual Host Configs
            ##
    
            include /etc/nginx/conf.d/*.conf;
            include /etc/nginx/sites-available/*;
    }
    
    
    #mail {
    #       # See sample authentication script at:
    #       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
    #
    #       # auth_http localhost/auth.php;
    #       # pop3_capabilities "TOP" "USER";
    #       # imap_capabilities "IMAP4rev1" "UIDPLUS";
    #
    #       server {
    #               listen     localhost:110;
    #               protocol   pop3;
    #               proxy      on;
    #       }
    #
    #       server {
    #               listen     localhost:143;
    #               protocol   imap;
    #               proxy      on;
    #       }
    #}
    
    
    • @Vanker

      The nginx.conf file appears to be fine as-is. Did you restart NGINX after making changes? Anytime you make any change, NGINX must be reloaded or restarted.

      With this line:

      include /etc/nginx/sites-available/*;
      

      Everything in that directory is being included when you restart, so next thing I'd do is check the error logs and see if something more is showing up.

      tail -50 /var/log/nginx/error.log
      
      • There is a laravel error, saying that it can't find the files, that's on me gonna have a lookse.

        thanks for your help.

Have another answer? Share your knowledge.