Part 1: Installing Nginx
Tags: nginx, devops
Published: 2024-02-05 17:02:18

# Downloading and Installing Nginx from Source

Nginx is a powerful and efficient web server thats widely used for hosting websites and applications. In this blog, well walk through the steps to download and install Nginx from source on a Linux system. This method is particularly useful when you need a specific version of Nginx or want to customize the build.

## Prerequisites
Before beginning, ensure you have a Linux system with internet access and root privileges. Then update the package list and install net-tools 
```
sudo apt-get update
sudo apt-get install net-tools
```

## Step 1: Downloading the Nginx Package
First, download the desired version of Nginx. As of this writing, lets use version 1.22.1. You can download it using the `wget` command:

```
wget https://nginx.org/download/nginx-1.22.1.tar.gz
```


This command fetches the tar.gz file containing the Nginx source code.

## Step 2: Extracting the File
Once the download is complete, extract the file and navigate to the extracted directory:

```
tar -zxvf nginx-1.22.1.tar.gz
cd nginx-1.22.1
```

## Step 3: Installing Build Essentials
Before compiling Nginx, you need to install `build-essential`, a package that contains references to numerous tools and utilities required for building software:

```
apt-get install build-essential
```

Then, initiate the configuration process:

```
./configure
```

## Step 4: Installing Dependencies
Nginx has several dependencies that need to be installed for it to function correctly. These include libraries for regular expression processing, encryption, and compression. Install these using the following command:

```
apt-get install libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
```

## Step 5: Final Configuration and Installation
After installing the dependencies, run the configure script again. This time, it should complete without any errors:

```
./configure
```

## Step 6: Additional Configuration for Nginx

This command configures Nginx with specific paths and options:
```
./configure --sbin-path=/usr/bin/nginx \
            --conf-path=/etc/nginx/nginx.conf \
            --error-log-path=/var/log/nginx/error.log \
            --http-log-path=/var/log/nginx/access.log \
            --with-pcre \
            --pid-path=/var/run/nginx.pid \
            --with-http_ssl_module
```

Explanation of the configuration options:
--sbin-path: Specifies the path where the nginx executable will be located.
--conf-path: Sets the path for the nginx configuration file.
--error-log-path: Designates the location for the nginx error log.
--http-log-path: Specifies the path for the nginx access log.
--with-pcre: Enables usage of Perl Compatible Regular Expressions for location matching.
--pid-path: Sets the path of the nginx PID file.
--with-http_ssl_module: This option enables the SSL module, which is essential for handling HTTPS traffic.

This configuration provides a structured approach to organizing the Nginx installation.
It helps in managing logs, configuration, and executable files in a standard and accessible manner.

## Step 7: Compiling and Installing Nginx
```
make install
```
This sequence compiles the Nginx source code and then installs it on your system.

## Step 8: Verifying the Installation
Listing the contents of the Nginx configuration directory
```
ls -l /etc/nginx
```
This command will show the files in the '/etc/nginx' directory, ensuring that Nginx is installed correctly.

## Step 9: Checking the Nginx Version and Configuration
```
nginx -V
```

## Step 10: Running Nginx

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzrd7uaxxjz9hpmtmor4.png)

To stop Nginx run
```
sudo /usr/bin/nginx -s stop
```