Skip to content

Instantly share code, notes, and snippets.

@Azoraqua
Created August 15, 2020 13:52
Show Gist options
  • Save Azoraqua/39a6f903c62676eb9a667e58bcd1451f to your computer and use it in GitHub Desktop.
Save Azoraqua/39a6f903c62676eb9a667e58bcd1451f to your computer and use it in GitHub Desktop.
Utility for managing Nginx VHosts (SSL + PHP only).
#!/bin/bash
NGINX_DIR="/etc/nginx"
NGINX_SITES_AVAILABLE_DIR="${NGINX_DIR}/sites-available"
NGINX_SITES_ENABLED_DIR="${NGINX_DIR}/sites-enabled"
## vhost.sh (create|delete|link|unlink|list) [APP_NAME] [DOMAIN]
create() {
if [[ $# -le 1 ]]; then
echo "$0 create <NAME> <DOMAIN>"
return
fi
name=$1
domain=$2
php_version=$(sudo /usr/bin/php --version | head -n 1 | cut -d" " -f2 | awk '{print substr ($1, 0, 4)}')
curl -s https://raw.githubusercontent.com/Azoraqua/NginxExamples/master/nginx-ssl.example -o "$name.conf"
## Copying the example configuration file and replacing the properties accordingly.
sed -i -e "s/<APP_NAME>/$name/g" "$name.conf"
sed -i -e "s/<DOMAIN>/$domain/g" "$name.conf"
sed -i -e "s/<PHP_VERSION>/$php_version/g" "$name.conf"
mv "$name.conf" "$NGINX_SITES_AVAIABLE_DIR/$name.conf"
## Checking if Nginx is running; Letsencrypt requires it to be disabled.
if [[ $(sudo /etc/init.d/nginx status | grep "Active" | cut -d" " -f5) == 'active' ]]; then
sudo /etc/init.d/nginx stop
fi
## Generating the SSL certificates.
letsencrypt certonly -d $domain
sudo /etc/init.d/nginx start
echo "Created VHost $name ($domain)."
}
delete() {
if [[ $# -le 0 ]]; then
echo "$0 delete <DOMAIN>"
return
fi
domain=$1
if [[ -f "$NGINX_SITES_AVAILABLE_DIR/$domain.conf" ]]; then
rm "$NGINX_SITES_AVAILABLE_DIR/$domain.conf"
fi
if [[ -f "$NGINX_SITES_ENABLED_DIR/$domain.conf" ]]; then
rm "$NGINX_SITES_ENABLED_DIR/$domain.conf"
fi
## Checking if Nginx is running; Restarting to apply changes.
if [[ $(sudo /etc/init.d/nginx status | grep "Active" | cut -d" " -f5) == 'active' ]]; then
sudo /etc/init.d/nginx restart
fi
echo "Deleted VHost $domain."
}
link() {
if [[ $# -le 0 ]]; then
echo "$0 link <DOMAIN>"
return
fi
$domain=$1
if [[ -f "$NGINX_SITES_AVAILABLE_DIR/$domain.conf" ]] && [[ ! -f "$NGINX_SITES_ENABLED_DIR/$domain.conf" ]]; then
ln -s "$NGINX_SITES_AVAILABLE_DIR/$domain.conf" "$NGINX_SITES_ENABLED_DIR/$domain.conf"
fi
## Checking if Nginx is running; Restarting to apply changes.
if [[ $(sudo /etc/init.d/nginx status | grep "Active" | cut -d" " -f5) == 'active' ]]; then
sudo /etc/init.d/nginx restart
fi
echo "VHost ($domain) linked."
}
unlink() {
if [[ $# -le 0 ]]; then
echo "$0 unlink <DOMAIN>"
return
fi
$domain=$1
if [[ -f "$NGINX_SITES_AVAILABLE_DIR/$domain.conf" ]] && [[ -f "$NGINX_SITES_ENABLED_DIR/$domain.conf" ]]; then
rm "$NGINX_SITES_ENABLED_DIR/$domain.conf"
fi
## Checking if Nginx is running; Restarting to apply changes.
if [[ $(sudo /etc/init.d/nginx status | grep "Active" | cut -d" " -f5) == 'active' ]]; then
sudo /etc/init.d/nginx restart
fi
echo "VHost ($domain) unlinked."
}
list() {
for host in ${NGINX_SITES_AVAILABLE_DIR}/*; do
echo "[VHost] $host"
done
}
if [[ $# -le 0 ]]; then
echo "$0 <create|delete|link|unlink|list> [name|domain]"
else
case $1 in
"create")
create $2 $3
;;
"delete")
delete $2
;;
"link")
link $2
;;
"unlink")
unlink $2
;;
"list")
list
;;
*)
echo "Unknown command."
;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment