2021-01-21 16:58:32 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# Create Vhosts on VPS3
|
|
|
|
#
|
|
|
|
DEBUG=0
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2021-02-17 11:24:46 -05:00
|
|
|
function usage
|
|
|
|
{
|
2021-01-21 16:58:32 -05:00
|
|
|
echo "Usage: ${0}"
|
|
|
|
echo " --domain domain.tld"
|
|
|
|
echo " Domain to use when creating vhost"
|
2021-02-17 11:24:46 -05:00
|
|
|
echo " --root /var/www/html"
|
|
|
|
echo " Root directory of this vhost"
|
|
|
|
echo " --backend http://127.0.0.1:80"
|
|
|
|
echo " Hostname of the backend server to pass traffic to"
|
2021-01-21 16:58:32 -05:00
|
|
|
echo " --listenip x.x.x.x"
|
|
|
|
echo " IP to bind to when listening"
|
|
|
|
echo " --desc x.x.x.x"
|
|
|
|
echo " Description of VHosts"
|
|
|
|
echo " -h | --help"
|
|
|
|
echo " Show this usage"
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_cert
|
|
|
|
{
|
2021-02-17 11:32:03 -05:00
|
|
|
/root/.acme.sh/acme.sh --issue --domain $_domain --webroot /srv/http-content-combined/ --cert-file /etc/nginx/ssl/${_domain}.crt --key-file /etc/nginx/ssl/${_domain}.key --fullchain-file /etc/nginx/ssl/${_domain}-fullchain.crt
|
2021-01-21 16:58:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_cwd="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
_bootstrap=${_cwd}/bootstrap.sh
|
|
|
|
|
|
|
|
# Init script
|
2021-02-17 11:24:46 -05:00
|
|
|
if test -f $_bootstrap; then
|
|
|
|
source $_bootstrap 2> /dev/null
|
|
|
|
else
|
|
|
|
echo "Unable to parse BOOTSTRAP: $_bootstrap"
|
|
|
|
exit 1
|
2021-01-21 16:58:32 -05:00
|
|
|
fi
|
|
|
|
|
2021-02-17 11:24:46 -05:00
|
|
|
OPTS=$(getopt -o h -l domain:,root:,backend:,listenip:,desc: -n 'createVhosts' -- "$@")
|
2021-01-21 16:58:32 -05:00
|
|
|
if [ "$?" -gt '0' ]; then
|
|
|
|
echo 'Failed to set command line arguments'
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
|
|
|
eval set -- "$OPTS"
|
|
|
|
|
|
|
|
_domain=false
|
2021-02-17 11:24:46 -05:00
|
|
|
_root=""
|
|
|
|
_backend=""
|
|
|
|
_listenip=""
|
2021-01-21 16:58:32 -05:00
|
|
|
while true; do
|
|
|
|
case "$1" in
|
|
|
|
--domain )
|
|
|
|
_domain=$2
|
|
|
|
shift ;;
|
2021-02-17 11:24:46 -05:00
|
|
|
--root )
|
|
|
|
_root=$2
|
|
|
|
shift ;;
|
|
|
|
--backend )
|
|
|
|
_backend=$2
|
2021-01-21 16:58:32 -05:00
|
|
|
shift ;;
|
|
|
|
--listenip )
|
|
|
|
_listenip=$2
|
|
|
|
shift ;;
|
|
|
|
--desc )
|
|
|
|
_desc=$2
|
|
|
|
shift ;;
|
|
|
|
-h | --help ) usage; shift ;;
|
|
|
|
-- ) shift; break ;;
|
|
|
|
* ) shift;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ $_domain = false ]]; then
|
|
|
|
err "You must set domain"
|
|
|
|
fi
|
|
|
|
|
2021-02-17 11:24:46 -05:00
|
|
|
if test -n "$_root"; then
|
|
|
|
if ! test -d $_root; then
|
|
|
|
err "Path doesn't exists! $_root"
|
|
|
|
fi
|
|
|
|
_rootpath="root $_root;"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test -n "$_backend"; then
|
2021-02-17 11:41:15 -05:00
|
|
|
echo "Verifying backend..."
|
2021-02-17 11:24:46 -05:00
|
|
|
if ! validate_host $_backend; then
|
|
|
|
err "Invalid hostname: $_backend. Not resolvable!"
|
|
|
|
fi
|
|
|
|
_locationblock=$(cat <<- EOF
|
|
|
|
proxy_pass $_backend;
|
|
|
|
include proxy_params;
|
|
|
|
EOF
|
|
|
|
)
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test -n "$_listenip"; then
|
|
|
|
if ! validate_ip $_listenip; then
|
|
|
|
err "Invalid IP: $_listenip"
|
|
|
|
fi
|
|
|
|
_listenip="$_listenip:"
|
|
|
|
else
|
|
|
|
warn "No listen ip specified, listing on all interfaces."
|
2021-01-21 16:58:32 -05:00
|
|
|
fi
|
|
|
|
|
2021-02-17 11:24:46 -05:00
|
|
|
if test -z "$_root" -a -z "$_backend"; then
|
|
|
|
err "You must specify either --root or --backend!"
|
2021-01-21 16:58:32 -05:00
|
|
|
fi
|
|
|
|
|
2021-02-17 11:24:46 -05:00
|
|
|
echo "Creating Nginx configuration..."
|
2021-01-21 16:58:32 -05:00
|
|
|
_vhost_conf_file=/etc/nginx/conf.d/${_domain}.conf
|
|
|
|
|
2021-02-17 11:32:03 -05:00
|
|
|
|
|
|
|
echo -n "Checking if /srv/http-content-combined/ exists?"
|
|
|
|
if ! test -d /srv/http-content-combined; then
|
|
|
|
echo " Creating..."
|
|
|
|
mkdir -p /srv/http-content-combined/
|
|
|
|
else
|
|
|
|
echo " Yes!"
|
|
|
|
fi
|
|
|
|
|
2021-01-21 16:58:32 -05:00
|
|
|
cat << EOF > $_vhost_conf_file
|
|
|
|
#### Description
|
|
|
|
## Type: HTTP
|
|
|
|
## VHost: $_domain
|
|
|
|
## $_desc
|
|
|
|
server {
|
2021-02-17 11:24:46 -05:00
|
|
|
listen ${_listenip}80;
|
2021-01-21 16:58:32 -05:00
|
|
|
server_name $_domain;
|
2021-02-17 11:24:46 -05:00
|
|
|
|
2021-01-21 16:58:32 -05:00
|
|
|
location /.well-known {
|
2021-02-17 11:32:03 -05:00
|
|
|
root /srv/http-content-combined/;
|
2021-01-21 16:58:32 -05:00
|
|
|
autoindex on;
|
|
|
|
}
|
2021-02-17 11:24:46 -05:00
|
|
|
|
2021-01-21 16:58:32 -05:00
|
|
|
location / {
|
|
|
|
return 302 https://${_domain}\$request_uri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
server {
|
2021-02-17 11:24:46 -05:00
|
|
|
listen ${_listenip}443 http2 ssl;
|
2021-01-21 16:58:32 -05:00
|
|
|
server_name $_domain;
|
2021-02-17 11:24:46 -05:00
|
|
|
$_rootpath
|
2021-01-21 16:58:32 -05:00
|
|
|
|
|
|
|
error_log /var/log/nginx/${_domain}.error.log;
|
|
|
|
access_log /var/log/nginx/${_domain}.access.log main;
|
2021-02-17 11:24:46 -05:00
|
|
|
|
2021-01-21 16:58:32 -05:00
|
|
|
ssl_certificate ssl/${_domain}-fullchain.crt;
|
|
|
|
ssl_certificate_key ssl/${_domain}.key;
|
2021-02-17 11:24:46 -05:00
|
|
|
|
2021-01-21 16:58:32 -05:00
|
|
|
location / {
|
2021-02-17 11:24:46 -05:00
|
|
|
${_locationblock}
|
2021-01-21 16:58:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
echo "Setting permissions on conf file..."
|
2021-02-17 11:24:46 -05:00
|
|
|
#setfacl -m user:sysadmin:rw $_vhost_conf_file
|
2021-01-21 16:58:32 -05:00
|
|
|
|
|
|
|
echo "Retrieving Let's Encrypt Certificate..."
|
2021-02-17 11:32:03 -05:00
|
|
|
get_cert
|