Created
March 11, 2010 19:09
-
-
Save freeatnet/329530 to your computer and use it in GitHub Desktop.
A simple scripts that takes away the pain of creating VirtualHosts on my home Ubuntu server w/ Apache2
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 | |
# A simple scripts that takes away the pain of creating VirtualHosts on my home Ubuntu server w/ Apache2 | |
# by Arseniy Ivanov, freeatnet.com | |
WWW_ROOT='/var/www/' | |
SITE_CONFS='/etc/apache2/sites-available/' | |
host_name='' | |
echo -n 'Enter host name: ' | |
read host_name | |
echo -n 'Determining DocumentRoot path for this hostname' | |
previous_path='' | |
path="$host_name" | |
while [ "$path" != "$previous_path" ] | |
do | |
echo -n '.' | |
previous_path="$path" | |
path=`echo "$path" | sed -r 's/([a-z0-9\-]+)\.(([a-z0-9\-]+\.)*([a-z0-9\-]+\.[a-z]+))((\/?(.*))*)$/\2\/\1\5/'` | |
done; | |
echo "" | |
echo -n "Path is $path, is that correct? " | |
ans='' | |
read ans | |
if [ $ans != "y" ] && [ $ans != "Y" ]; | |
then | |
echo -n "Enter the correct path: " | |
read path | |
fi | |
echo "Using path $WWW_ROOT$path as DocumentRoot." | |
mkdir -p $WWW_ROOT$path | |
if [ $? -ne 0 ] | |
then | |
echo "Failed to create DocumentRoot path, exiting" | |
exit | |
else | |
if [ -d $WWW_ROOT$path ] | |
then | |
echo "We're good, folder at $WWW_ROOT$path exists." | |
else | |
echo "Weird, mkdir returned 0, but the folder is not there." | |
exit | |
fi | |
fi | |
echo -n "Setting permissions on DocumentRoot: " | |
chmod 755 $WWW_ROOT$path && chown www-data:www-data $WWW_ROOT$path | |
if [ $? -ne 0 ] | |
then | |
echo "" | |
echo "Could not set correct permissions for some reason." | |
exit | |
else | |
echo "Done." | |
fi | |
echo -n "Creating and setting permissions on log folder: " | |
mkdir -p $WWW_ROOT'/logs/'$host_name \ | |
&& chmod 644 $WWW_ROOT'/logs/'$host_name \ | |
&& chown www-data:www-data $WWW_ROOT'/logs/'$host_name | |
if [ $? -ne 0 ] | |
then | |
echo "Something failed" | |
exit | |
else | |
echo "Ok." | |
fi | |
echo -n "Creating web site config: " | |
if [ ! -f $SITE_CONFS$host_name ] | |
then | |
touch $SITE_CONFS$host_name | |
else | |
echo "" | |
echo "File $SITE_CONFS$host_name already exists!" | |
exit | |
fi | |
echo "<VirtualHost *:80> | |
ServerAdmin webmaster@$host_name | |
ServerName $host_name | |
#ServerAlias $host_name | |
DirectoryIndex index.php index.html | |
DocumentRoot $WWW_ROOT$path/ | |
# Logfiles | |
LogLevel warn | |
ErrorLog $WWW_ROOT/logs/$host_name/error.log | |
CustomLog $WWW_ROOT/logs/$host_name/access.log combined | |
</VirtualHost> | |
" > $SITE_CONFS$host_name | |
echo "Done." | |
echo -n "Enabling site..." | |
a2ensite $host_name | |
if [ $? -ne 0 ] | |
then | |
echo "" | |
echo "Failed to enable site!" | |
else | |
echo "Ok." | |
fi | |
echo -n "Reloading Apache..." | |
/etc/init.d/apache2 reload | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment