Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Bash script that creates a new project and virtual host for that project. Can also be used to quickly create a new Laravel project.
# This script creates a new project (or site) under /var/sites and creates
# new virtual host for that site. With the options a site can also
# install the latest version of Laravel directly.
# This script was originally based on the following script by @Nek from
# Coderwall:
# Display the usage information of the command.
create-project-usage() {
cat <<"USAGE"
Usage: create-project [OPTIONS] <name>
-h, --help Show this help screen
-u, --url Specify a local address, default is
-r, --remove Remove a Virtual Host
-e, --email Email of the administrator in the virtual host file
--list List the current virtual host
-l, --laravel Create a new Laravel project
create-project foo
create-project --laravel foo
create-project --remove foo
exit 0
# Remove a project and its Virtual Host.
project-remove() {
sudo -v
echo "Removing $url from /etc/hosts."
sudo sed -i '/'$url'/d' /etc/hosts
echo "Disabling and deleting the $name virtual host."
sudo a2dissite $name
sudo rm /etc/apache2/sites-available/$name
sudo service apache2 reload
echo "Project has been removed. The document root still exists."
exit 0
# List the available and enabled virtual hosts.
project-list() {
echo "Available virtual hosts:"
ls -l /etc/apache2/sites-available/
echo "Enabled virtual hosts:"
ls -l /etc/apache2/sites-enabled/
exit 0
# Define and create default values.
# Loop to read options and arguments.
while [ $1 ]; do
case "$1" in
# If we are not creating a Laravel project then the document root will be
# htdocs, otherwise it will be public.
if [ "$laravel" = 0 ]; then
# Check if the docroot exists, if it does not exist then we'll create it.
if [ ! -d "$docroot" ]; then
echo "Creating $docroot directory..."
mkdir -p $docroot
# If creating a Laravel project then we'll use composer to create the
# new project in the document root.
if [ "$laravel" = 1 ]; then
echo -e "Installing latest version of Laravel...\n"
composer create-project --keep-vcs laravel/laravel $docroot
echo -e "\nCreating the new $name Virtual Host with DocumentRoot: $docroot"
sudo cp /etc/apache2/sites-available/template /etc/apache2/sites-available/$name
sudo sed -i 's/'$email'/g' /etc/apache2/sites-available/$name
sudo sed -i 's/template.url/'$url'/g' /etc/apache2/sites-available/$name
sudo sed -i 's#template.docroot#'$docroot'#g' /etc/apache2/sites-available/$name
echo "Adding $url to the /etc/hosts file..."
sudo sed -i '1s/^/ '$url'\n/' /etc/hosts
sudo a2ensite $name
sudo service apache2 reload
echo -e "\nYou can now browse to your Virtual Host at http://$url"
exit 0
<VirtualHost *:80>
ServerName template.url
DocumentRoot template.docroot
<Directory />
Options FollowSymLinks
AllowOverride All
<Directory template.docroot/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from All
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel debug
CustomLog ${APACHE_LOG_DIR}/access.log combined

This comment has been minimized.

Copy link
Owner Author

@jasonlewis jasonlewis commented Aug 21, 2013

This is a shell script that creates a new Virtual Host for a given name without having to go through the entire process.


Download the script to /usr/local/bin and the template to /etc/apache2/sites-available.

$ cd /usr/local/bin && sudo wget
$ cd /etc/apache2/sites-available && sudo wget

You can then rename and chmod the file.

sudo mv /usr/local/bin/ /usr/local/bin/create-project && sudo chmod +x /usr/local/bin/create-project


To see usage instructions simply run create-project --help.


This comment has been minimized.

Copy link

@JasonMortonNZ JasonMortonNZ commented Aug 23, 2013

I've followed your instructions for installation, but I'm getting "zsh: command not found: create-project" when I try running "create-project --help". Any idea's why this might be?


This comment has been minimized.

Copy link
Owner Author

@jasonlewis jasonlewis commented Aug 26, 2013

@JasonMortonNZ Sorry for the delayed reply. You probably need to chmod the file. Will update the instructions.

sudo chmod +x create-project

This comment has been minimized.

Copy link

@mezigh mezigh commented Aug 28, 2013

Hi it's been 2 days that am struggling with the script i just discover that my apache config wants that
the file in /etc/apache2/sites-available needs to ends with .conf otherwise i can't enable the site.
Now when i lauch the script every thing is allright except that 403 ERROR . :-( . What's really drive me nuts is i have the same configuration.


This comment has been minimized.

Copy link

@mezigh mezigh commented Sep 5, 2013

Hi just some precisions about the 403 error.
Be sure to work on a true install of linux. In my case i have an Ubuntu Gnome with wubi. My HD is in NTFS format
and both NTFS and Fat32 doesn't handle all those user/groups permissions.

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