linux-server-tools/makevhost/makevhost.sh

82 lines
3.1 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
## MakeVHost Script
## Author: Ze'ev Schurmann
## Reddit: u/thisiszeev
## License: GPL-3.0 or later
## This script creates a vhost on a debian based server running Apache2. It assumes that
## the www path is /var/www/ and that the Apache2 logs are stored at /var/log/apache2/
## To use it, either use ./makevhost.sh {domainname} or simple use ./makevhost.sh and it
## will ask you for the domain name.
## Requires certbot from Let's Encrypt
## Please consider making me small donation. Even though my scripts are open source and free to use, I still need to eat. And the
## occasional bottle of wine also goes down well.
## $5 buys me a cup of coffee
## $10 buys me a nice burger
## $20 buys me a bottle of wine
## Anything above that will be awesome as well.
## You can send me a donation via Paypal https://www.paypal.com/paypalme/thisiszeev
## Drop me a message on Reddit if you do make a donation.
## Support is only offered freely to those who donate $20 or more.
## Your donation contributes to further development.
## If you need a custom script, contact me on Reddit for pricing.
if [[ -z $1 ]]; then
echo -n "Enter domain to add to Apache2 server as vhost: "
read domain
else
domain=$1
fi
echo "Creating web folder at /var/www/$domain/"
mkdir "/var/www/$domain"
touch "/var/www/$domain/index.html"
echo "<html><head><title>$domain</title></head><body><p>$domain is working!</p></body></html>" >> "/var/www/$domain/index.html"
chown -R www-data:www-data "/var/www/$domain"
echo "Creating log folder at /var/log/apache2/$domain/"
mkdir "/var/log/apache2/$domain"
chown -R root:adm "/var/log/apache2/$domain"
echo "Configuring Apache2"
touch "/etc/apache2/sites-available/$domain.conf"
echo "<VirtualHost *:80>" >> "/etc/apache2/sites-available/$domain.conf"
echo " # Created using MakeVHost Script" >> "/etc/apache2/sites-available/$domain.conf"
echo " # https://git.zaks.web.za/thisiszeev/linux-server-tools" >> "/etc/apache2/sites-available/$domain.conf"
echo " ServerName $domain" >> "/etc/apache2/sites-available/$domain.conf"
echo " ServerAdmin webmaster@localhost" >> "/etc/apache2/sites-available/$domain.conf"
echo " DocumentRoot /var/www/$domain" >> "/etc/apache2/sites-available/$domain.conf"
echo " ErrorLog /var/log/apache2/$domain/error.log" >> "/etc/apache2/sites-available/$domain.conf"
echo " CustomLog /var/log/apache2/$domain/access.log combined" >> "/etc/apache2/sites-available/$domain.conf"
echo "</VirtualHost>" >> "/etc/apache2/sites-available/$domain.conf"
systemctl reload apache2
echo "Enabling VHost for $domain"
a2ensite $domain.conf
systemctl reload apache2
echo "Checking if DNS is working..."
test=$( nslookup $domain | grep "** server can" )
if [[ -z $test ]]; then
echo "Looks good!"
else
echo "Please configure the DNS zone for the domain to point to this server and set the TTL to 180 or less."
echo "Will keep checking every 300 seconds..."
echo "To exit, press CTRL+C"
while [[ ! -z $test ]]; do
sleep 300s
test=$( nslookup $domain | grep "** server can" )
done
echo "Looks good!"
fi
echo "Setting up Let's Encrypt Certificate"
certbot --apache -d "$domain"
echo "DONE!!!"