Skip to main content

Two shell script to enable/disable NGINX sites.

##
# ENABLE NGINX SITE 'wwwenablesite.sh'
##

#!/bin/sh

##
# Enables the specified available nginx site
#
# ref: https://github.com/h5bp/server-configs-nginx/blob/master/doc/sites-enabled.md
# ref: https://gist.github.com/fideloper/8261546
##

set -e

sites_available_path="/usr/local/etc/nginx/sites-available"
sites_enabled_path="/usr/local/etc/nginx/sites-enabled"

if [ "$(id -u)" != "0" ]; then
    echo "error: You must be root to run 'wwwenablesite'." >&2
    exit 1
fi

if [ -z "$1" ]; then
    echo "Usage: wwwenablesite <available_site>"
    echo "       Enables the specified available nginx site."
    exit 1
fi

# -h filename: True if file exists and is a symbolic link.
# -f filename: Returns True if file, filename is an ordinary file.
if [ -h "${sites_enabled_path}/$1" ] || [ -f "${sites_enabled_path}/$1" ]; then
    echo "'$1' is already enabled."
    exit 1
else
    echo "> Enabling site '$1'..."
    if [ ! -f "${sites_available_path}/$1" ]; then
        echo "Site '$1' does not exist in '${sites_available_path}'."
    else
        cd "${sites_enabled_path}"
        [ -L "$1" ] && unlink "$1"
        ln -s "../sites-available/$1" .

        # restart nginx again php-fpm services
        service nginx restart
        service php-fpm restart

        echo "Enabled site '$1'."
        echo "Finished."
        exit 0
    fi
fi

##
# DISABLE NGINX SITE 'wwwdisablesite.sh'
##

#!/bin/sh

##
# Disables the specified enabled nginx site
#
# ref: https://github.com/h5bp/server-configs-nginx/blob/master/doc/sites-enabled.md
# ref: https://gist.github.com/fideloper/8261546
##

set -e

sites_enabled_path="/usr/local/etc/nginx/sites-enabled"

if [ "$(id -u)" != "0" ]; then
    echo "error: You must be root to run 'wwwdisablesite'." >&2
    exit 1
fi

if [ -z "$1" ]; then
    echo "Usage: wwwdisablesite <enabled_site>"
    echo "       Disables the specified enabled nginx site."
    exit 1
else
    echo "> Disabling site '$1'..."
    # -h filename: True if file exists and is a symbolic link.
    # -f filename: Returns True if file, filename is an ordinary file.
    if [ ! -h "${sites_enabled_path}/$1" ] && [ ! -f "${sites_enabled_path}/$1" ]; then
        echo "error: '$1' is not enabled."
        exit 1
    else
        cd "${sites_enabled_path}"
        rm "${sites_enabled_path}/$1"

        # restart nginx again php-fpm services
        service nginx restart
        service php-fpm restart

        echo "Disabled site '$1'."
        echo "Finished."
        exit 0
    fi
fi