Introduction
As part of the DevOps track with HNG, I was tasked with setting up and configuring NGINX on an Ubuntu server to render a simple webpage. This is a common responsibility for a DevOps Engineer, and I am happy to walk you through the process :)
Prerequisites
To follow along, you’ll need access to a cloud provider to host a Linux server.
Note: I am using Amazon Web Service (AWS) to host my Linux server, if you need guidance on setting up a Linux server on AWS, click here. Additionally, ensure that your server is accessible over the internet by configuring your security group to allow HTTP (port 80) and HTTPS (port 443) traffic.
Steps
Logging Into The Server
We can log in via a terminal emulator or ssh clients.
If you’re using a Linux or macOS system, you can access your server using the built-in terminal. Open a terminal and run:ssh -i /path/to/your-private-key.pem username@your-server-ip
- /path/to/your-private-key.pem is actual path to your SSH private key.
- username is the server's username (default is ubuntu for Ubuntu instances).
- @your-server-ip is the server's public IP address.
If you need to understand SSH in depth and how to use SSH to access a server, check out this article I wrote.
For this task, I am using the Mobaxterm tool to access the Linux server.
Updating Packages and Installing Nginx
Once logged in, run this command;
touch nginx.sh
the cmd creates a file named nginx.sh
next, we open the file and write some commands in the file.
to open the file, run this cmd;nano nginx.sh
paste these commands in the file;
sudo apt update && sudo apt upgrade -y
sudo apt install nginx -y
sudo apt update && sudo apt upgrade -y
– Updates the package list and upgrades all installed packages to their latest versions.sudo apt install nginx -y
– Installs the Nginx web server without prompting for confirmation.
Ctrl + S to save the content and Ctrl + x to exit.
Executing the File
We need to run the file but we can't because it is not executable. To do this, we first need to change the permissions of the file. To change this, run this cmd;
chmod +x nginx.sh
now to run this file, run this cmd;
We can confirm the status of nginx using this cmd;
systemctl status nginx
Nginx renders web pages in the /var/www/html directory by default. We will need to navigate to here and create the html we will render.
To navigate there, run this cmd;cd /var/www/html
Once here, we will create a file named index.html and open it using nano;touch index.html
.
nano index.html
If you get a permission error, run this cmd;sudo su
Here, I wrote the required text to complete this stage.
Ctrl + S to save the content of the file and Ctrl + x to exit.
Now, we check the status of nginx and restart the service.
To check the status and restart nginx, run these cmds;
systemctl status nginx
systemctl restart nginx
If after running systemctl status nginx and it is not active, run this cmd;systemctl enable nginx
At this point, the web page can be accessed via the public IP of the server.
http://<your-server-ip>/
Learning Outcome
This task demonstrates my ability to automate the configuration of a web server and deliver a functional web server.
Looking to Hire the Best Engineers
HNG connects companies with the best engineers suited to their needs. Looking for top-tier talent to handle your DevOps, cloud infrastructure, and CI/CD tasks, HNG has the best developers available for hire and freelance jobs anywhere in the world.
Explore specialized talents available for hire:
Devops Engineers
Linux Developers
Author Of article : Akalezi Caleb🌴 Read full article