Install NGINX on Ubuntu 18.04

Updated by Linode Written by Linode

Contribute on GitHub

Report an Issue | View File | Edit File

Install NGINX on Ubuntu 18

What is NGINX?

NGINX is an open source web server with powerful load balancing, reverse proxy, and caching features. It was initially designed to solve scaling and concurrency problems with existing web servers. Its event-based, asynchronous architecture has made it one of the most popular and best-performing web servers available.

Install NGINX

Currently, the best way to install NGINX on Ubuntu 18.04 is to use the version included in Ubuntu’s repositories:

sudo apt update
sudo apt install nginx

Configure NGINX

Add Basic Site

NGINX site-specific configuration files are kept in /etc/nginx/conf.d/. Generally you will want a separate file in this directory for each domain or subdomain you will be hosting.

  1. Copy the default configuration file. Replace example.com with your website’s domain name or your Linode’s public IP address.

    sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/example.com.conf
    
  2. Disable the default configuration file by adding .disabled to the filename:

    sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.disabled
    
  3. Open your site’s configuration file in a text editor. Replace example.com in the server_name directive with your site’s domain name or IP address. If you already have content ready to serve (such as a WordPress installation or static files) replace the path in the root directive with the path to your site’s content:

    /etc/nginx/conf.d/example.com.conf
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    server {
        listen       80;
        server_name  example.com;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
        #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;
        }
    }

Test NGINX

  1. Test your configuration for errors:

    sudo nginx -t
    
  2. Reload the configuration:

    sudo nginx -s reload
    
  3. Navigate to your Linode’s domain name or IP address in a browser. You should see the NGINX default page displayed (or your own content, if you specified the path in the previous section).

Advanced Configuration

For more advanced configuration options, including security and performance optimizations and TLS setup, see our four-part series on NGINX:

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

Join our Community

Find answers, ask questions, and help others.

comments powered by Disqus

This guide is published under a CC BY-ND 4.0 license.