Install Nginx on Ubuntu22.04

Page content

How to install Nginx Web Server on Ubuntu 22.04

Nginx is an all in one load balancer, reverse proxy and web server. It is free and open source and preferred for its speed and security.

In this post, we will explain how to install Nginx web server on an EC2 instance running Ubuntu 22.04.

First of all, let us create an ec2 instance on which we can install the server.

Login to your AWS console and go to your EC2 dashboard. Now, click on the launch instances button.

Give your instance a name and select Ubuntu for the Amazon Machine Image.

Create EC2 Keypairs

Amazon CloudSearch Integration With Wordpress

We will be using a t2.micro instance for this tutorial. Now, you need to add a keypair and then create the security group to allow SSH traffic as well as http traffic and https traffic from the internet. Once you are finished launching the instance and wait a few minutes for the instance to start. When you launch the instance, it is initially in the pending stage. Once the instance is running, we can ssh to the instance and begin the installation.

How to select the right EC2 instance family for your workload

Click on the connect button when the instance is running. It will allow you to ssh to your instance.

We will first update the repositories to make sure that the latest updates and patches are installed.

Run the following command:

Sudo apt update

Let the updates finish. It takes no more than a few minutes. Now, let’s install the Nginx server.

sudo apt install nginx -y

Wait till the server gets installed on your instance. Once the server has been installed, you can check its status:

sudo systemctl status nginx

You will see an output like the following showing that the Nginx server is active and running on your instance:

$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Mon 2024-04-29 07:58:08 UTC; 31s ago
       Docs: man:nginx(8)
    Process: 2031 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 2032 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 2034 (nginx)
      Tasks: 2 (limit: 1130)
     Memory: 1.7M (peak: 1.9M)
        CPU: 13ms
     CGroup: /system.slice/nginx.service
             ├─2034 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             └─2035 "nginx: worker process"

Apr 29 07:58:08 ip-172-31-16-145 systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server...
Apr 29 07:58:08 ip-172-31-16-145 systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.

You can check the version of the installed nginx server with:

nginx -v

You will receive an output like the following showing the version installed on your instance:

nginx version: nginx/1.24.0 (Ubuntu)

You can start, stop, reload and enable or disable the nginx server with the following commands.

Start nginx server:

sudo systemctl start nginx

Stop nginx server:

sudo systemctl stop nginx

Reload nginx server:

Sudo systemctl reload nginx

Enable nginx server:

Sudo systemctl enable nginx

Disable nginx server:

Sudo systemctl disable nginx

You can also hard restart nginx server with:

Sudo systemctl restart nginx

Now, we will allow nginx traffic through the firewall. To allow both http and https traffic through the firewall

$ sudo ufw allow 'Nginx Full'
Rules updated
Rules updated (v6)

We have not installed ssl yet, so it will not load the https version but if you access the public ip like in a browser, it will show the default nginx page.

nginx server on Ubuntu

Now, copy the ip address and from the terminal type ‘ curl -i your public ip’:

curl -i

You will receive the headers and the html of the Nginx welcome page as output:

$ curl -i
HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
Date: Mon, 29 Apr 2024 09:09:12 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Mon, 29 Apr 2024 07:58:07 GMT
Connection: keep-alive
ETag: "662f530f-267"
Accept-Ranges: bytes

<!DOCTYPE html>
<title>Welcome to nginx!</title>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href=""></a>.<br/>
Commercial support is available at
<a href=""></a>.</p>

<p><em>Thank you for using nginx.</em></p>

nginx server Ubuntu 22.04

As you can see above, we received a 200 OK response and towards the bottom it is the html page.

So, we have successfully installed the nginx server on our virtual machine. Next, we can configure it to serve website content by creating a server block and adding the website configurations.

First of all, we will create the root folder for the new website we are going to run using nginx.

Sudo mkdir -p  /var/www/example_domain

Now, we will configure the ownership and permissions for the root folder:

sudo chown -R $USER:$USER /var/www/example_domain
sudo chmod 755 /var/www/example_domain

Now, we can add a simple html page to our website’s root folder. We will add some Lorem Ipusm text to it and title it My Example Domain.

Sudo nano /var/www/example_domain/index.html

Now, paste the following content into thai file:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Example Domain</title>
        <h1>Welcome to My Example Domain</h1>

            <h2>About Us</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum auctor ultricies velit, sit amet efficitur ipsum ultrices quis. Mauris nec dui vel nisi suscipit suscipit. Phasellus pulvinar bibendum dolor, in condimentum nisi efficitur ac. Nullam accumsan dignissim libero, ac lacinia odio tincidunt sed. Duis eu dolor at ligula viverra bibendum. Aliquam erat volutpat. Mauris sit amet sapien ut velit dignissim ultrices. Nam eleifend lectus eget velit aliquam, at pulvinar nunc ultrices.</p>

            <h2>Our Services</h2>
            <p>Nullam nec tincidunt arcu. In sit amet nulla eu libero rhoncus gravida. Ut sit amet urna vel tellus mollis pulvinar vel sit amet velit. Sed sed feugiat nisi, a pulvinar felis. Fusce malesuada libero non ex vehicula, eget fermentum lorem tempor. Curabitur ac dolor et ex posuere bibendum. Nam euismod magna vitae libero finibus, et placerat ligula bibendum. Donec sodales condimentum rutrum. Proin convallis dui et risus volutpat, quis ullamcorper velit egestas. Curabitur auctor vel turpis id dapibus.</p>

            <h2>Contact Us</h2>
            <p>Integer nec sem nec nulla malesuada eleifend. Etiam vel nisi auctor, hendrerit mauris a, pellentesque mauris. Phasellus suscipit metus at lorem ultricies tincidunt. Vivamus egestas tellus vitae tortor blandit, in egestas lacus tristique. Nam ut metus sed turpis finibus vestibulum. Nullam pretium placerat nulla, a tincidunt felis vestibulum et. Donec vel turpis nec velit vehicula bibendum quis a justo. Nam et ultricies mi, a laoreet lectus.</p>

        <p>&copy; 2024 My Example Domain. All rights reserved.</p>

Our html website is ready to be accessed online. However, we will first need to create a server block. The server block is the counterpart of the virtual hosts file that we create in the Apache web server.

Sudo nano /etc/nginx/sites-available/example_domain

To this file, add the following:

server	{
listen 80;
root /var/www/example_domain;
index index.html index.htm index.nginx.debian.html;
location /      	{
try_files $uri $uri/ =404;

In the above file replace the root folder with the folder you created and the server_name with your real domain names in non www and www versions or just one. Save the file and close it. (ctrl +s and Ctrl + X).

Now, we need to create a symbolic link between the sites-available directory and the startup directory.

sudo ln -s /etc/nginx/sites-available/example_domain /etc/nginx/sites-enabled

This is the same step as a2ensite in Apache server. We are linking the newly added site in sites-available directory to sites-enabled directory.

Now, restart the nginx server:

And test the nginx configuration:

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If the output shows that syntax is ok and the test is successful, everything has been set up perfectly and we can move ahead to making the dns changes so we can load our website using its domain name.

Make changes to your DNS and load the website using its domain name.

nginx server installation

Suggested Reading