DEV Community

Nonso Echendu
Nonso Echendu

Posted on

Setting up Nginx Server

Objective

Hello builders! Today we'll be looking at something basic, i.e setting up and configuring an nginx web server.

We'll be using it to host a very simple webpage.

Prerequisites

  1. An Ubuntu Server

I'll be using AWS to quickly spin up an Ubuntu server using an ec2 instance.

You can check out the official AWS documentation on how to create an Ubuntu ec2 instance here.

Because Nginx listens on port 80 (HTTP), i'll also be creating an inbound rule to allow traffic to port 80.

Also allow traffic to port 22, so that we can SSH into the ubuntu server to configure Nginx.


Installing Nginx

  • Now that we've got our Ubuntu server running, SSH into the just the ubuntu server, using:
ssh -i <your-pem-key-file> ubuntu:<your-ec2-public-ip>
Enter fullscreen mode Exit fullscreen mode
  • Next, let's install Nginx.

First, we'll update Ubuntu's package lists:

sudo apt update && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

The -y command-line option is to auto-approve and skip the prompt.

Install Nginx:

sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode

Next we'll enable and start the installed Nginx service

sudo systemctl enable nginx
sudo systemctl start nginx
Enter fullscreen mode Exit fullscreen mode


Let's verify our Nginx configuration and check if Nginx is running:

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

If successfully installed and running, you should see an output like this:

Image description


Setting Up Nginx

Now that we've installed Nginx successfully, let's set up a custom webpage that'll be shown by default when we visit our public ip address on any browser.

We'll be creating a custom index.html file in /var/www/html/index.html:

nano /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode


Then we create a basic html template. Paste the following line of code into the index.html file:

<!DOCTYPE html>
<html>
<head>
    <title>A Custom Nginx Page</title>
</head>
<body>
    <h1>Welcome to DevOps Stage 0 - [Nonso Echendu]/Michaelo_0x]</h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode



Next, we want to configure our nginx server, such that it listens for HTTP requests on port 80, and will serve files from /var/www/html.

Let's edit the Nginx default site configuration file:

sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

Replace everything in it with this:

server {
    listen 80 default_server;

    root /var/www/html;
    index index.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }
}
Enter fullscreen mode Exit fullscreen mode

Let me explain some key lines in the above configuration.

listen 80 default_server tells Nginx to listen for incoming connections on port 80.

The default_server flag means that if a request comes in and it doesn't match any other server block, this server block will respond by default.

root /var/www/html; defines the root directory that Nginx should go to look for files to serve. Remember, that we have our custom index .html file in that directory - /var/www/html.

index index.html specifies when anyone goes to your webpage http://<your-public-ip>, it should serve the default index.html file which we created earlier.



Now let's restart our Nginx server for all these changes to take effect.

Restart Nginx with this command:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Re-confirm the Nginx server is running by using:

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

The output should be something like this:

Image description


And that's it! We have our Nginx server configured to serve a custom webpage.

Let's now confirm all that we've done visually.

Get your ec2 instance public ip, go to a web browser, and visit http://<your-public-ip>. You should see something like this:

Image description


Conclusion

And so there we have it. I was able to show how to install and setup an Nginx on a fresh Ubuntu server.

I also showed how to a create a custom HTML web page and configure Nginx to serve that webpage by default.


P.s.
This mini project is a task given to me by HNG Intenship - an internship program that will be running for a couple of months.

If you're looking to hire Devops engineers (and related fields) do check these out:

DevOps Engineers

Cloud Engineers

Site Reliability Engineers

Platform Engineers

Infrastructure Engineers

Kubernetes Specialists

AWS Solutions Architects

Azure DevOps Engineers

Google Cloud Engineers

CI/CD Pipeline Engineers

Monitoring/Observability Engineers

Automation Engineers

Docker Specialists

Linux Developers

PostgreSQL Developers

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more