Skip to content

Instantly share code, notes, and snippets.

@swarminglogic
Last active December 19, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swarminglogic/5887160 to your computer and use it in GitHub Desktop.
Save swarminglogic/5887160 to your computer and use it in GitHub Desktop.
Script to add and configure a virtual host to apache2 server. PS: Requires that /etc/apache2/ports.conf contains the lines "#ADD NAMEVIRTUALHOST" and "#ADD LISTEN" PPS: Rough script, but it gets the job done.
#!/bin/bash
#
# Modifies /etc/apache2/ports.conf to listen to specified port-number ($2)
# Adds configuration file to /etc/apache2/sites-available/$1.conf
# Creates symbolink to the above file in /etc/apache2/sites-enabled/$1.conf
# Restarts apache server.
#
# Note: This is a very rough script, and only used to automate a process.
# Not intended for production.
#
# Note2: Requires that /etc/apache2/ports.conf contains the lines "#ADD NAMEVIRTUALHOST" and "#ADD LISTEN"
# Which specifies where it should insert the lines.
#
# example:
# ./createwebfolder www_test 42312
# gnome-open http://localhost:42312
#
#
if [ $# -ne 2 ] ; then
echo "Not enough parameters: ./createweb folder port-number"
exit
fi
folder=$1
portno=$2
sudo mkdir /var/www/$folder
sudo chown www-data /var/www/$folder
sudo chmod 777 /var/www/$folder
targetName="#ADD NAMEVIRTUALHOST"
addName="NameVirtualHost *:"$portno
targetListen="#ADD LISTEN"
addListen="Listen *:"$portno
cmnd1=`echo sudo perl -p -i -e "'s,"$targetName",# "$folder"\n"$addName"\n"$targetName",g'" /etc/apache2/ports.conf`
cmnd2=`echo sudo perl -p -i -e "'s,"$targetListen",# "$folder"\n"$addListen"\n"$targetListen",g'" /etc/apache2/ports.conf`
eval $cmnd1
eval $cmnd2
conffile=/etc/apache2/sites-available/${folder}.conf
if [ -e /etc/apache2/sites-available/${folder}.conf ] ; then
echo "Config file already exists: "$conffile
exit
fi
echo '<VirtualHost *:'$portno'>
ServerAdmin webmaster@localhost
DocumentRoot /var/'$folder'
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/'$folder'>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>' > /tmp/.tempcreatewebfile
sudo cp /tmp/.tempcreatewebfile $conffile
rm /tmp/.tempcreatewebfile
cmnd2=`echo "sudo ln -s $conffile /etc/apache2/sites-enabled/${folder}.conf"`
eval $cmnd2
echo "Restarting apache server"
sudo service apache2 restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment