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
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.
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.
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.
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.
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.
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
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.
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.
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
Post a Comment