Skip to content

Instantly share code, notes, and snippets.

@gmolop
Forked from jasonlewis/create-project.sh
Last active October 24, 2019 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmolop/0e56ab078b719bae6a4a8b6edcb9657d to your computer and use it in GitHub Desktop.
Save gmolop/0e56ab078b719bae6a4a8b6edcb9657d to your computer and use it in GitHub Desktop.
Bash script that creates a new project and virtual host for that project. Can also be used to quickly create a new Laravel project.
#!/bin/bash
# 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: https://coderwall.com/p/cqoplg
# Display the usage information of the command.
createProjectUsage() {
cat <<"USAGE"
Usage: create-project [OPTIONS] <name>
-h, --help Show this help screen
-u, --url Specify a local address, default is http://name.local
-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
Examples:
create-project foo
create-project --laravel foo
create-project --remove foo
USAGE
exit 0
}
# Remove a project and its Virtual Host.
projectRemove() {
sudo -v
echo "Removing $url from /etc/hosts."
sudo sed -i '/'$url'/d' /etc/hosts
sudo sed -i '/'$url'/d' /mnt/c/Windows/System32/drivers/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.
projectList() {
echo "Available virtual hosts:"
sudo ls -lash /etc/apache2/sites-available/
echo "Enabled virtual hosts:"
sudo ls -lash /etc/apache2/sites-enabled/
exit 0
}
# Check if template exist, download if not
getTemplate() {
sudo rm /etc/apache2/sites-available/template
sudo wget --no-cache -O '/etc/apache2/sites-available/template' https://gist.githubusercontent.com/gmolop/0e56ab078b719bae6a4a8b6edcb9657d/raw/template
}
# Define and create default values.
# name="${!#}"
name="$1"
email="webmaster@localhost"
url="$name.local"
docroot="/var/www/devroot/$name"
laravel=0
# Loop to read options and arguments.
while [ $1 ]; do
case "$1-DISABLED" in
'--list')
projectList;;
'--help'|'-h')
createProjectUsage;;
'--remove'|'-r')
url="$2"
projectRemove;;
'--url'|'-u')
url="$2";;
'--email'|'-e')
email="$2";;
'--laravel')
laravel=1;;
esac
shift
done
# If we are not creating a Laravel project then the document root will be
# htdocs, otherwise it will be public.
if [ "$laravel" = 0 ]; then
docroot="$docroot/htdocs"
fi
# Check if the docroot exists, if it does not exist then we'll create it.
if [ ! -d "$docroot" ]; then
echo "Creating $docroot directory..."
sudo mkdir -p $docroot
# create dummy index file
echo "<?php echo 'Works!'; ?>" | sudo tee $docroot/index.php
fi
# 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"
sudo composer create-project --keep-vcs laravel/laravel $docroot
docroot="$docroot/public"
fi
echo -e "\nCreating the new $name Virtual Host with DocumentRoot: $docroot"
getTemplate
sudo cp /etc/apache2/sites-available/template /etc/apache2/sites-available/$url.conf
sudo sed -i 's/template.email/'$email'/g' /etc/apache2/sites-available/$url.conf
sudo sed -i 's/template.url/'$url'/g' /etc/apache2/sites-available/$url.conf
sudo sed -i 's#template.docroot#'$docroot'#g' /etc/apache2/sites-available/$url.conf
echo "Adding $url to the /etc/hosts file..."
# sudo sed -i "1s/^/127.0.0.1 $url\n/" /etc/hosts
sudo echo "127.0.0.1 $url" >> /mnt/c/Windows/System32/drivers/etc/hosts
sudo a2ensite $url.conf
# Creating SSL certificate
sudo sh ~/generate-ssl.sh $url $2
sudo service apache2 restart
echo -e "\nYou can now browse to your Virtual Host at https://$url"
powershell.exe /c start https://$url
exit 0
<VirtualHost *:80>
ServerAdmin template.email
ServerName template.url
DocumentRoot template.docroot
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory template.docroot/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel debug
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerAdmin template.email
ServerName template.url
DocumentRoot template.docroot
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory template.docroot/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel debug
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/template.url.crt
SSLCertificateKeyFile /etc/apache2/ssl/template.url.key
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment