Bash Tool for Laravel Developers

george udosen
7 min readSep 24, 2018

--

Now I often create sites using frameworks like Laravel and Angular and I relish the fact that I can just open up a terminal [ which by the way is my preferred way of doing things ] and type laravel new app_name and after a few seconds or minutes [ as is the case in Nigeria due to slow internet speeds ] I get a Laravel app ready to work on, well that’s DevOps as I have come to know it.

After that I have to do several things to get my Laravel application to sit right so I can see it in my browser and that will involve creating a virtualhost in Apache server, basically this would involve these steps if done manually:

  1. Create new laravel application in /var/www/html folder for Apache to access,
  2. Create a virtualhost file in /etc/apache2/sites-available,
  3. Enable that virtualhost file with the Apache command:
  • sudo a2ensite site_conf

4. Then finally add an entry in the /etc/hosts file so that I can simply do example.local in my browser and my laravel application will show.

Doing this every time can become a hazard as errors can be easily made while typing in new information in any of the above steps. So I decided to use a bash script to achieve the same thing with little typing involved but before that I am assuming several things:

  1. Laravel is installed and configured to run without sudo, if run with sudo then the line(s) in the script that involves laravel application can have sudo prefixed to it and you will simply enter your sudo password when asked, and
  2. Apache server is installed and running and that your server root for sites is at /var/www/html

Now here is that script I spoke of:

#!/usr/bin/env bash# This stops the script if any of the commands fails
set -e
set -u
###############################################################################
# Script to help setup a laravel site on Ubuntu Xenial Xepus #
# requires you pass in 1. name of site, example somesite.[a-z]{3} #
# it will be install to /var/www/html, and the Virtualhost file will #
# be setup in /etc/apache2/sites-available with the name of your choice, #
# and activated with `sudo a2ensite /etc/apache2/somesite.conf` #
# Apache will be restarted with `sudo systemctl restart apache2` #
###############################################################################
# Get the name of the laravel site and the Virtualhost config file name
echo -e "\e[0;34m Enter laravel site name to create \e[0m"
read -r site
# check the name is an alphabets or not-empty
if [[ -n "$site" ]]
then
if [[ "$site" =~ ^[a-zA-Z]+$ ]]
then
# convert site name to lower case letters
site=$(echo "$site" | tr '[:upper:]' '[:lower:]')
#echo "$site"
# get the Virtualhost conf file name
echo -e "\e[0;34m Enter the name of the apache virtualhost file to create \e[0m"
read -r apconf
if [[ -n "$apconf" ]]
then
#echo "Entry not empty! $apconf.conf"
if [[ "$apconf" =~ ^[[:alnum:]][-[:alnum:]]{0,61}[[:alnum:]]$ ]]
then
# convert all letters to lower case
apconf=$(echo "$apconf" | tr '[:upper:]' '[:lower:]')
echo -e "\033[0;37m Name entered for config file \033[0m \033[0;36m $apconf \033[0m"
apconf="$apconf.conf"
echo -e "\e[0;37m Config file name:\e[0m \e[0;36m $apconf \e[0m"
# Delay the next command so user will see echo output
sleep 5

else
echo -e "\e[0;31m Name can only contain alphanumeric! \e[0m"
exit 1
fi
else
echo -e "\e[0;31m Nothing was entered, please enter a name! \e[0m"
exit 1
fi
else
echo -e "\e[0;31m Only alphabets allowed! \e[0m"
exit 1
fi
else
echo -e "\e[0;31m Nothing was entered at terminal! \e[0m"
exit 1
fi
# Create laravel site
echo -e "\e[0;37m Change into '\e[0;36m /var/www/html \e[0m' directory \e[0m"
cd /var/www/html
echo ""
echo -e "\e[0;37m Checking if laravel is installed... \e[0m"
which laravel
sleep 5
echo -e "\e[0;37m It is so we create new application! \e[0m"
echo ""
echo -e "\e[0;37m Begin laravel site creation... \e[0m"
laravel new "$site"
echo -e "\e[0;37m Finished laravel site creation... \e[0m"
echo ""
# Create site apache config file
echo -e "\e[0;37m Creaing $apconf in \e[0m \e[0;36m /etc/apache2/sites-available/ \e[0m"
sudo touch /etc/apache2/sites-available/"$apconf"
sleep 3
echo ""
# Write configuration into conf file
echo -e "\e[0;37m Writing to config file in \e[0m \e[0;36m/etc/apache2/sites-available/$apconf ...\e[0m"
sleep 5
echo ""
echo "<VirtualHost *:80>
ServerName $site.local
DocumentRoot /var/www/html/$site/public/
DirectoryIndex index.php
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/html/$site>
AllowOverride All
Require all granted
</Directory>
ErrorLog \${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog \${APACHE_LOG_DIR}/access.log combined
</VirtualHost>" | sudo tee /etc/apache2/sites-available/"$apconf"
echo ""
echo -e "\e[0;37m Finished writing config file\e[0m"
# Write name of laravel application in /etc/hosts
echo ""
echo -e "\e[0;37m Writing in \e[0m \e[0;36m /etc/hosts file ...\e[0m"
# Find a blank space and add the new entry
sudo sed -i "s/^[[:space:]]*$/127.0.0.1 $site.local\n/" /etc/hosts
sleep 5
echo -e "\e[0;37m Finished writing to \e[0m \e[0;36m /etc/hosts ...\e[0m"
echo ""
#Changing ownership on laravel file
echo -e "\e[0;37m Changing owner of laravel application file \e[0m"
sudo chown -R "$USER":www-data /var/www/html/"$site"
sleep 3
echo ""
echo -e "\e[0;31m Make the Laravel storage folder accessible with permission 775!\e[0m"
sudo chmod -R 775 "$site"/storage
sleep 5
echo " "
echo -e "\e[0;31m Please add yourself to the www-data (apache group) if not in that group already!\e[0m"
echo -e "\e[0;31m do that with the command \e[0m \e[0;36m 'sudo usermod -a -G www-data' $USER \e[0m"
echo ""
# Activate the config file in apache
echo -e "\e[0;34m Enabling site...\e[0m"
sudo a2ensite "$apconf"
sleep 5
echo ""
echo -e "\e[0;34m Restarting apache... \e[0m"
sudo systemctl restart apache2 || echo "apache wasn't restart please do so manually!"
sleep 5
echo ""
echo "Finished"
exit 0

Now let’s see how it is used:

No extension is used for the config file as the apache default is used “.conf” when the tool creates that file, that of course can be changed.

Now in the same vein we can remove that site folder and all the configurations added using another bash tool I created, and here is the code:

#!/usr/bin/env bash

#############################################################
# Bash tool to remove previously created Laravel site #
# #
#############################################################

# This stops the script if any error occurrs
set -e
set -u

# Disable site function
disable() {
# Find conf file for site
echo -e "\e[0;31m Checking for config file to be removed ... \e[0m"
echo " "
echo -e "\033[0;37mConfig file to be removed \033[0m \033[0;36m $fRemove \033[0m"
echo " "
sleep 4

echo -e "\e[0;31m Removing site config file ... \e[0m"
# Remove file from /etc/apache2/sites-available
sudo rm -rf "$fRemove"
sleep 5
echo " "
echo -e "\e[0;31m Removing site entry from /etc/hosts ... \e[0m"
sudo sed -i.old "/^127.0.0.1\s*$1.local/d" /etc/hosts
sleep 4
echo " "
echo -e "\033[0;34m Moving /etc/hosts.old to /tmp folder .. \033[0m"
sudo mv /etc/hosts.old /tmp
sleep 4
echo " "
echo -e "\e[0;31m A backup copy of the /etc/hosts file has been made! \e[0m"
echo -e "\e[0;31m called /etc/hosts.old \e[0m"
echo " "
echo -e "\e[0;31m Restarting Apache ... \e[0m"
sudo systemctl restart apache2 || echo -e "\e[0;31m Apache didn't restart please do so manually! \e[0m"
sleep 5
}

# Get the name of the site to remove
echo -e "\e[0;34m Enter name of laravel folder in /var/www/html to remove \e[0m"
read -r site
fRemove=$(grep -ERl "$site".local /etc/apache2/sites-enabled/)

echo " "
echo -e "\033[0;37m Site folder to remove \033[0m \033[0;36m $site \033[0m"

# Check if that folder exists
# in the /var/www/html folder
if [[ -n "$site" ]]
then
if [[ -d /var/www/html/"$site" ]]
then
echo -e "\e[0;34m Making a backup of the site folder in the \e[0m\e[0;31m/tmp folder\e[0m \e[0;34m ... \e[0m"
# Preserve folder attributes when copying
sudo cp -R --preserve /var/www/html/"$site" /tmp/"$site"
sleep 5
echo " "
echo -e "\033[0;31m Removing site folder from /var/www/html ... \033[0m \033[0;36m $site \033[0m"
sudo rm -rf /var/www/html/"$site"
sleep 5
echo " "
echo -e "\e[0;34m Disabling the site in Apache ... \e[0m"

# sudo a2dissite "$site"
if [[ ! $(sudo a2dissite "${fRemove##*/}") ]]
then
echo -e "\e[0;31m That site configuration file doesn't exist! \e[0m"
echo " "
echo -e "\e[0;31m Please enter the correct name for the config file ... \e[0m"
read -r newconf
if [[ ! -n "$newconf" ]]
then
echo -e "\e[0;31m Program with exit as no config file name was entered \e[0m"
echo -e "\e[0;34m Restoring site files ... \e[0m"
sudo cp -R --preserve /tmp/"$site" /var/www/html/
sleep 5
exit 1
else
# Try disabling site again from Apache
if [[ $(sudo a2dissite "$newconf") ]]
then
disable "$newconf"
else
echo -e "\e[0;31m Exiting script, site config not found ... \e[0m"
echo " "
exit 1
fi

fi
else
disable "$site"
fi
else
echo -e "\033[0;31m The specified folder\033[0m\033[0;36m $site \033[0m\033[0;31m doesn't exist in the /var/www/html folder! \033[0m"
exit 1
fi
else
echo -e "\e[0;31m No name was entered! \e[0m"
exit 1
fi

How does this work? Simply by reversing what was done by the previous command. It will also ask you for the config file name if it is different from the site folder name, usually I use the same name for both when creating my apache sites.

And this is how that is used:

Location

To get this tool to work from any where put those scripts in any of you system path locations, I place mine in my /home/$USER/bin folder whose path I had added to my .bashrc file. Now this makes it local to my user but you could also place it in a system wide location for easy reach for all users.

Summary

I made a tool [ which can be improved ] to setup my Laravel applications and remove them if and when needed using bash scripts. One thing to note is that the local application extension .app and even .local (used here) are not functional in my chrome browser. So the extension .local will only work in Firefox, I am aware that Google bought the domain .app but not sure about .local. Leave me a comment if you know why! Repo link https://github.com/udoyen/laravel-setup-helper-for-linux

--

--

george udosen
george udosen

Written by george udosen

DevOps | FullStack developer | Python::Flask | GCP Cloud Certified | AWS & AZURE Cloud Savy | Linux Sysadmin | Google IT Support

No responses yet