Let’s install Nginx on CentOS 8

This is my first ever project with Level Up In Tech. Here I will be installing Nginx on CentOS8.

Nginx is one of the most popular web servers in the world. It is responsible for hosting some of the largest and well known sites on the internet (Dell.com, Zoom.us, Alipay.com, Netflix and more). It is more resource-friendly than Apache in most cases and can be used as a web server or reverse proxy. It has been quickly growing as one of the worlds leading web servers.

So without further ado, lets gets started!

Prerequisites

  • Access to a Command-Line Interface (CLI) such as Command Prompt (Windows) or Terminal (Mac OS)
  • Access to a CentOS8 server as a non-root user with sudo privileges

Step 1: Connect To Your Server

Open your Command Line Interface and SSH into your CentOS8 server. To do so enter the following command with your credentials…

ssh username@public_ip_address

You will be asked to enter your password

Step 2: Update Packages

Now that you are connected to your CentOS8 server lets update the system. Keeping your system updated is an essential sysadmin task. To update, run this command…

sudo yum update

*please note that you will be asked to enter your password when using sudo commands as a non-root user*

If your system is already updated you should see this…

In this case, my system had already been updated. If not, the command will proceed to update all required dependencies.

Step 3: Adjusting Firewall Rules

Run this command to permanently enable HTTP connections on port 80 on your server.

sudo firewall-cmd --permanent --add-service=http
If all went well you should see that it was a “success”

To make sure the HTTP firewall service was added correctly, run this command…

sudo firewall-cmd --permanent --list-all
Here you can see that http was allowed to the list services

To make sure your firewall will work correctly, you’ll need to reload the firewall service. Run the following command to do so…

sudo firewall-cmd --reload
If all went well you should see that it was a “success”

Let’s get some further confirmation that our firewall is indeed running on the server. You can check by entering this command…

systemctl status firewalld
Here in green I can see the firewall is active and running

Step 4: Search For And Install Nginx

Let’s quickly search to see what version of Nginx you will be installing. To do so enter the following command…

sudo yum info nginx
Here we can see that version 1.14.1 is available on my distribution

Finally, let’s install Nginx shall we? Enter this command…

sudo dnf install nginx

Hit ‘y’ when prompted to continue, this may take a few moments, so be patient, grab your coffee. When it’s finished you should see this at the bottom of page…

Now we can enable and start the server with these commands…

sudo systemctl enable nginx
sudo systemctl start nginx

Step 5: Test Nginx

Let’s Verify that TCP port 80 is open with this command…

sudo ss -tulpn

look for this to confirm port 80 is open…

looks like port 80 is open

At this point your Nginx webserver should be up and running. Now we can find out our IP address with this command…

curl -4 icanhazip.com
Here I can see 18.225.11.160. Copy your IP result and place in a browser to test

Place your IP in a browser…If you see this, ya done did it! That’s it! Nginx is now installed on your server.

Optional: Host A Simple HTML On NGINX

You’ll find the file you need to edit in /usr/share/nginx/html. To get there enter this command…

cd /usr/share/nginx/html

Now delete the current index.html file with this command…

sudo rm index.html

Now lets create a new index file with the Nano text editor…

sudo nano index.html

When it opens add your content. Here is an example of mine…

<!DOCTYPE html><html><head><title>LEVEL UP IN TECH</title></head><body><h1>Lets LEVEL UP!</h1><p>Welcome to LUIT</p></body></html>

When finished, hit CTRL and X, then save by pressing Y

Now refresh or enter the IP address again…

If you see something similar to this, you have successfully edited your html file for display on the internet!

Optional 2: Create A Bash Script That Automates The Entire Process

What I did was open up a the Nano text editor and create a new bash script called nginx.sh. You can call your script whatever you like. To do this enter this command…

nano nginx.sh

This will then open up text editor screen on the GNU nano. I then entered all the commands I used in the installation process into the new script file as you can see here…

Don’t forget to start off your bash scripts with !#bin/bash

Once it’s complete, hit CTRL + X to exit. Hit Y to save.

At this point the nginx.sh script was created, but it was not executable. Here is a screencap of the following steps I took…

-rw-r--r--(also known as 644) lets me know that only user has read and write permissions; the group and others can read only.

-rwxr--xr--x (also known as 755) lets me know that the owner can read/write/execute; group/others can read/execute…WAIT! I feel like that is too much power for everyone to have. Lets change that shall we?…

-rwxr--r--(also known as 744) lets me know that the user / owner can read, can write and can execute. Group can read, can’t write and can’t execute. Others can read, can’t write and can’t execute….Ahhh that’s better.

I now have a executable bash script that allows me to do the entire installation process with this command…

sudo sh nginx.sh

Now, I ran this with everything already installed and enabled just to check, (hence the warnings) but it runs! Should I decide to remove everything. I can simply run the script and have it all back again.

Thanks for following along as I learn to do cloud stuffs. Please feel free to let me know if there is anything I can do do clean up this process. Whether it’s constructive criticism or you wanna chuck some tomatoes at me; I am open to it all.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dan Santarossa

Dan Santarossa

DevOps | Cloud | Level Up In Tech