BASH

Control Web Server Service (Nginx/Apache)

Easily start, stop, restart, or check the status of your Nginx or Apache web server using a simple bash script for streamlined server management.

#!/bin/bash

SERVICE_NAME="nginx" # Change to "apache2" or "httpd" for Apache

case "$1" in
    start)
        echo "Starting $SERVICE_NAME..."
        sudo systemctl start "$SERVICE_NAME"
        ;;
    stop)
        echo "Stopping $SERVICE_NAME..."
        sudo systemctl stop "$SERVICE_NAME"
        ;;
    restart)
        echo "Restarting $SERVICE_NAME..."
        sudo systemctl restart "$SERVICE_NAME"
        ;;
    status)
        echo "Checking status of $SERVICE_NAME..."
        sudo systemctl status "$SERVICE_NAME"
        ;;
    reload)
        echo "Reloading $SERVICE_NAME configuration..."
        sudo systemctl reload "$SERVICE_NAME"
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status|reload}"
        exit 1
        ;;
esac

if [ $? -eq 0 ]; then
    echo "$SERVICE_NAME operation successful."
else
    echo "Error during $SERVICE_NAME operation. Check service logs."
fi
How it works: This script provides a convenient way to manage a system service, such as a web server like Nginx or Apache. By passing `start`, `stop`, `restart`, `status`, or `reload` as an argument, you can execute the corresponding `systemctl` command. This simplifies server administration tasks and ensures consistent handling of your web server's lifecycle.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs