Laravel is a popular, open-source PHP web application framework. It has an expressive, elegant syntax and provides tools needed for large, robust applications. Deployer is a modular open-source PHP deployment tool packed with time-saving features and optimizations. It supports deployment of many popular frameworks, including Laravel, Symfony, Zend Framework and CodeIgniter. In this tutorial, we will show you how to deploy a Laravel application with Deployer on Ubuntu 16.04.
Table of Contents
Requirements
- Ubuntu 16.04 server with root or sudo access.
- Git server. You can either use your own self hosted git server or any popular Git services such as Gitlab, Github or Bitbucket.
Setting up the Local Development Environment
You will deploy your Laravel application from your local computer so first you need to setup the local development environment and install Deployer.
First if you already don’t have composer installed on your local computer install it with:
Next, create a new Laravel project on your local computer:
To install Deployer on your local computer run the following commands in the console:
Finally connect to the git server by adding your local PC ssh key to the git server.
If you don’t have SSH keys, you can check visit our article about How To Set Up SSH Keys on Ubuntu 16.04
If you are using Git hosted service you can check their documentation about how to add SSH key to your account.
Install and configure PHP
Now that your local development machine is ready before we can start deploying we need to configure the server by installing all thw necessary services.
To install the latest stable version of PHP version 7 and all necessary modules, run:
We also need to install composer:
Install and configure Nginx
Install the latest stable Nginx version from the official Ubuntu repositories:
Next, create a new Nginx server block:
Activate the server block by creating a symbolic link:
Test the Nginx configuration and restart nginx:
Create MySQL database
If you don’t have MySQL installed you can visit our tutorial about How to Install MySQL on Ubuntu 16.04
To create a database for the laravel application, run the following commands:
Create a Deployer User
The main purpose of this user is to deploy our Laravel application by executing commands on our server.
To create the user run the following command:
Both Nginx and PHP FPM are running as a user www-data. The files and directories created by the deployer user must be writable by the www-user.
Run the following command to add the deployer user to the www-data group:
Change the document root ownership by running:
and set user group ID
With the above command all new files and subdirectories created within the `/var/www/html/my-laravel-app.com` will inherit the directory group ID.
Add your local user public SSH key to the /home/deployer/.ssh/authorized_keys file so you can login to the remote sever from your local computer without a password as a user deployer and run the deployment commands.
Finally you need to add the deployer user SSH key to your git server, same as you did with your local computer user.
Using Deployer
Open your local computer terminal and switch over to the laravel project directory.
Run the following command to create a recipe file named deploy.php
.
Open the file and configure and set the correct repository credentials and configure the host parameter as follows:
From the inside project directory you can now run the `dep` command to deploy your application.
For more information about how to configure and use Deployer please visit their documentation page.
Of course, you don’t have to configure your remote server if you use one of our Blazing-Fast Ubuntu Servers, in which case you can simply ask our expert Linux admins to create the deployer user and install and configure all necessary services for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post, on how to Automatically Deploy Laravel Applications with Deployer on Ubuntu 16.04, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.