How to Install and Configure Nginx on Ubuntu 20.04?


Nginx is a popular open-source web server software that can be used as a reverse proxy, load balancer, HTTP cache, and more. It’s known for its speed and scalability, and is widely used to serve web content for high-traffic websites.

If you’re running an Ubuntu 20.04 server and want to use Nginx as your web server, this guide will walk you through installation and basic configuration process.

Step 1: Update system

Before we begin, it’s a good idea to update system to ensure that all packages are up to date. You can do this by running following command −

sudo apt update && sudo apt upgrade -y

This will update package list and install any available updates.

Step 2: Install Nginx

Now that system is up to date, you can proceed with installing Nginx. You can do this by running following command −

sudo apt install nginx -y

This will install latest version of Nginx on your Ubuntu 20.04 server.

Step 3: Configure Nginx

Once Nginx is installed, you can begin configuring it to serve your web content. By default, Nginx will serve content located in /var/www/html directory. You can test that Nginx is working correctly by visiting your server’s IP address in a web browser.

To configure Nginx to serve your own content, you will need to create a new configuration file in /etc/nginx/sites-available directory. You can do this by creating a new file with your preferred text editor, such as nano or vim.

sudo nano /etc/nginx/sites-available/example.com

In this file, you can define your server block, which will contain configuration for your website. Here’s an example of a basic server block −

server {
   listen 80;
   server_name example.com;
   root /var/www/example.com;
   index index.html;
}

This configuration sets server to listen on port 80, and serves content from /var/www/example.com directory. You can replace example.com with your own domain name, and /var/www/example.com with location of your web content.

Once you’ve created your configuration file, you’ll need to create a symbolic link to enable it. You can do this by running following command −

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

Finally, you can test your configuration and restart Nginx by running following commands −

sudo nginx -t

sudo systemctl restart nginx

If there are no errors, your website should now be live and accessible via your domain name.

Step 4: Configure SSL/TLS

If you’re serving sensitive information or accepting user data, it’s important to secure your website with SSL/TLS encryption. You can do this by obtaining an SSL/TLS certificate and configuring Nginx to use it.

One popular way to obtain an SSL/TLS certificate is through Let’s Encrypt, a free and open certificate authority. You can install Let’s Encrypt client on your server by running following commands −

sudo apt install certbot python3-certbot-nginx -y

Once Let’s Encrypt client is installed, you can obtain a certificate for your domain by running following command −

sudo certbot --nginx -d example.com

This will run Let’s Encrypt client and configure Nginx to use newly obtained SSL/TLS certificate.

Here are a few more advanced configuration options that you may want to consider when working with Nginx on Ubuntu 20.04 −

Customizing Server Blocks

In previous section, we created a simple server block that serves content from a specific directory. However, you may have multiple domains or subdomains that need to be served from same server. To do this, you can create additional server blocks that define configuration for each domain or subdomain.

For example, suppose you have two domains, example.com and example.org. You can create two server blocks, one for each domain, and place them in /etc/nginx/sites-available directory. Then, you can enable each server block by creating symbolic links in /etc/nginx/sites-enabled directory.

sudo nano /etc/nginx/sites-available/example.com

server {
   listen 80;
   server_name example.com;
   root /var/www/example.com;
   index index.html;
}

sudo nano /etc/nginx/sites-available/example.org

server {
   listen 80;
   server_name example.org;
   root /var/www/example.org;
   index index.html;
}

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

Enabling Caching

Nginx can also be used as a caching server to improve performance of your website. By caching frequently accessed content, Nginx can reduce load on your web server and improve response times for your visitors.

To enable caching, you can add following configuration to your server block −

server {
   listen 80;
   server_name example.com;
   root /var/www/example.com;
   index index.html;

   # Enable caching
   location / {
      proxy_cache_valid 200 60m;
      proxy_cache_bypass $http_pragma;
      proxy_cache_revalidate on;
      proxy_cache_min_uses 3;
      proxy_cache_use_stale error timeout invalid_header http_502;
      proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;
      proxy_cache_key "$scheme$request_method$host$request_uri";
      proxy_cache_lock on;
      proxy_cache_methods GET HEAD POST;
      add_header X-Cache-Status $upstream_cache_status;
   }
}

This configuration enables caching for all requests and sets cache expiration time to 60 minutes. You can adjust cache settings to meet needs of your website.

Load Balancing

If you have multiple web servers, you can use Nginx as a load balancer to distribute incoming traffic across servers. This can improve scalability and reliability of your website.

To enable load balancing, you can add following configuration to your server block −

server {
   listen 80;
   server_name example.com;
   root /var/www/example.com;
   index index.html;

   # Enable load balancing
   upstream backend {
      server backend1.example.com;
      server backend2.example.com;
   }

   location / {
      proxy_pass http://backend;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}

This configuration defines an upstream group named "backend" that includes two servers, backend1.example.com and backend2.example.com. location block then proxies requests to backend group, distributing traffic across two servers.

These are just a few examples of advanced configuration options that are available with Nginx. By exploring official documentation and community forums, you can discover many more ways to customize and optimize Nginx for your specific needs.

Firewall Configuration

As with any server, it’s important to ensure that your Ubuntu 20.04 server is secured with a firewall. By default, Ubuntu 20.04 comes with UFW (Uncomplicated Firewall) preinstalled.

To allow traffic to Nginx, you can add a rule to UFW to allow HTTP traffic on port 80 −

sudo ufw allow 80/tcp

If you’re using SSL/TLS encryption, you can also add a rule to allow HTTPS traffic on port 443 −

sudo ufw allow 443/tcp

With these rules in place, your Ubuntu 20.04 server should be properly secured with UFW.

Monitoring and Logging

It’s important to monitor performance and availability of your Nginx server to ensure that it’s running smoothly. You can use various tools and services to monitor your server, including −

  • Nginx Amplify − A monitoring and management tool specifically designed for Nginx. It provides real-time performance metrics and alerts for your Nginx server.

  • Netdata − A real-time monitoring and troubleshooting tool that provides detailed metrics on various aspects of your server, including Nginx.

  • Prometheus − A popular open-source monitoring solution that can be used to monitor Nginx, as well as other applications and systems.

In addition to monitoring, it’s important to keep track of access and error logs generated by Nginx. These logs can provide valuable insights into behavior of your website and can help you troubleshoot issues.

By default, Nginx logs are stored in /var/log/nginx directory. You can view access log by running following command −

sudo tail -f /var/log/nginx/access.log

You can view error log by running following command −

sudo tail -f /var/log/nginx/error.log

Conclusion

In this guide, we’ve walked through process of installing and configuring Nginx on an Ubuntu 20.04 server. We started by updating system and installing Nginx. Then, we configured Nginx to serve our web content and tested configuration. Finally, we secured our website with SSL/TLS encryption using Let’s Encrypt.

Keep in mind that this is just a basic configuration, and there are many additional options and modules that you can use to customize Nginx for your specific needs. You can learn more about Nginx by reading official documentation or by joining Nginx community forums.

Overall, Nginx is a powerful and versatile web server software that can handle a wide range of web applications and traffic loads. By following these steps, you can get started with Nginx on your Ubuntu 20.04 server and begin serving your web content to world.

Updated on: 12-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements