How to Install and Configure NGINX on Linux - Full Tutorial

How to Install and Configure NGINX on Linux - A Full Tutorial


In today's digitally driven world, deploying applications and web services has become easier and more efficient thanks to web servers. NGINX is one such powerful open-source web server software that also works as a reverse proxy, HTTP cache, and load balancer. This article will offer a full tutorial on how to install and configure NGINX on Linux.


## Step 1: Prerequisites


Before you begin the NGINX installation process, ensure you have:

1. A Linux system – this tutorial uses Ubuntu 18.04. Still, the procedures apply to all Debian-based distributions.

2. A user with sudo privileges.

3. An operating system up-to-date with the latest patches and updates. You can update your system using the commands:

```

sudo apt update

sudo apt upgrade

```


## Step 2: Installing NGINX On Linux


Follow the commands below to install NGINX:

```

sudo apt update

sudo apt install nginx

```


After installing, you can verify the NGINX installation using the following command:

```

nginx -v

```


You should see an output displaying the NGINX version installed on your system.


## Step 3: Manage The NGINX Service


After successful installation, manage the NGINX service using systemd commands.

To start NGINX service, use:

```

sudo systemctl start nginx

```


To stop the service, use:

```

sudo systemctl stop nginx

```


Automatically start NGINX at boot by using:

```

sudo systemctl enable nginx

```


## Step 4: Configuring NGINX


Configuration files for NGINX are located at /etc/nginx. The main configuration file is /etc/nginx/nginx.conf.

To set up your server block, navigate to /etc/nginx/sites-available and create a configuration file. A server block can look like this:

```

server {

listen 80;

server_name your_domain www.your_domain;

location / {

proxy_pass http://your_upstream;

}

}

```


Remember to replace 'your_domain' and 'your_upstream' with your domain name and application's IP address, respectively.

Once done, enable the server block by creating a symbolic link to the sites-enabled directory:

```

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

```


Test the configuration to ensure it’s free from any syntax errors, use:

```

sudo nginx -t

```


If the configuration is correct, restart the NGINX server:

```

sudo systemctl restart nginx

```


Congratulations! You’ve successfully installed and configured NGINX on your Linux system. This tutorial aimed to provide a straightforward guide on the installation and basic configuration of NGINX. It’s not an exhaustive guide but a starting point. You can explore more advanced configurations based on your needs.