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
- 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>
- Next, let's install Nginx.
First, we'll update Ubuntu's package lists:
sudo apt update && sudo apt upgrade -y
The -y
command-line option is to auto-approve and skip the prompt.
Install Nginx:
sudo apt install nginx -y
Next we'll enable and start the installed Nginx service
sudo systemctl enable nginx
sudo systemctl start nginx
Let's verify our Nginx configuration and check if Nginx is running:
sudo systemctl status nginx
If successfully installed and running, you should see an output like this:
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
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>
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
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;
}
}
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
Re-confirm the Nginx server is running by using:
sudo systemctl status nginx
The output should be something like this:
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:
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:
Top comments (0)