Last active
January 25, 2022 17:17
-
-
Save idimopoulos/faa6623533184ddb619d516d674b59c2 to your computer and use it in GitHub Desktop.
a2createsite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ======= Variables ======== | |
# Set the path to your localhost | |
CDPATH=/var/www/html/ | |
apache_config=/etc/apache2/sites-available | |
host_name_suffix=test | |
# ======= /Variables ======== | |
# This script creates virtual hosts. | |
# you should put it under /usr/local/bin/ | |
# and run it with '$ sudo a2createsite' | |
# Based on https://gist.github.com/tsi/3088993#file-newvhost | |
# This script runs for apache2 on Ubuntu 14+. Possibly on other systems as well | |
# but I have not tried them. | |
# Move to webroot for autocomplete. | |
cd "${webroot}" | |
echo "Moving to webroot." | |
if [ "$(whoami)" != "root" ]; then | |
echo "This command needs to run as root. Please run 'sudo a2createsite' instead." | |
exit 1 | |
fi | |
while [[ -z "$site_dir_true" ]] | |
do | |
read -e -p "Enter directory name under $CDPATH: " site_dir | |
# If passed data is empty, display an error. | |
if [[ -z "$site_dir" ]]; then | |
echo "Directory cannot be empty." | |
# If directory is not found, prompt to create it. | |
elif [[ ! -d "${CDPATH}${site_dir}" ]]; then | |
while [[ "${create_dir}" != 'y' && "${create_dir}" != 'n' && "${create_dir}" != 'i' ]] | |
do | |
read -p "Directory does not exist. Create it? (I/y/n for 'ignore', 'yes' and 'no'(abort))): " create_dir | |
if [[ -z "${create_dir}" ]]; then | |
create_dir='i' | |
fi | |
create_dir="${create_dir,,}" | |
if [[ "${create_dir}" != 'y' && "${create_dir}" != 'n' && "${create_dir}" != 'i' ]]; then | |
echo "Only 'y', 'n' and 'i' are accepted." | |
fi | |
done | |
if [[ "${create_dir}" = 'y' ]]; then | |
mkdir -p "${CDPATH}${site_dir}" | |
site_dir_true=true | |
elif [[ "${create_dir}" = 'i' ]]; then | |
site_dir_true=true | |
elif [[ "${create_dir}" = 'n' ]]; then | |
echo "Nothing to do here. Aborting." | |
exit 1 | |
fi | |
# If directory exists, everything is ok. | |
elif [[ -d "${CDPATH}${site_dir}" ]]; then | |
site_dir_true=true | |
fi | |
done | |
# Remove trailins slash. | |
site_dir=${site_dir%/} | |
echo "The site directory to be used is under ${CDPATH}${site_dir}" | |
# Get a name for the configuration file. | |
while [[ -z "${config_unique}" ]]; do | |
read -p "Enter the name of the configuration file (without the trailing '.conf'): " config_name | |
if [[ -z "${config_name}" ]]; then | |
echo "You must give a name (without the trailing '.conf')." | |
elif [[ -f "${apache_config}/${config_name}.conf" ]]; then | |
echo "File already exists. Enter another name." | |
else | |
config_unique=true | |
fi | |
done | |
# Get a name for the configuration file. | |
read -p "Enter the hostname for the site (suffix .${host_name_suffix} will be added)[${config_name}]: " host_name | |
if [[ -z "${host_name}" ]]; then | |
host_name=${config_name} | |
fi | |
# Create the file with VirtualHost configuration in /etc/apache2/site-available/ | |
echo "<VirtualHost *:80> | |
DocumentRoot ${CDPATH}${site_dir}/ | |
ServerName ${host_name}.${host_name_suffix} | |
<Directory ${CDPATH}${site_dir}/> | |
Options +Indexes +FollowSymLinks +MultiViews +Includes | |
AllowOverride All | |
Order allow,deny | |
allow from all | |
</Directory> | |
</VirtualHost> | |
<VirtualHost *:443> | |
DocumentRoot ${CDPATH}${site_dir}/ | |
ServerName ${host_name}.${host_name_suffix} | |
SSLEngine On | |
SSLCertificateFile /etc/apache2/ssl/apache.pem | |
<Directory ${CDPATH}${site_dir}/> | |
Options +Indexes +FollowSymLinks +MultiViews +Includes | |
AllowOverride All | |
Order allow,deny | |
allow from all | |
</Directory> | |
</VirtualHost>" >$apache_config/$config_name.conf | |
# Add the host to the hosts file | |
echo 127.0.0.1 ${host_name}.${host_name_suffix} >> /etc/hosts | |
# Enable the site | |
a2ensite $config_name | |
# Reload Apache2 | |
#/etc/init.d/apache2 restart | |
service apache2 reload | |
echo "Your new site is available at http://${host_name}.${host_name_suffix}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<VirtualHost *:8000>
ServerAlias localhost *.local *.loc #wildcard catch all
VirtualDocumentRoot /var/www/%1
UseCanonicalName Off
<Directory "/var/www">
Options FollowSymLinks
AllowOverride None