Skip to content

Instantly share code, notes, and snippets.

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 masudiiuc/6731454 to your computer and use it in GitHub Desktop.
Save masudiiuc/6731454 to your computer and use it in GitHub Desktop.
Creating Virtual host with a single shell script
#!/bin/bash
#---------------------------------------------------------------
# Create a virtual host automatically with host name and
# document root send by parameter
#
# SYNTAX:
# sudo script_name HOST_NAME DOCUMENT_ROOT
#
# EXAMPLE:
# sudo ./create_virtual_host www.dev-host.local symfony/web
#---------------------------------------------------------------
echo -e "\n\nCreating Virtual Host '$1' with document root '$2'\n"
#Set Document Root
DOC_ROOT="/Users/Masud/Sites/personal/$2"
#set Vhost Directory
VHOST_DIR="/Users/Masud/Sites/testVhost"
#set Vhost Name
VHOST_NAME="$1"
#Virtual host content
text="<VirtualHost *:80>\n
\tServerName $VHOST_NAME \n
\tDocumentRoot '$DOC_ROOT'\n
\t<Directory '$DOC_ROT'>\n
\t\tOptions Indexes FollowSymLinks MultiViews\n
\t\tAllowOverride All\n
\t\tAllow from All\n
\t</Directory>\n
</VirtualHost>"
# Here is the step for creating Virtual host finally
# 1. Go to Vhost Directory
# 2. create new Vhost configuration file
# 3. write in to new Vhost file
echo -e "1. Creating New Host File"
cd $VHOST_DIR && touch test.conf && echo -e $text > test.conf
# add this new Vhost to /etc/hosts file
# >> indicates the new host name will be added at the end of file
echo -e "2. Adding new Host to /etc/hosts File"
echo -e "127.0.0.1\t$VHOST_NAME" >> /etc/hosts
echo -e "2. Done!\n"
@masudiiuc
Copy link
Author

Sample command for executing this script:

sudo ./cvhost.sh wwww.laravel-test.local laravel-test/public

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