Last active
May 12, 2020 12:34
-
-
Save jamesdube/b526928afe6ed2f4e935 to your computer and use it in GitHub Desktop.
Automatically create Virtual Host in apache
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 | |
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