Installing and Configuring Nginx from Source on Linux (original) (raw)

Last Updated : 7 May, 2026

Nginx is a free and open-source web server and reverse proxy designed to handle a large number of simultaneous connections efficiently. It is widely used for web serving, load balancing and media streaming. Known for high performance and scalability, Nginx is commonly deployed in production environments to manage traffic and improve reliability.

Install Nginx from Source

Step 1: Download Nginx Source Archive

Download the Nginx archive from this link and save the archive file on your desktop.

Nginx Download page

**Command:

wget http://nginx.org/download/nginx-1.21.1.tar.gz

**Output:

Downloading the Nginx server

After downloading the archive, we need to navigate the folder where we have downloaded that archive and have to extract the archive using any archive utility. You can run the following command to extract the Nginx archive file.

tar -xf nginx-1.21.1.tar.gz

Nginx folder

Step 3: Configure and Build Nginx

To begin the installation of Nginx, navigate to the extracted folder and open the terminal here, then run the following command.

**Command:

cd ~/Desktop/nginx-1.21.1

**Command:

./configure

Here below is a summary of the configuration file:

Configuration summary

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

**Command:

make

**Command:

sudo make install

This command will install Nginx in the /usr/local/nginx directory.

Step 4: Verify Installation

Confirm the installation and check the installed version of Nginx, navigate to /usr/local/nginx using the cd command (change directory)

**Command:

cd /usr/local/nginx/sbin

**Command:

./nginx -v

**Output:

Successfully installed Nginx

Starting the Nginx server

Navigate to the default location where Nginx is installed

**Command:

cd /usr/local/nginx/sbin

**Command:

sudo ./nginx

**Output:

Nginx start (Welcome page)

Change the default Nginx Listening port

By default, the Nginx is configured to listen on port 80. If you want to change the default Nginx listening port, you can do that by reconfiguring the nginx.conf file located under /usr/local/nginx/conf.

Step 1: Open Configuration File

Open the nginx.conf file

**Command:

sudo nano /usr/local/nginx/conf/nginx.conf

Step 2: Modify Port

After opening, the nginx.conf file should look like this:

configuring listen port

**Change it to your desired port. For example:

listen 5555;

Step 3: Save the File

Step 4: Test and Restart Nginx

Before restarting, it’s a good practice to test the configuration:

sudo nginx -t

sudo systemctl restart nginx

Step 5: Verify the Change

Open your browser and visit:

http://localhost:5555

custom listen port

Stopping the Nginx server

To stop the Nginx server, we just need to add the flag -s to stop the Nginx.

**Command:

sudo ./nginx -s stop

This will stop the Nginx server, you can refresh the localhost page and see.

**Output:

Nginx stop

Uninstalling the Nginx server

To uninstall Nginx, run the command in the terminal with superuser permissions

**Command:

sudo rm -f -R /usr/local/nginx && rm -f /usr/local/sbin/nginx

**Note: Ensure correct path before deletion as rm -f -R permanently removes files