Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@IgorDePaula
Created October 8, 2014 22:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IgorDePaula/b786ee388ef7b6460eb3 to your computer and use it in GitHub Desktop.
Save IgorDePaula/b786ee388ef7b6460eb3 to your computer and use it in GitHub Desktop.
#!/bin/bash
###Checking for user
if [ "$(whoami)" != 'root' ]; then
echo "You have no permission to run $0 as non-root user. Use sudo !!!"
exit 1
fi
###Configure emil and vhost dir
email='igordepaula@adminweb.com.br'
vhroot='/etc/apache2/sites-available'
iserror='no'
hosterror=''
direrror=''
#Take inputs host name and root directory
echo "Please provide hostname. e.g.dev,staging"
read hostname
#echo -e "Please provide web root directory. e.g. /websites/dev, e.g. /websites/stagin"
#read rootdir
rootdir="/home/"$hostname
### Add user with your home directory
useradd -m -s /bin/false $hostname
passwd $hostname
mkdir /home/$hostname/public_html
chmod -R 777 /home/$hostname/public_html
###check inputs
if [ -z "$hostname" ]; then
iserror="yes"
hosterror="Please provide domain name"
fi
if [ -z "$rootdir" ]; then
iserror="yes"
direrror="Please provide web root directory name"
fi
###check vhether hostname already exists
if [ -e $vhroot"/"$hostname ]; then
iserror="yes"
hosterror="Hostname already exists provide another hostname"
fi
### check if directory exists or not
if ! [ -d $rootdir ]; then
iserror="yes"
direrror="Directory provide does not exists"
fi
if [ "$iserror" = "yes" ]; then
echo "Please correct following errors:"
if [ -z "$hosterror" ]; then
echo "$hosterror"
fi
if [ -z "$direrror" ]; then
echo "$direrror"
fi
exit;
fi
if ! touch $vhroot/$hostname
then
echo "ERROR: "$vhroot"/"$hostname" could not be created"
else
echo "Virtual host document root created in "$vhroot"/"$hostname
fi
if ! echo "<VirtualHost *:80>
ServerAdmin $email
ServerName $hostname
ServerAlias $hostname www.$hostname
Options -Indexes
DocumentRoot $rootdir/public_html
DirectoryIndex index.php index.html
<Directory />
AllowOverride All
</Directory>
<Directory $rootdir/public_html>
AllowOverride All
</Directory>
ErrorLog /var/log/apache2/$hostname
LogLevel error
CustomLog /var/log/apache2/$hostname custom
</VirtualHost>" > $vhroot/$hostname
then
echo "ERROR: the virtual host could not be added"
else
echo "New virtual host added to the Apache vhost file"
fi
### Add hostname in /etc/hosts
if ! echo "127.0.0.1 $hostname" >> /etc/hosts
then
echo "ERROR: Not able write in /etc/hosts"
else
echo "Host added to /etc/hosts file"
fi
### enable website
a2ensite $hostname
### restart apache
/etc/init.d/apache2 restart
chmod 755 $rootdir
if ! touch $rootdir/public_html/phpinfo.php
then
echo "ERROR: "$rootdir"/public_html/phpinfo.php could not be created"
else
echo ""$rootdir"/public_html/phpinfo.php created"
fi
if ! echo "<?php echo phpinfo();" > $rootdir/public_html/phpinfo.php
then
echo "ERROR: not able to write in file "$rootdir"/public_html/phpinfo.php. Please check the permissions"
else
echo "Added content to "$rootdir"/public_html/phpinfo.php"
fi
echo "Complete! The new virtual host has been created.
To check the funcionality browse http://"$hostname"/phpinfo.php
Document root is $vhroot"/"$hostname""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment