Auto Deploy Laravel application using git on AWS Ubuntu Server

[norebro_social_networks icon_layout=”boxed”]

Hi everyone today we are going to see how we can deploy a new laravel application using Git on AWS. Assuming you have a AWS EC2 instance running on Ubuntu OS with installation of LAMP stack totally. If not please check our previous tutorial about how to install LAMP stack on AWS Ubuntu 14.04.

Any developer can’t be a successful web developer without knowing some sort of deployment procedure of the application which is developed into production environment using some tools like FTP or any code repository mechanisms like SVN, Git etc.

There are many ways to deploy our developed application into the production server. Each one of them has its own pros and cons, and each one starts with setting up environment and finishing up the in the same way. But what is important is what happens in between this whole deployment procedure and how we are doing it.

You can use FileZilla to transfer files from our local system repository to live production server. But it’s not a good way when to transfer a large number of files.

For the same application if we use Git. We can track every change developed by a developer who is working on this application and to deploy application we just have to use one command git clone repository URL.

Let’s see how we can deploy a Laravel application using Git in different ways. Login into live production server using SSH command like below.

ssh -p 22 username@hostname or ssh -p 22 username@ipaddress

Once logged in successfully traverse to the directory path like below.

cd /var/www/html

Git Manual Deploy

Let’s assume we have a working laravel project in our localhost and we want to deploy that into our live production server. Before you going to deploy the application into live server we need to have a repository created for this application in Git. If you don’t have any repository let’s create it using bitbucket and git.

Once we created the repository using Bitbucket, we have to clone the repository into our localhost and then we have to add the application code to the repository using Git.

We have to follow step by step exactly like below. If it’s the first time then we have to set up git config parameters with bitbucket login credentials.

git config --global "John Doe"
git config --global

Then we have to clone the empty repository using below command.

git clone

Once we cloned the empty repository successfully we have to add our code files to this repository using below command.

git add .

It will add all kinds of files once we are done adding, we have to commit them into the repository using this command.

git commit --m "comment for future sue"

Once we committed the files we have to save our change to reflect our files into the repository using this command.

git push origin master

Once we pushed the code, we are ready to pull our code in the live server. Now we have to pull code using below command in live production server.

git pull origin master

Once we pulled the whole code it’s time to do some necessary changes in our live production server code like database configuration and URL configuration in laravel config files if necessary.

To remove public from the application URL path we just have to point our virtual server path to the public folder and we have to run our composer using below command to install dependency packages.

composer install

Suppose if you don’t have any pre-developed laravel application you can create a new laravel application using composer command and then follow above steps.

composer create laravel/laravel projectname

This is one way of deploying our Laravel web applications in laravel. But we can deploy our application without even logging into live production server using git hooks.

Git Hooks Auto Deploy

Lets setup our server first for this kind of deployment. Let’s assume the server paths.

Our server live directory: /var/www/html/

Our server repository: /var/www/repo/site.git

Let’s create our repository by logging into live production server using SSH and then using below commands.

cd /var/www

mkdir repo && cd repo

mkdir site.git && cd site.git

git init --bare

–bare means that our folder doesn’t have any code files, just version control files.

Git repositories have a folder called ‘hooks’. This folder contains some sample files for possible actions that you can hook and perform custom actions set by you. Git defines three possible server hooks: pre-receive, post-receive and updatepre-receive is executed as soon as the server receives a push, update is similar but it executes once for each branch, and post-receive is executed when a push is completely finished and it’s one we are interested in.
You will see a few files and folders, including the hooks folder. So let’s go to hooks folder:

cd hooks

Now, create the file post-receive by typing:

cat  > post-receive

When you execute this command, you will have a blank line indicating that everything you type will be saved to this file. So lets type:


git --work-tree =/var/www/html/ --git-dir=/var/www/repo/site.git checkout -f

When you finish typing, press control-d to save. In order to execute the file, we need to set the proper permission using:

chmod +x post-receive

The post-receive file will be looked into everytime a push is completed and it’s saying that our files need to be in /var/www/html/

Git Auto Deploy using Laravel

Once the laravel installation done in the local system. We need to add our server credentials in our /app/config/remote.php file.

'connections' => array(

        'production' => array(
            'host'      => '',
            'username'  => 'username',
            'password'  => '*********',
            'key'       => '',
            'keyphrase' => '',
            'root'      => '/var/www/html',


We just have to commit all the changes which we like to add to present git repository using above commands in the first method of deploying. Now we will see deployment workflow easy, efficient, controllable and customizable when we do auto deploy.

Let’s begin by creating a simple route that reference to a controller. We need to write route in the routes.php file.

Route::get('/deploy', 'Server@deploy');

Whenever we visit http://localhost/deploy the public function deploy in the Server controller will be executed. Now let’s create the controller which we referenced in the previous route.

class ServerControlelr extends BaseController

       public function deploy() { }

We need to save this as /app/controllers/Server.php file. Now we have to insert the SSH facade to access the production remote configurations we set up earlier.


Now the run() function accepts two arguments. The first one accepts an array of terminal commands we want to run when we execute our deploy() function.

    'cd ~/var/www/html/',
    'git pull origin master'

The second argument is a function which will handle the feedback, we are going to receive from the server.

    'cd ~/var/www/html/',
    'git pull origin master'
), function($line) {
     echo $line.PHP_EOL; // outputs server feedback

Now, whenever we want to deploy our website all we have to do is run and we are done deploying into the live server.

There will be a couple of security breaches if we add Server.php file also to git so what we have to do is, we have to ignore some files using .gitignore. So let’s create a new file under /app/controllers with name .gitignore add the following line of text to the file and save it.


Another thing is we have to check whether Server.php exists or not before we run our route to deploy, so let’s rewrite our route for this case.

if (file_exists(__DIR__.'/controllers/Server.php')) {
    Route::get('/deploy', 'Server@deploy');

Now we are able to deploy easily compared to the first method and second method. Just pick which ever method you like to do. But best one is auto deploy using Laravel framework.

Thanks for reading this tutorial

Comments to: Auto Deploy Laravel application using git on AWS Ubuntu Server

    Your email address will not be published. Required fields are marked *

    Attach images - Only PNG, JPG, JPEG and GIF are supported.

    Stay connected




    Recent Comments

      Recent Posts

      Recent Comments