Skip to content

Instantly share code, notes, and snippets.

@victordit
Last active November 20, 2017 21:14
Show Gist options
  • Save victordit/e35ef7e233a215bb42a7b6c227d09142 to your computer and use it in GitHub Desktop.
Save victordit/e35ef7e233a215bb42a7b6c227d09142 to your computer and use it in GitHub Desktop.
#!/bin/bash
#config
sites_available="/etc/apache2/sites-available/"
sites_enabled="/etc/apache2/sites-enabled/"
site_default="000-default.conf"
domain_default="localhost"
path_default="/var/www/html"
hosts="/etc/hosts"
local_ip="127.0.0.1"
# sudo sh create_virtual_host.sh dev.mysite.com /var/www/html/mysite.com
# for restart: sudo sh create_virtual_host.sh dev.mysite.com /var/www/html/mysite.com restart
domain=$1
path=$2
ssl=$3
restart=$4
if [[ ! -z $restart && $ssl == 'restart' ]]; then
restart=$ssl
fi
if [[ $1 == '--help' || $1 == '-h' ]]
then
echo "use: sudo create_virtual_host.sh <domain> <path> <restart>";
echo "<domain> dev.mysite.com"
echo "<path> /var/www/html/mysite.com"
echo "<ssl> (optional) the ssl 'config host under port 443"
echo "<restart> (optional) the word 'restart' restart apache2"
exit;
fi
# check required params
if [[ -z $domain || -z $path ]]
then
echo "--domain (1) and --path (2) are options required. View the help with -h"
exit;
fi
filename=${sites_available}${domain}.conf
sml=${sites_enabled}${domain}.conf
vhost="<VirtualHost *:80>
ServerAdmin support@${domain}
ServerName ${domain}
ServerAlias ${domain}
DocumentRoot ${path}
ErrorLog ${path}/error.log
CustomLog ${path}/access.log combined
<Directory ${path}>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>"
vhostssl='<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster@${domain}
ServerName ${domain}
ServerAlias ${domain}
DocumentRoot ${path}
ErrorLog ${path}/error.log
CustomLog ${path}/access.log combined
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
<Directory ${path} >
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
</IfModule>'
# create new host
echo "-- Create new virtual host - the script require the sudo privileges"
echo "--- Name: $domain"
echo "--- Path: $path"
echo "-- Create new file of Virtual Host"
#copy the default file of virtaul host
if [[ ! -z $ssl ]]
then
echo -e "${vhostssl}" >> $filename
else
echo -e "${vhost}" >> $filename
fi
echo " --- File created in $filename"
#create symlink link
echo "-- Create symlink link for sites_enabled"
ln -s $filename $sml
echo " --- File created in $sml"
#insert virtual host in file hosts
echo "Insert VH in file hosts: $hosts"
printf " \n # $domain in local virtual host \n $local_ip $domain " >> $hosts
echo "Inserted VH in file hosts successfully"
#enabled site
echo "Enabled site ${domain}.conf"
a2ensite ${domain}.conf
echo "${domain}.conf enabled successfully "
#restart apache2
if [[ $restart == 'restart' ]]
then
echo "Restart Apache"
service apache2 restart
echo "Server restarted "
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment