So From 80-90 Day I will be sharing my upcoming DevOps Pipeline Project ranging from Linux Monitoring,Shell Scripting, CI/CD, Docker, Kubernetes, and many more.
Project Overview
In this Linux Monitoring Project, I will be creating a Monitoring Script for Nginx which will be monitoring the status of the Nginx.
What is Nginx ?
- Nginx, pronounced engine-ex began as a web server but has grown to also act as a reverse proxy, caching data from the web, and balancing loads between servers.
How Does Nginx Works ?
Nginx minimizes memory usage and handles multiple requests simultaneously. It adopts an
event-driven model
, avoiding the need for new processes per request.Nginx employs a master-worker setup where
one master
processsupervises multiple worker
processes.Workers
handle actual request processing, coordinated by the master.Requests are processed asynchronously, allowing each worker to handle requests concurrently without blocking others.
Key Features:
Acts as a reverse proxy with caching.
Supports IPv6 and load balancing.
Provides FastCGI support with caching.
Handles WebSockets.
Efficiently serves static files and indexes.
Project
- Step 1: Firstly we will check if the Nginx is installed or not. If not then we will install it.
# Check if Nginx is installed or not if its installed then it will show the path of the Nginx.
which nginx
- Step 2: Now we will install Nginx on the local machine.
sudo apt install nginx -y
- Step 3: Now check the version of Nginx installed.
nginx -v
- Step 4: Now we will see some system commands for Nginx.
# Start Nginx
sudo systemctl start nginx
# Stop Nginx
sudo systemctl stop nginx
# Restart Nginx
sudo systemctl restart nginx
# Check the status of Nginx
sudo systemctl status nginx
- Step 5: Now we will create a Monitoring Script for Nginx.
#!/bin/bash
# This script is used to monitor the status of the Nginx service
# Specify the shell to be used for running the script
SHELL=/bin/bash
# Set the PATH variable to include essential directories
PATH=/sbin:/bin:/usr/sbin:/usr/bin
# Define color variables for output
Red=$'\e[1;31m'
Green=$'\e[1;32m'
Blue=$'\e[1;34m'
# Check the status of the Nginx service and redirect the output to a file
service nginx status > output_file.txt
# Check if the word "running" is present in the output file
if grep -q "running" output_file.txt; then
echo $Blue "The Nginx Service is running"
echo $Green $(date +%I)$(date +:%M)$(date +:%S) $(date +%p) $(date +%Z)
else
# If Nginx is not running, start the service
if service nginx start; then
echo $Red "Nginx Service is starting......"
sleep .5s
else
echo "Failed to start Nginx Service."
fi
fi
# Remove the temporary output file
sudo rm -f output_file.txt
- Step 7: Now we will make the script executable and run it.
# Make the script executable
chmod 777 monitoring.sh
# 777 means that the owner, group, and others have read, write, and execute permissions.
- Step 8: Now we will run the script.
./monitoring.sh
- Step 9: Now if the Nginx Service is not running or stopped we get the output as below.
- Step 10: Now if the Nginx Service is not running then we will schedule the script to run every 2 minutes using Cron Job.
# Open the crontab file
crontab -e
- Step 11: Now add the below line to the crontab file to run the script every 2 minutes.
# Run the monitoring script every 5 minutes
*/2 * * * * /home/rohit/monitoring.sh