Skip to content

Instantly share code, notes, and snippets.

@AhmedHelalAhmed
Last active July 18, 2018 07:50
Show Gist options
  • Save AhmedHelalAhmed/e9ac4ff67ddd869fd7ef2310f909fc21 to your computer and use it in GitHub Desktop.
Save AhmedHelalAhmed/e9ac4ff67ddd869fd7ef2310f909fc21 to your computer and use it in GitHub Desktop.
Bash script to make the virtual host for Ubuntu and apache2 with document root the current path
#! /bin/bash
vh_name=$1;
curent_path=`pwd`
file_name_path='/tmp/'$vh_name'.conf'
#to generate the conf file in /tmp
printf "<VirtualHost *:80>\n" >/tmp/$vh_name.conf
printf " ServerAlias ${vh_name}\n" >>/tmp/$vh_name.conf
printf " ServerName ${vh_name}\n" >>/tmp/$vh_name.conf
printf " DocumentRoot ${curent_path}\n" >>/tmp/$vh_name.conf
printf " <Directory ${curent_path}>\n" >>/tmp/$vh_name.conf
printf " Options Indexes FollowSymLinks\n">>/tmp/$vh_name.conf
printf " Require all granted\n">>/tmp/$vh_name.conf
printf " AllowOverride all\n">>/tmp/$vh_name.conf
printf " Allow from All\n">>/tmp/$vh_name.conf
printf " </Directory>\n">>/tmp/$vh_name.conf
printf " ErrorLog \${APACHE_LOG_DIR}/error.log\n">>/tmp/$vh_name.conf
printf " CustomLog \${APACHE_LOG_DIR}/access.log combined\n">>/tmp/$vh_name.conf
printf "</VirtualHost>\n">>/tmp/$vh_name.conf
sudo cp $file_name_path /etc/apache2/sites-available/$vh_name.conf
rm $file_name_path
sudo a2ensite $vh_name.conf
(sudo echo "127.0.0.1 "${vh_name} && sudo cat /etc/hosts) > /tmp/hosts && sudo mv /tmp/hosts /etc/hosts
sudo systemctl restart apache2
@AhmedHelalAhmed
Copy link
Author

AhmedHelalAhmed commented Jul 17, 2018

Put the script in

/usr/local/bin

A command to make forum.local virtual host
vh "forum.local"

Note
you must be on the path that you want it to be the document root
use cd to your document path then execute the command

@mohamed-ramdan
Copy link

keep up the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment