Configure Nginx Web Server in Redhat Linux

Loading

Nginx is a very popular and commonly used open-source web server that can run on both Linux and Windows operating systems. In this blog, we will build a very basic static Nginx website

Lab Server Setup:

Hostname: nginx01.ash.local
Operating System: Redhat 9.2

Packages and Config files required for the Nginx server

  • nginx – The package installs the Nginx web server.
  • links – Optional package for text-based web browser
    Default ports: 80 and 443 (SSL)

These are the main folders storing Nginx config files

PurposeLocation
Version nginx -v
Check if config is correctnginx -t
Default Parent Directory /usr/share/
Main Nginx root configuration directory./etc/nginx
Additional Nginx configuration files or directives( additional website config file )/etc/nginx/conf.d
Main Configuration file/etc/nginx/nginx.conf
Contains log files ( SymLink to /etc/httpd/logs)
/etc/log/nginx
Main web document root directory/var/www
Access Logs/var/log/nginx/access.log
Error Logs/var/log/nginx/error.log

Concept of Master and Worker Process

Master ProcessEvaluates the Config files
Worker ProcessResponsible for serving web content

Step 1 – Set up the host file

Step 2– Check the Nginx daemon(httpd) install

Step 3– Install Nginx

Step 4 – Enable the Nginx service with either nginx using the below command

 

Step 5 – Check the version of Nginx installed.

Step 6 –Verify that port 80 is open

Step 7 –Allow HTTP and HTTPS traffic in our firewall if enabled.

Step 8 – The main Nginx configuration file is located at /etc/nginx/nginx.conf. This file contains directives that affect the entire Nginx server. Directives are used to link a file to this config file when we have multiple websites or config’s that need to be loaded are defined under /etc/nginx/conf.d directory

  • The user that runs is Nginx
  • The error_log is located in /var/log/nginx/error.log
  • The worker connections would imply the max amount of connections the web server can take at a time
  • The Access_log is located in /var/log/nginx/access.log

Our web store is in this path. Additional config files are kept in the /etc/nginx/default.d/ directory.

Step 8 – Let’s create some web content under /usr/share/nginx folder for RHEL.

Step 9 – Test the config file

Step 10 – Verify the webpage

Configure Multiple Websites on Nginx

Nginx uses multiple server block configurations to run multiple websites – ie: Shared Website Hosting.

The default server block is good for single website hosting but if we need multiple websites needs to be hoisted from the same server, we use multiple server blocks which are also known as Virtual Servers.

The objective of a server block host entry in Nginx config is to handle multiple websites from a single IP address on our Nginx VM so that a single massive VM can serve a ton of websites from it which is the principle of Shared Website Hoisting.

We will aim to set up our two hotel websites running from a single web server as shown below – www.east.ash.local and www.west.ash.local from our single server which has a single IP address of 192.168.0.17, so we must have DNS names resolving to this machine IP address or make an entry in /etc/hosts file. Each hotel branch will have its own URL as shown.

Set up the host file

For multiple websites, to be hoisted, create a dedicated directory for each website file and keep all the website content on those directories respectively as shown below. In this example, my east and west website files are kept separately.

For East Website, we will create the following

Open the main configuration file /etc/nginx/conf.d, we will now create two directives one for west and east and make the below changes

Test the config file

Test the east and west website page

(Visited 27 times, 1 visits today)

By Ash Thomas

Ash Thomas is a seasoned IT professional with extensive experience as a technical expert, complemented by a keen interest in blockchain technology.