Watch all our Tutorials and Training Videos for Free on our Youtube Channel, Get Online Web Tools for Free on swebtools.com

Search Suggest

How to Install and Configure Nginx on Linux

install nginx on ubuntu, configure nginx on ubuntu, install nginx linux, nginx installation on linux, configure nginx on linux, linux install nginx
How to Install and Configure Nginx on Linux

Nginx, pronounced as "engine-x," is a powerful open-source web server and reverse proxy software that is gaining popularity among web developers and system administrators. It is known for its high performance, stability, and low memory usage.

In this article, we will walk you through the process of installing and configuring Nginx on Linux.

Step 1: Update your system

Before installing Nginx, it is essential to ensure that your Linux system is up-to-date. So make sure to update the system packages and upgrade it as given below.

sudo apt update && sudo apt upgrade

This command will update the package list and install the available updates for your system.


Step 2: Install Nginx

Once your system is updated, you can proceed to install Nginx. In most Linux distributions, Nginx is available in the default package repositories, and you can install it using the package manager.

For example, on Ubuntu or Debian, run the following command:

sudo apt install nginx

If you are using CentOS, Fedora, or Red Hat, you can install Nginx using the following command:

sudo yum install nginx


Step 3: Start Nginx and enable it on boot

After installing Nginx, you can start it using the following command:

sudo systemctl start nginx

To ensure that Nginx starts automatically when you boot your system, run the following command:

sudo systemctl enable nginx


Step 4: Configure Firewall

By default, Nginx listens on port 80 and 443. You need to open these ports on your firewall to allow incoming connections.

use 'ufw' command as below.

sudo ufw allow 'Nginx Full'

This command will open both port 80 and 443.


Step 5: Verify Nginx Installation

To verify that Nginx is running and serving web pages, open your web browser, and navigate to your server's IP address.

If you see the Nginx welcome page, it means that Nginx is successfully installed and configured.


Step 6: Configure Virtual Hosts

Nginx supports virtual hosts, which allows you to host multiple websites on the same server. To configure a virtual host, create a new server block file in the /etc/nginx/sites-available/ directory.

For example, let's create a virtual host for a website named example.com:

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

Then, add the following configuration:

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


Save the file and exit the editor. Then, create a symbolic link to the sites-enabled directory using the following command:

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

Finally, reload Nginx to apply the changes:

sudo systemctl reload nginx


Step 7: Install SSL Certificate

If you want to secure your website using SSL, you need to install an SSL certificate. You can either purchase an SSL certificate or use a free certificate from Let's Encrypt.

To install Let's Encrypt certificate, you need to install Certbot, which is an automated tool for obtaining and renewing SSL certificates.

If you are not sure, how to install CertBot and How to Obtain SSL certificates for any domains, refer this link How to Install CertBot on Ubuntu?

That's it for this post. Hope you have got some idea on How to Install and Configure Nginx on Linux Easily.

Keep practicing and have fun. Leave your comments if any.

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.