KodeInfo | Learning resources for web and mobile development

Auto Deploy Laravel application using git on AWS Ubuntu Server

December 17th, 2014 10:03:16 by Imran Iqbal Comments(0) - Views(11673)

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 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 user.name "John Doe"
$ git config --global user.email johndoe@example.com

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

git clone https://username@bitbucket.org/applicationname.git

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 done adding, we have to commit them into 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 repository using this command.

git push origin master

Once we pushed the code, we are ready to pull our code in 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 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.

creating laravel project using 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/domain.com

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

Lets 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.

In our repository if you type:

ls

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:

#!/bin/sh

git --work-tree =/var/www/html/domain.com --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/domain.com.

Git Auto Deploy using Laravel

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

'connections' => array(

		'production' => array(
			'host'      => 'domain.com',
			'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 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.

SSH::into('production')->run();

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

SSH::into('production')->run(array(
    'cd ~/var/www/html/domain.com',
    'git pull origin master'
));

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

SSH::into('production')->run(array(
    'cd ~/var/www/html/domain.com',
    '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 http://domain.com/deploy and we are done deploying into 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 new file under /app/controllers with name .gitignore add the following line of text to the file and save it.

Server.php

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 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

Kode Info 

Author

  • Imran Iqbal
    Imran Iqbal

    Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Related

WHY USE A FRAMEWORK OVER PLAIN PHP

WHY USE A FRAMEWORK OVER PLAIN PHP
read more

GETTING STARTED WITH LARAVEL

GETTING STARTED WITH LARAVEL
read more

UNDERSTANDING LARAVEL STRUCTURE

UNDERSTANDING LARAVEL STRUCTURE
read more

UNDERSTANDING LARAVEL ROUTES

UNDERSTANDING LARAVEL ROUTES
read more

comments powered by Disqus