Installing NGINX on Ubuntu 18.04

Select distribution:
Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $100 credit.
This credit will be applied to any valid services used during your first 60 days.

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.

Before You Begin

  1. Set up your Linode in the Getting Started and Securing your Server guides.

  2. If you want a custom domain name for your site, you can set this up using our DNS Manager guide.

    Note

    This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, visit our Users and Groups guide.

    All configuration files should be edited with elevated privileges. Remember to include sudo before running your text editor.

Install NGINX

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

sudo apt update
sudo apt install nginx

Add a Basic Site

  1. Create a new directory for your site. Replace example.com with your site’s domain name.

    sudo mkdir /var/www/example.com
    
  2. You can add your site’s files in your /var/www/example.com directory. Create an index file with a simple “Hello World” example. Using the text editor of your choice, create a new file, /var/www/example.com/index.html. Replace example.com with your website’s domain name or your Linode’s public IP address.

    File: /var/www/example.com/index.html
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    <!DOCTYPE html>
    <html>
        <head>
            <title>My Basic Website</title>
        </head>
        <body>
            <header>
                <h1>Hello World!</h1>
            </header>
        </body>
    </html>

Configure NGINX

NGINX site-specific configuration files are kept in /etc/nginx/sites-available and symlinked into /etc/nginx/sites-enabled/. Generally you will want to create a separate original file in the sites-available directory for each domain or subdomain you will be hosting, and then set up a symlink in the sites-enabled directory.

  1. Disable the default configuration file by removing the symlink in /etc/nginx/sites-enabled/:

    sudo unlink /etc/nginx/sites-enabled/default
    
  2. Create a configuration file for your site in the text editor of your choice. Replace example.com in the server_name directive with your site’s domain name or IP address:

    File: /etc/nginx/sites-available/example.com
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    server {
        listen  80;
        listen [::]:80;
        server_name example.com;
    
        root /var/www/example.com;
        index index.html;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
  3. Set up a new symlink to the /etc/nginx/sites-enabled/ directory to enable your configuration:

    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

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 your simple page displayed.

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.

This page was originally published on


Your Feedback Is Important

Let us know if this guide made it easy to get the answer you needed.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.