Set up virtual host and integrate git with auto pull

Try Fast Read

Git With Auto Pull

This script will help to create a git repo and also a virtual host on your system. You can clone and commit to the repo, all the changes will be directly applied to the production server, i.e git with auto pull feature. This assumes that you have a user called git on your server whose home directory is /home/git. Also the script needs sudo permission to function.

How to run

sudo sh site_name


The Script

#git with auto pull
#check if parameter is sent, parameter should be the name of git repo and site you wanna create

if [ "$#" -eq 1 ]


echo "Ok , I am creating git repo $1.git"
cd /home/git
sudo mkdir $1.git

cd $1.git
#create bare git repo
sudo git init --bare
#configure the repo
sudo git --bare update-server-info

sudo git config core.bare false

sudo git config receive.denycurrentbranch ignore
#set the working tree, the place where the code is to be auto pushed
sudo git config core.worktree /var/www/$1

cd ..

sudo chown git:www-data $1.git -R
cd $1.git
#now create a post hook event for git repo, for auto pull
sudo echo "#!/bin/sh" |  tee hooks/post-receive

sudo echo "git checkout -f" |  tee -a hooks/post-receive

sudo chmod +x hooks/post-receive

#no parameter is passed, so exit the script
echo "Please ask what is the file name"


#now creating virtual host, assuming your webserver directory is /var/www clone the site first
cd /var/www/
git clone [email protected]:$1.git $1
cd $1
sudo chown git:www-data -R .
#add site to virtual host
cd /etc/apache2/sites-available/

echo "" | sudo tee $
echo "ServerAdmin [email protected]" | sudo tee -a $
echo "ServerName $ " | sudo tee -a $
echo "# ServerAlias www.$" | sudo tee -a $
echo "DocumentRoot /var/www/$1" | sudo tee -a $

echo "" | sudo tee -a $
echo "AllowOverride All" | sudo tee -a $
echo "" | sudo tee -a $

echo "" | sudo tee -a $
echo "AllowOverride All" | sudo tee -a $
echo "" | sudo tee -a $
echo "ErrorLog /var/log/apache2/error.$" | sudo tee -a $
echo "LogLevel error" | sudo tee -a $
echo "CustomLog /var/log/apache2/custom.$1.log custom" | sudo tee -a $
echo ""  | sudo tee -a $
#enable site and restart apache
sudo a2ensite $
sudo service apache2 reload

#Show info to add/clone repo also log the creation on gitLog folder
echo "Created git repo for $1.git"

echo "You can clone the repo using git clone [email protected]:$1.git"

echo "or you can add your codes to repo using git remote add origin [email protected]:$1.git"

echo "[email protected]:$1.git created on " >> gitLog $(date)
cd ~

Post navigation