Skip to main content

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

This is the fourth part of the Deploy Laravel 5.4 App on LEMP Stack (DigitalOcean Guide). In the last post, I have shown how to prepare the server for Laravel application deployment. And in this post, I'm going to show how to setup the local machine for production deployment.

Make sure that your local Laravel directory is under git version control before you continue. If there is a .git folder in your Laravel folder that means Git is initialized. You can use git log command to check the commit logs.


You can learn more about Git from the official website.

Following command add new remote and set up our server as a push location. Substitute the red text with your domain name or IP address.

git remote add production ssh://root@196.0.0.1/var/repo/site.git

You can run git remote command to view a list of existing remotes.


You can execute the following command to push it to the server.

git push production master

It will prompt you to enter the password.The process is similar to GitHub remote push.


Now it's time to verify whether Git Hook works.Log into the server using the following command. Remember to change the IP address in the following command.

ssh root@196.0.0.1

Now cd into your Laravel folder. And list all the files using ls command. You will be able to see the Laravel folder structure.

cd /var/www/app


Run Composer


Change into the Laravel folder and run the following command to run the composer.

composer install --no-dev

The --no-dev flags tell the composer not to download any dependency that is used in development process. You will surely get warning messages in yellow but it is normal since some of the dev dependencies won’t work on our server.


Laravel Permissions


Nginx needs certain permissions over the Laravel directory we made. Let's give group ownership of our Laravel directory structure to the web group by typing:

sudo chown -R :www-data /var/www/app

Next, we need to give the web group write privileges over our storage directory so it can write to this folder. This is where you store log files, cache, and even file uploads.

sudo chmod -R 775 /var/www/app/storage

Now go the domain address or IP address specified in the Nginx config file. You will see something like this.


To examine the error, go to the storage/logs folder in Laravel application and open the laravel.log file using the following command.


This error says that Laravel application is unable to write to the /boostrap/cache/ folder.

Run the following command to allow write access.

sudo chmod -R 775 /var/www/app/bootstrap/cache

There are some other errors to be corrected. They will be addressed in the later sections.

Database Setup


Almost every Laravel application consist of a connection to the database. We have installed MySQL as part of LEMP stack. Let's log into the MySQL console and create a database.

mysql -u root -p'password'

There is no space between -p and password. And you have to include quotation marks as well. This approach is less secure so you can use the following syntax as well.


mysql -u root -p

Above command request, the password from you and you won't get a feedback on screen (you won't see the password you entered).


You should have a basic understanding of the SQL language syntax. Following command lists the databases on the MySQL server host.

SHOW DATABASES;


Let's create a database for our Laravel application. I'm going to name it 9url since it is the Laravel application I'm going to host.

CREATE DATABASE 9url;


Now if you run the SHOW DATABASES; you will see the new database as well. You can exit from the MySQL CLI using exit or quit commands.

Configuration


If you check the .gitignore file in the Laravel app will see that some files will not push to production.


By default storage directory is in your .gitignore file which means that anything stored in there (like user generated images) will not be transferred to the server. You can edit .gitignore to include any file or folder you want to push.

However, it is a bad idea to include .env in the repository .env file contains sensitive information such as passwords, database credentials, SMTP credentials, and API keys.

Create .env file


You can create a new .env file using the sample file provided in Laravel app. Go to the Laravel application folder located in /var/www/app and execute ls -a. It shows all the files including hidden files.


cp Linux command is used to copy and rename the file. Execute the following command to copy and rename the .env file.

cp .env.example .env

Now I'm going to edit the file using nano editor. Anything in the .env file will override whatever is in the config files.


Set APP_DEBUG variable to false. Let's change the database variables. Make DB_HOST to localhost. DB_DATABASE, DB_USERNAME, DB_PASSWORD need to be changed according to your configurations.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=9url
DB_USERNAME=root    
DB_PASSWORD=password


And you can set the APP_NAME variable as well. Normally this is the domain name or IP address with http://.

You may need to change other variables according to your Laravel application requirements.

Setting up Encryption Key


Laravel needs an encryption key to be set up in order to encrypt sessions, cookies, and passwords. This needs to be a random key that is unique to our application to make everything more secure. I'm going to generate this key with the following Artisan command.

php artisan key:generate

 
To give your application a speed boost, you should cache all of your configuration files into a single file using the config:cache Artisan command. This will combine all of the configuration options for your application into a single file which will be loaded quickly by the framework.

php artisan config:cache


Database Migrations


Now it's time to migrate database schemas. Migrations are typically paired with Laravel's schema builder to easily build your application's database schema.

To run all of your outstanding migrations, execute the migrate Artisan command as follows.


Finally


You can visit the domain name or IP address to view the homepage or route / of your Laravel application.



This is the last part of a lengthy tutorial. If you encounter any problem please comment it and I will try to help you to solve it.

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