WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Install and Configure Nginx on Ubuntu

Edited 4 days ago by ExtremeHow Editorial Team

NginxUbuntuServerLinuxWeb ServerInstallationConfigurationOperating SystemsSystemSetup

How to Install and Configure Nginx on Ubuntu

This content is available in 7 different language

Welcome to this guide on how to install and configure Nginx on Ubuntu Server. In this detailed article, we will cover each step in detail, explaining the process from installation to configuration, including various modules and settings that can be adjusted according to your needs. Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption, making it a popular choice for web servers. Let's dive into the steps to run Nginx on your Ubuntu system.

Understanding Nginx

Nginx (pronounced "engine x") is a high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. It has been developed to handle many concurrent connections, which means it is highly efficient at serving static content and providing load balancing features. Nginx is open-source software, which means it is free to use, modify, and distribute.

Prerequisites

Before you can install Nginx, you must have a server running Ubuntu. This guide assumes the following:

Make sure your server is updated with the latest software packages, for this do the following: sudo apt update && sudo apt upgrade.

Installing Nginx

To install Nginx, your package manager must be up to date. You will use apt, which is the default package manager for Ubuntu. Follow these steps:

sudo apt update
sudo apt install nginx

The first command updates the package list. The second command installs Nginx. After installation, Nginx starts automatically. To verify that Nginx is running, you can use the command: systemctl status nginx. If the service is active and running, you should see output indicating so.

Adjusting the firewall

By default, Nginx registers itself with ufw, which is a firewall configuration tool. Using ufw one can manage which HTTP and HTTPS ports are open. Run:

sudo ufw app list

You should see the Nginx profile listed there. Grant Nginx Full permission as follows:

sudo ufw allow 'Nginx Full'

Then, verify the change by checking the firewall status: sudo ufw status to make sure traffic is allowed on the required ports.

Checking the Nginx server

Now that Nginx is running and the firewall is adjusted, you can view the web server's default page. Open a web browser and navigate to your server's public IP address. If Nginx is set up correctly, you should see the "Welcome to Nginx!" page. To find your server's public IP, you can use:

curl -4 icanhazip.com

Understanding Nginx directory structure

It is important that you are familiar with the directory structure where Nginx is installed, primarily in /etc/nginx. The main directories and files include:

Configuration changes should generally be made in nginx.conf or by creating new files in sites-available.

Basic Nginx configuration

nginx.conf file is the main configuration file for Nginx. Simple tasks can involve editing it directly, but for specific changes related to hosted sites, use server block files. Let's create a new server block file for the domain named example.com. Inside /etc/nginx/sites-available/:

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

Add the following configuration:

server {
    listen 80;
    server_name example.com www.example.com;
    location / {
        try_files $uri $uri/ =404;
    }
}

To enable the file, create a symbolic link to sites-enabled directory:

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

To check if you have it configured correctly:

sudo nginx -t

If successful, reload Nginx:

sudo systemctl reload nginx

This example only serves static files from the directory, assuming they are located at /var/www/html under the default Nginx root.

Setting up a server block

Server blocks allow you to host multiple domains on a single server. Each domain's configuration file must be in sites-available and the symlink in sites-enabled. Example of a server block:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html index.htm index.nginx-debian.html;
    location / {
        try_files $uri $uri/ =404;
    }
}

Create directory structure:

sudo mkdir -p /var/www/example.com/html

Specify permissions and create a sample HTML page:

sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com
echo '<h1>Hello World!</h1>' > /var/www/example.com/html/index.html

Remember to symlink and test the configuration, then reload Nginx.

Implementing SSL with Let's Encrypt

To secure traffic to and from your server, it's important to implement SSL/TLS. Let's Encrypt offers free SSL certificates. Start by installing Certbot, Let's Encrypt's client:

sudo apt install certbot python3-certbot-nginx

Certbot can automatically configure SSL for Nginx; run:

sudo certbot --nginx -d example.com -d www.example.com

During the process, it is necessary to provide an email address and agree to the terms of service. Certbot will handle the modifications to your Nginx configuration to serve content over HTTPS.

Automate SSL renewals

Let's Encrypt certificates expire every 90 days. Set up a cron job to automatically renew certificates. Open the crontab:

sudo crontab -e

Add:

0 3 * * * /usr/bin/certbot renew --quiet

This will run the renewal command every day at 3 AM.

Performance tuning

For sites with high traffic, performance tuning is important. Some tuning strategies:

Consider using tools like New Relic or Datadog to monitor Nginx performance.

Conclusion

Congratulations, you have installed and configured Nginx on Ubuntu! You learned how to serve multiple domains, secure your site with SSL, and optimize the configuration for better performance. Since Nginx's options and modules are so rich, exploring its documentation further can give you many advanced and interesting capabilities. Keep experimenting, and you'll soon be managing a highly optimized web server.

If you find anything wrong with the article content, you can


Comments