Tutorial: Installing NGINX on Ubuntu

Nginx is a popular open-source web server known for its high-performance and scalability. It’s commonly used to serve static and dynamic content and is a great alternative to traditional web servers like Apache. In this tutorial, we’ll show you how to install Nginx on Ubuntu and get started with the basics.

Step 1: Update Packages

Before we start, we should update the package list and upgrade any existing packages on the server.

sudo apt update
sudo apt upgrade

Step 2: Install Nginx

Nginx is available in the default Ubuntu repositories, so we can install it using apt.

sudo apt install nginx

This command will install Nginx and all its dependencies.

Step 3: Adjust Firewall

If you have UFW (Uncomplicated Firewall) enabled, you will need to allow traffic to the Nginx server. By default, Nginx listens on port 80 and 443. We’ll allow traffic to those ports by running the following commands:

sudo ufw allow 'Nginx Full'
sudo ufw status

The status command will show you the current status of UFW and the rules that are currently enabled.

Step 4: Verify Installation

After the installation is complete, you can verify that Nginx is running by accessing your server’s IP address or domain name in a web browser.

If you’re using a firewall, make sure to allow traffic to port 80 and 443.

http://your_server_ip

You should see the Nginx default landing page.

Step 5: Manage Nginx Service

Nginx is installed as a system service, so you can start, stop, and restart it using the systemctl command.

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

You can also check the status of the Nginx service to make sure it’s running without errors.

sudo systemctl status nginx

Step 6: Configure Nginx

By default, Nginx serves files from the /var/www/html directory. You can edit the default Nginx configuration file to change the root directory or add additional server blocks.

The default configuration file is located at /etc/nginx/sites-available/default.

sudo nano /etc/nginx/sites-available/default

Make your changes to the configuration file, save it, and then test the configuration for syntax errors.

sudo nginx -t

If there are no syntax errors, restart the Nginx service for the changes to take effect.

sudo systemctl restart nginx

Step 7. Create New Server Profile (Optional)

Create a new file for the server profile in the /etc/nginx/sites-available/ directory. For example, you could create a file called example.com.conf for a server profile handling traffic for the domain example.com:

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

Add the configuration details for the new server profile to the file. For example:

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

    location / {
        try_files $uri $uri/ =404;
    }
}

This example defines a simple server profile that listens for traffic on port 80 and handles requests for the example.com domain. Requests are served from the /var/www/example.com directory, and the default file served is index.html.

Save and exit the file.

Create a symbolic link to the new configuration file in the /etc/nginx/sites-enabled/ directory:

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

This enables the server profile by creating a symbolic link from the sites-available directory to the sites-enabled directory.

Test the new configuration for syntax errors:

sudo nginx -t

If there are any errors, correct them in the configuration file and test again.

Restart Nginx to apply the new configuration:

sudo systemctl restart nginx

That’s it! Your new server profile should now be active and serving traffic.

You have now successfully installed Nginx on Ubuntu. Nginx is a powerful and efficient web server that can serve static and dynamic content. You can now configure Nginx for your specific needs, such as hosting multiple sites or adding SSL/TLS encryption with Let’s Encrypt.

Leave a Reply

Scroll to top