How to Automatically Deploy Laravel Applications with Deployer on Ubuntu 16.04

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.

Requirements

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 https://getcomposer.org/installer | 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 https://deployer.org/deployer.phar
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 https://getcomposer.org/installer | 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/my-laravel-app.com
server {  
    listen 80;
    server_name my-laravel-app.com;
    root /var/www/html/my-laravel-app.com/current/public;

    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/my-laravel-app.com /etc/nginx/sites-enabled/my-laravel-app.com

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
CREATE DATABASE laravel;
GRANT ALL PRIVILEGES ON laravel.* TO 'laraveluser'@'localhost' IDENTIFIED BY 'your-password';
FLUSH PRIVILEGES;
\q

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/my-laravel-app.com

and set user group ID

sudo chmod g+s /var/www/html/my-laravel-app.com

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.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

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:

host('your_remove_server_ip')
    ->user('deployer')
    ->set('deploy_path', '/var/www/html/my-laravel-app.com');

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.


Automatically Deploy Laravel Applications with Deployer on Ubuntu 16.04Of 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.

Leave a Comment