Skip to main content

Deploy Laravel 5.4 App on LEMP Stack (DigitalOcean Guide) Part - 3

This is a third part of the Deploy Laravel 5.4 App on LEMP Stack (DigitalOcean Guide). In this post, I'm going to prepare the server for Laravel application deployment.

Let's create a folder for our Laravel application. By default, NginX will look at the /var/www/ path to serve the files. In this folder let's create a folder called app.

Open the following file to update the new path. Run the following command in your terminal.

sudo nano /etc/nginx/sites-available/default

Let's change the root line and add the public folder path.


Change the following location section to add /index.php?$query_string;

location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }


Changed code section should look like this.


Restart the server to changes to take effect.

sudo service nginx restart

Install Composer


You can follow the official guide to install composer. In short, execute the following commands in your terminal.

cd ~
curl -sS https://getcomposer.org/installer | php


We have composer.phar in our home folder move it into our bin so we can use composer commands easier by just typing composer.

sudo mv composer.phar /usr/local/bin/composer

Type composer in your terminal and you will see the output of composer help files.


Install Git


Using git, you can push the source code to our server effortlessly with no downtime. You can use SFTP as well, but it is a slow and less secure approach. In this tutorial, I use git to upload the source code to the server.

Let's install Git in /var/repo/ folder. Run the following commands in your terminal.

cd /var
mkdir repo && cd repo

Execute the following commands. --bare flag is generally used on servers. A bare repo is a special kind of repo whose sole purpose is to receive pushes from developers.

mkdir site.git && cd site.git
git init --bare

Above commands creates the repository in the /var/repo/site.git file path.

Git Hooks


Git has a way to fire off custom scripts when certain actions occur. There are two groups of these hooks: client-side and server-side. Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations such as receiving pushed commits. In this tutorial, I'm going to focus on server-side hooks.

There are three types of hooks. They are pre-receive, post-receive, and update. You can learn more about them using this link

We focus on post-receive because we need to trigger it after the source code push. According to the documentation, post-receive hook runs after the entire process is completed and can be used to update other services or notify users.

To set up hooks go to the hooks folder in site.git.


You will see sample files inside the hooks/ folder. But in this tutorial, I'm going to create a new empty file called post-receive. Run the following command to create an empty file.

sudo nano post-receive

Add the following code to the file created. Save the file and exit.

#!/bin/sh
git --work-tree=/var/www/app --git-dir=/var/repo/site.git checkout -f



The post-receive file needs execution permissions in order to work. Run the following command to add execution permissions to the file. Please note that you are still in the same folder.

sudo chmod +x post-receive

Now you can exit from the SSH session. Because in the next step we are going to work on the local machine. You can use exit command to exit from the ssh session in the terminal. Next blog post will focus on how to set up local machine to push the production code.

Comments

Popular posts from this blog

Alternatives to the SQLite in Android

At the moment there are several embeddable databases and libraries out there that you can use in a mobile application. In this post, I examine most popular libraries and databases and highlight some of their characteristics. Realm Realm is a mobile platform and a replacement for SQLite & Core Data. According to the website, it has more than 100k active developers. Realm is fully open source and distributes under Apache License. Realm Mobile Database is much faster than an ORM, and often faster than raw SQLite due to zero-copy design. Some of the benefits of Realm are fast queries, safe threading, encryption of data and reactive architecture. You can learn more about Realm by visiting this page . Sugar ORM Sugar ORM is a library that can be used to interact with SQLite database using Object-Relational Mapping. Object-Relational Mapping (ORM) is a technique that used to query and manipulate data from a SQLite database using an object-oriented paradigm. And Sugar ORM tak

Laravel 5.4 Bootstrap row class for every 3 columns

Sometimes you need to show three or four columns in a row using Laravel 5.4 and blade template engine. Here I show how to do it using chunk function in Laravel blade. As the first step let's get products from the database and pass it to the blade view. And let's check our blade file. In the following example,  @ foreach  takes the  $ products array and chuck it. In this example, I split it by three. You can pass any number according to your specification. And you will see the inner @ foreach loop through  $ items. If you have any question please comment it and I will try to solve it.

Mobile Backend As A Service (BaaS) platform for Android

Many mobile apps and games rely on a backend service for things that can’t be done solely on hosted device, such as sharing and processing data from multiple users, or storing large files. Backend as a service (BaaS) provides a centralised database and other features to manage user-generated data and application data. The developer can link applications to the backend service and backend service expose APIs to manage users, integrate push notification and analytics. BaaS is a recent development of cloud computing technology. Using backend services provide developers functionalities to manage users, data and to analyse real-time changes actively. Backend service should be able to handle the offline case gracefully and minimise battery drain. Back in the day developers had to develop custom backend platforms using server-side technologies. And developers had to scale up and down according to the user base and app usage. It was time-consuming in terms of resources and skills. Most mob