Skip to content

Instantly share code, notes, and snippets.

@jamesdube
Last active May 12, 2020 12:34
Show Gist options
  • Save jamesdube/b526928afe6ed2f4e935 to your computer and use it in GitHub Desktop.
Save jamesdube/b526928afe6ed2f4e935 to your computer and use it in GitHub Desktop.
Automatically create Virtual Host in apache
#!/bin/bash
echo "Enter the VirtualHost name : "
read name
echo "Enter the VirtualHost root [/var/www/html/] : "
read root
if [ -d "/var/www/html/"$root ]
then
if [ -w /etc/hosts ]
echo "creating apache virtial host..."
then
if [ -w /etc/apache2/sites-available ]
then
printf "<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/$root
ServerName www.$name.dev
ServerAlias $name.dev
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>" >> /etc/apache2/sites-available/$name.conf
echo "adding site to hosts file..."
printf "127.0.0.1 $name.dev\n" >> /etc/hosts
sudo a2ensite "$name.conf"
sudo service apache2 restart
google-chrome "$name.dev"
else
echo "You do not have pemission to write to the apache folder , try using sudo."
fi
else
echo "You do not have pemission to write to the $root folder , try using sudo."
fi
else
echo "Directory $root does not exist!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment