Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6bf8bdd7b1 | ||
|
|
4c397b89b7 | ||
|
|
cce5a7c32f | ||
|
|
2ffa4eb3b9 | ||
|
|
7b9a3e6eb6 |
17
README.md
17
README.md
@@ -1,6 +1,6 @@
|
|||||||
# 🚀 NGINX - Site Manager
|
# 🚀 NGINX - Site Manager
|
||||||
|
|
||||||
This script simplifies the process of managing NGINX sites by providing a command-line interface for common operations such as enabling/disabling, listing, and editing site configurations.
|
This script simplifies the process of managing NGINX sites by providing a command-line interface for common operations such as enabling/disabling, listing, editing, creating, and removing site configurations. This script was designed for Ubuntu. If you want to modify it to make it work for other flavors as well, feel free.
|
||||||
|
|
||||||
## 🛠️ Usage
|
## 🛠️ Usage
|
||||||
|
|
||||||
@@ -14,13 +14,17 @@ This script simplifies the process of managing NGINX sites by providing a comman
|
|||||||
- `-d, --disable`: Disable a site by disabling its configuration file.
|
- `-d, --disable`: Disable a site by disabling its configuration file.
|
||||||
- `-l, --list`: List all available sites and their status.
|
- `-l, --list`: List all available sites and their status.
|
||||||
- `-ed, --edit`: Edit the configuration file of a site.
|
- `-ed, --edit`: Edit the configuration file of a site.
|
||||||
|
- `-c, --create`: Create a new configuration file for a site.
|
||||||
|
- `-r, --remove`: Remove a site's configuration file.
|
||||||
|
|
||||||
## 📖 Examples
|
## 📖 Examples
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
./nginx-sm.sh --enable example.com # Enable the site example.com
|
./nginx-sm.sh --enable example.com # Enable the site example.com
|
||||||
./nginx-sm.sh --list # List all available sites
|
./nginx-sm.sh --list # List all available sites
|
||||||
./nginx-sm.sh --edit example.com # Edit the configuration of example.com
|
./nginx-sm.sh --edit example.com # Edit the configuration of example.com
|
||||||
|
./nginx-sm.sh --create example.com # Create a new configuration for example.com
|
||||||
|
./nginx-sm.sh --remove example.com # Remove the configuration for example.com
|
||||||
```
|
```
|
||||||
|
|
||||||
## 📚 Dependencies
|
## 📚 Dependencies
|
||||||
@@ -28,6 +32,15 @@ This script simplifies the process of managing NGINX sites by providing a comman
|
|||||||
- NGINX web server must be installed and running.
|
- NGINX web server must be installed and running.
|
||||||
- The script requires sudo privileges to modify NGINX configuration files.
|
- The script requires sudo privileges to modify NGINX configuration files.
|
||||||
|
|
||||||
|
## Exit Codes
|
||||||
|
|
||||||
|
The script uses the following exit codes to indicate the result of an operation:
|
||||||
|
|
||||||
|
- `0`: Success - The operation completed successfully.
|
||||||
|
- `1`: General error - For example, site configuration file not found, or the specified site does not exist in either available or enabled directories.
|
||||||
|
- `2`: User cancellation - The user aborted the operation (specific to the `remove_site` command).
|
||||||
|
- `3`: Nginx configuration test failed - This is specific to the `enable_site` command and indicates that Nginx has failed the configuration test after attempting to enable a site.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
Copyright (c) 2023 nobody
|
Copyright (c) 2023 nobody
|
||||||
|
|||||||
212
nginx-sm.sh
212
nginx-sm.sh
@@ -1,32 +1,43 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
#!/bin/bash
|
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# NGINX - Site Manager
|
# NGINX - Site Manager
|
||||||
#
|
#
|
||||||
# This script manages NGINX sites, providing functionality for enabling/disabling,
|
# This script manages NGINX sites, providing functionality for enabling/disabling,
|
||||||
# listing, and editing site configurations. It simplifies the process of managing
|
# listing, editing, creating, and removing site configurations. It simplifies the
|
||||||
# NGINX sites by providing a command-line interface for common operations.
|
# process of managing NGINX sites by providing a command-line interface for common
|
||||||
|
# operations.
|
||||||
#
|
#
|
||||||
# Usage:
|
# Usage:
|
||||||
# ./nginx-sm.sh [options]
|
# ./nginx-sm.sh [options] <site>
|
||||||
#
|
#
|
||||||
# Options:
|
# Options:
|
||||||
# -e, --enable Enable a site by enabling its configuration file.
|
# -e, --enable <site> Enable a site by enabling its configuration file.
|
||||||
# -d, --disable Disable a site by disabling its configuration file.
|
# -d, --disable <site> Disable a site by disabling its configuration file.
|
||||||
# -l, --list List all available sites and their status.
|
# -l, --list List all available sites and their status.
|
||||||
# -ed, --edit Edit the configuration file of a site.
|
# -ed, --edit <site> Edit the configuration file of a site.
|
||||||
|
# --editor <editor> Set editor for editing configurations.
|
||||||
|
# -c, --create <site> Create a new site configuration.
|
||||||
|
# -rm, --remove <site> Remove an existing site configuration.
|
||||||
|
# -h, --help Display help information.
|
||||||
#
|
#
|
||||||
# Examples:
|
# Examples:
|
||||||
# ./nginx-sm.sh --enable example.com # Enable the site example.com
|
# ./nginx-sm.sh --enable example.com # Enable the site example.com
|
||||||
# ./nginx-sm.sh --list # List all available sites
|
# ./nginx-sm.sh --list # List all available sites
|
||||||
# ./nginx-sm.sh --edit example.com # Edit the configuration of example.com
|
# ./nginx-sm.sh --edit example.com # Edit the configuration of example.com
|
||||||
|
# ./nginx-sm.sh --create example.com # Create a new configuration for example.com
|
||||||
|
# ./nginx-sm.sh --remove example.com # Remove the configuration for example.com
|
||||||
#
|
#
|
||||||
# Dependencies:
|
# Dependencies:
|
||||||
# - NGINX web server must be installed and running.
|
# - NGINX web server must be installed and running.
|
||||||
# - The script requires sudo privileges to modify NGINX configuration files.
|
# - The script requires sudo privileges to modify NGINX configuration files.
|
||||||
#
|
#
|
||||||
|
# Global Exit Codes:
|
||||||
|
# 0 - Success, the operation completed successfully.
|
||||||
|
# 1 - General error, such as a missing site configuration or invalid command.
|
||||||
|
# 2 - Operation cancelled by the user.
|
||||||
|
# 3 - Nginx configuration test failed (specific to enable_site command).
|
||||||
|
#
|
||||||
# Copyright (c) [2023] [nobody]
|
# Copyright (c) [2023] [nobody]
|
||||||
# Licensed under the MIT License.
|
# Licensed under the MIT License.
|
||||||
################################################################################
|
################################################################################
|
||||||
@@ -58,6 +69,7 @@ declare -g site_to_disable=""
|
|||||||
declare -g site_to_edit=""
|
declare -g site_to_edit=""
|
||||||
declare -g list_sites=false
|
declare -g list_sites=false
|
||||||
declare -g display_help=false
|
declare -g display_help=false
|
||||||
|
declare -g create=""
|
||||||
declare -g editor="nano"
|
declare -g editor="nano"
|
||||||
|
|
||||||
# Fetch NGINX configuration directories
|
# Fetch NGINX configuration directories
|
||||||
@@ -85,6 +97,7 @@ initialize_variables() {
|
|||||||
site_to_edit=""
|
site_to_edit=""
|
||||||
list_sites=false
|
list_sites=false
|
||||||
display_help=false
|
display_help=false
|
||||||
|
create=""
|
||||||
editor="nano"
|
editor="nano"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,25 +114,48 @@ show_help() {
|
|||||||
echo -e "\nnginx Site Manager"
|
echo -e "\nnginx Site Manager"
|
||||||
echo -e "Usage: $(basename "$0") [options] <site>\n"
|
echo -e "Usage: $(basename "$0") [options] <site>\n"
|
||||||
echo -e "Options:"
|
echo -e "Options:"
|
||||||
echo -e "\t-e, --enable <site>: Enable site"
|
echo -e "\t-e, --enable <site> Enable site"
|
||||||
echo -e "\t-d, --disable <site>: Disable site"
|
echo -e "\t-d, --disable <site> Disable site"
|
||||||
echo -e "\t-l, --list: List sites"
|
echo -e "\t-l, --list List sites"
|
||||||
echo -e "\t-ed, --edit <site>: Edit site"
|
echo -e "\t-ed, --edit <site> Edit site configuration"
|
||||||
echo -e "\t--editor <editor>: Set editor"
|
echo -e "\t--editor <editor> Set editor for editing configurations"
|
||||||
echo -e "\t-h, --help: Display help"
|
echo -e "\t-c, --create <site> Create a new site configuration"
|
||||||
|
echo -e "\t-rm, --remove <site> Remove an existing site configuration"
|
||||||
|
echo -e "\t-h, --help Display this help message"
|
||||||
echo -e "\n"
|
echo -e "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Enable a specified site by creating a symbolic link in the sites-enabled directory
|
# Enable a specified site by creating a symbolic link in the sites-enabled directory
|
||||||
enable_site() {
|
enable_site() {
|
||||||
local site=$1
|
local site=$1
|
||||||
if [ ! -e "$sitesAvail/$site" ]; then
|
local site_avail_path="$sitesAvail/$site"
|
||||||
|
local site_enabled_path="$sitesEnabled/$site"
|
||||||
|
|
||||||
|
if [ ! -e "$site_avail_path" ]; then
|
||||||
echo "Site does not appear to exist."
|
echo "Site does not appear to exist."
|
||||||
elif [ -e "$sitesEnabled/$site" ]; then
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -e "$site_enabled_path" ]; then
|
||||||
echo "Site appears to already be enabled."
|
echo "Site appears to already be enabled."
|
||||||
|
return 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
check_sudo
|
||||||
|
|
||||||
|
# Enable the site by creating a symbolic link
|
||||||
|
ln -s "$site_avail_path" "$site_enabled_path"
|
||||||
|
|
||||||
|
# Now check the configuration after enabling the site
|
||||||
|
if nginx -t; then
|
||||||
|
echo "Configuration test passed. Reloading Nginx."
|
||||||
|
nginx -s reload
|
||||||
|
echo "Site enabled and Nginx reloaded."
|
||||||
else
|
else
|
||||||
ln -s "$sitesAvail/$site" "$sitesEnabled/$site"
|
# If the configuration test fails, remove the symbolic link to revert the change
|
||||||
echo "Site enabled."
|
echo "Configuration test failed. Disabling the site."
|
||||||
|
rm "$site_enabled_path"
|
||||||
|
return 3
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,6 +165,7 @@ disable_site() {
|
|||||||
if [ ! -e "$sitesEnabled/$site" ]; then
|
if [ ! -e "$sitesEnabled/$site" ]; then
|
||||||
echo "Site does not appear to be enabled."
|
echo "Site does not appear to be enabled."
|
||||||
else
|
else
|
||||||
|
check_sudo
|
||||||
rm "$sitesEnabled/$site"
|
rm "$sitesEnabled/$site"
|
||||||
echo "Site disabled."
|
echo "Site disabled."
|
||||||
fi
|
fi
|
||||||
@@ -169,6 +206,87 @@ edit_site() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
create_site() {
|
||||||
|
# Assign the first argument to the variable 'site'
|
||||||
|
local site=$1
|
||||||
|
|
||||||
|
# Check if the site configuration already exists
|
||||||
|
if [ -e "$sitesAvail/$site" ]; then
|
||||||
|
echo "Site already exists."
|
||||||
|
else
|
||||||
|
# Ensure the user has administrative privileges
|
||||||
|
check_sudo
|
||||||
|
|
||||||
|
# Create a new site configuration file
|
||||||
|
touch "$sitesAvail/$site"
|
||||||
|
echo "Site created."
|
||||||
|
|
||||||
|
# Prompt the user to edit the new site configuration
|
||||||
|
echo -n "Do you want to edit the new site? [y/n]: "
|
||||||
|
|
||||||
|
# Save current stty configuration and set stty for raw input
|
||||||
|
local old_stty_cfg=$(stty -g)
|
||||||
|
stty raw -echo
|
||||||
|
|
||||||
|
# Read a single character from the user
|
||||||
|
answer=$(head -c 1)
|
||||||
|
|
||||||
|
# Restore previous stty configuration
|
||||||
|
stty $old_stty_cfg
|
||||||
|
|
||||||
|
# Check if the user answered 'yes' (case insensitive)
|
||||||
|
if echo "$answer" | grep -iq "^y" ; then
|
||||||
|
# Open the site configuration file in the default editor
|
||||||
|
$editor "$sitesAvail/$site"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Remove a specified site
|
||||||
|
remove_site() {
|
||||||
|
local site=$1
|
||||||
|
local site_avail_path="$sitesAvail/$site"
|
||||||
|
local site_enabled_path="$sitesEnabled/$site"
|
||||||
|
|
||||||
|
if [[ ! -e "$site_avail_path" && ! -L "$site_enabled_path" ]]; then
|
||||||
|
echo "Site does not appear to exist."
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
check_sudo
|
||||||
|
|
||||||
|
echo "You are about to remove the site: $site"
|
||||||
|
read -r -p "Type 'yes' to confirm: " confirmation
|
||||||
|
if [[ $confirmation != "yes" ]]; then
|
||||||
|
echo "Site removal cancelled."
|
||||||
|
return 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Remove enabled site symlink if it exists
|
||||||
|
if [ -L "$site_enabled_path" ]; then
|
||||||
|
rm "$site_enabled_path"
|
||||||
|
echo "Removed symlink from $site_enabled_path."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Remove available site file if it exists
|
||||||
|
if [ -e "$site_avail_path" ]; then
|
||||||
|
rm "$site_avail_path"
|
||||||
|
echo "Removed file from $site_avail_path."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Site $site removed successfully."
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if the string contains only spaces
|
||||||
|
is_all_spaces() {
|
||||||
|
local input_string="$1"
|
||||||
|
if [[ "$input_string" =~ ^[[:space:]]+$ ]]; then
|
||||||
|
echo "The string consists only of spaces. Exiting the script."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Parse command line arguments
|
||||||
parse_arguments() {
|
parse_arguments() {
|
||||||
while [ "$#" -gt 0 ]; do
|
while [ "$#" -gt 0 ]; do
|
||||||
local key="$1"
|
local key="$1"
|
||||||
@@ -178,6 +296,7 @@ parse_arguments() {
|
|||||||
echo "No site specified to enable. Use -h for help."
|
echo "No site specified to enable. Use -h for help."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
is_all_spaces "$2"
|
||||||
check_sudo
|
check_sudo
|
||||||
site_to_enable="$2"
|
site_to_enable="$2"
|
||||||
shift
|
shift
|
||||||
@@ -187,6 +306,7 @@ parse_arguments() {
|
|||||||
echo "No site specified to disable. Use -h for help."
|
echo "No site specified to disable. Use -h for help."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
is_all_spaces "$2"
|
||||||
check_sudo
|
check_sudo
|
||||||
site_to_disable="$2"
|
site_to_disable="$2"
|
||||||
shift
|
shift
|
||||||
@@ -196,6 +316,7 @@ parse_arguments() {
|
|||||||
echo "No site specified to edit. Use -h for help."
|
echo "No site specified to edit. Use -h for help."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
is_all_spaces "$2"
|
||||||
check_sudo
|
check_sudo
|
||||||
site_to_edit="$2"
|
site_to_edit="$2"
|
||||||
shift
|
shift
|
||||||
@@ -205,9 +326,29 @@ parse_arguments() {
|
|||||||
echo "No editor specified. Use -h for help."
|
echo "No editor specified. Use -h for help."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
is_all_spaces "$2"
|
||||||
editor="$2"
|
editor="$2"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
-c|--create)
|
||||||
|
if [ -z "$2" ]; then
|
||||||
|
echo "No site specified. Use -h for help."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
is_all_spaces "$2"
|
||||||
|
create="$2"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-rm|--remove)
|
||||||
|
if [ -z "$2" ]; then
|
||||||
|
echo "No site specified. Use -h for help."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
is_all_spaces "$2"
|
||||||
|
check_sudo
|
||||||
|
remove="$2"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
-l|--list)
|
-l|--list)
|
||||||
list_sites=true
|
list_sites=true
|
||||||
;;
|
;;
|
||||||
@@ -241,18 +382,29 @@ main() {
|
|||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ -n "$site_to_enable" ]; then
|
||||||
|
enable_site "$site_to_enable"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$site_to_disable" ]; then
|
||||||
|
disable_site "$site_to_disable"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if $list_sites; then
|
||||||
|
list_sites
|
||||||
|
fi
|
||||||
|
|
||||||
if check_editor "$editor"; then
|
if check_editor "$editor"; then
|
||||||
if [ -n "$site_to_enable" ]; then
|
|
||||||
enable_site "$site_to_enable"
|
|
||||||
fi
|
|
||||||
if [ -n "$site_to_disable" ]; then
|
|
||||||
disable_site "$site_to_disable"
|
|
||||||
fi
|
|
||||||
if [ -n "$site_to_edit" ]; then
|
if [ -n "$site_to_edit" ]; then
|
||||||
edit_site "$site_to_edit"
|
edit_site "$site_to_edit"
|
||||||
fi
|
fi
|
||||||
if $list_sites; then
|
|
||||||
list_sites
|
if [ -n "$create" ]; then
|
||||||
|
create_site "$create"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$remove" ]; then
|
||||||
|
remove_site "$remove"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user