How to Automatically Deploy Laravel Applications with Deployer on Ubuntu 16.04


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.


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:

curl -sS | php
sudo mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer

Next, create a new Laravel project on your local computer:

composer create-project --prefer-dist laravel/laravel my-laravel-app

To install Deployer on your local computer run the following commands in the console:

curl -LO
mv deployer.phar /usr/local/bin/dep
chmod +x /usr/local/bin/dep  

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:

sudo apt update
sudo apt install php7.0-fpm php7.0-cli php7.0-gd php7.0-mysql php7.0-mcrypt php-pear php7.0-curl

We also need to install composer:

curl -sS | php
sudo mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer

Install and configure Nginx

Install the latest stable Nginx version from the official Ubuntu repositories:

sudo apt install nginx

Next, create a new Nginx server block:

sudo nano /etc/nginx/sites-available/
server {  
    listen 80;
    root /var/www/html/;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;


Activate the server block by creating a symbolic link:

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Test the Nginx configuration and restart nginx:

sudo nginx -t
sudo service nginx restart

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:

mysql -u root -p
GRANT ALL PRIVILEGES ON laravel.* TO 'laraveluser'@'localhost' IDENTIFIED BY 'your-password';

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:

sudo adduser deployer

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:

sudo usermod -aG www-data deployer

Change the document root ownership by running:

sudo chown deployer:www-data /var/www/html/

and set user group ID

sudo chmod g+s /var/www/html/

With the above command all new files and subdirectories created within the `/var/www/html/` 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.

cd ~/my-laravel-app

Run the following command to create a recipe file named deploy.php.

dep init -t Laravel

Open the file and configure and set the correct repository credentials and configure the host parameter as follows:

    ->set('deploy_path', '/var/www/html/');

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.



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.