Install Nginx Web Server on Ubuntu

Web Server

By Chandrashekhar Fakirpure

Updated on Jan 27, 2024

In this article, we'll explain how to install Nginx Web Server on Ubuntu. 

Nginx (pronounced "engine x") is a high-performance and open-source web server software that also functions as a reverse proxy server, load balancer, and HTTP cache. Nginx is known for its exceptional performance and efficiency. 

It is designed to handle a large number of concurrent connections. Nginx is a popular choice for many web applications, including e-commerce sites, content delivery networks (CDNs), media streaming services, and much more.

Install Nginx Web Server on Ubuntu

Let's start with the installation process. 

There are two ways to install Nginx Web server. First way is simple, we just need to execute one command and it gets installed. Second way is installing from source. It is bit complex process and need to have Linux command knowledge. 

Let's check the first way of the installation.

Keep the server up-to-date

First, we need to update the server using following command:

sudo apt update

Install Nginx Web Server

Now, execute the following command to install Nginx web server.

sudo apt install nginx

That's it, we have installed Nginx web server.

Now, let's check the second way of the installation.

Install the dependencies

Here we are installing build essential because we need gcc compiler.

sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g libssl-dev libgd-dev libxml2

Download the latest release

Now, we need to download the latest release from [official website](https://nginx.org/en/download.html).

wget https://nginx.org/download/nginx-1.24.0.tar.gz

Extract the file

Next, we will extract it and change our working directory. Execute set of following commands:

tar xvzf nginx-1.24.0.tar.gz
cd nginx-1.24.0

Install Nginx Web server 

We need to run configure command. 

sudo ./configure --prefix=/var/www --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre  --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --modules-path=/etc/nginx/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_gunzip_module

Here we have mentioned different path for different options. With the source installation, we can fully customize the installation with options:

  • --with-http_v3_module: Enables building a module that provides support for HTTP/3.
  • --with-http_image_filter_module=dynamic: For transforms image.
  • --with-http_mp4_module: Server-side support for MP4 files.
  • --without-http_empty_gif_module: Disables building a module that emits single-pixel transparent GIF.
  • --with-http_perl_module=dynamic: Enables building the embedded Perl module.

And many more. Before using other modules, visit [official document page](https://nginx.org/en/docs/configure.html) to learn more about the modules.

Next, compile and install the files using following commands:

sudo make 
sudo make install

Configure firewall

If you have firewall enabled, you need to add 80 and 443 port in the firewall. Execute following commands:

firewall-cmd --add-port={80,443}/tcp --permanent
firewall-cmd --reload

Verify the installation

Finally, let's verify the installation. Execute following command:

sudo nginx

Navigate to your browser and access it using your server IP.

That's it, we have seen how to install Nginx Web Server on Ubuntu.