Scroll to top

Deploying Laravel on DigitalOcean using Github and dploy.io

Prerequisites

  • Create Github Account .
  • Create dploy.io Account.
  • Download and Setup Github.
  • Install a fresh copy of Laravel.
  • Create a DigitalOcean Account by clicking here (Note: Referral link, you will get 10$ as free credit but you need to upload at least 5$ to activate the account).

Steps

Below is a list of steps we will follow to deploy our new app

  • Setup Repository on Github.
  • Connect Github to dploy.io
  • Setup new Droplet on DigitalOcean
  • Connect DigitalOcean to dploy.io
  • Configure Web Server
  • Fixing Permissions
  • Push, push and push.

Setup Repository on Github

Run Github app and Click on the + icon on the top left side, this will show you a dialog to Create New Repository, Enter the name of your new repository and select your new laravel copy by clicking browse and click Create repository. It will open your repo tab click publish repository and we are good to go with the git now. Make sure you put vendor folder in git ignore

Connect Github to dploy.io

I hope you have created a new dploy.io account if not click here to create one

Once you are done with signup click on Connect Repository on Dashboard, select GitHub and click connect new account this will redirect you to Github for OAuth , login to GitHub and Click Authorize Application. Select your repository from the drop-down and click connect. This will insert a webhook into your repository you can see that going to repository settings

You can now see your new connected repository in dploy.io.

Setup new Droplet on DigitalOcean

DigitalOcean provides One-Click apps which will be auto-installed when you create a new droplet, for us two of them are important shown below

  • Lamp Stack
    Start up your server with the basics with the one-click LAMP Droplet. Apache, MySQL, PHP are all installed and set up on a fresh Droplet in 55 seconds.
  • Lemp
    Spin up this lightweight web stack on a fresh Droplet, and you’ll be ready to go with Nginx, MySQL, and PHP.

I prefer to go with nginx as the server so i will choose Lemp. Let’s create our new droplet go to your dashboard and click New Droplet, enter droplet name, select size, select region, in select image go to applications tab and select LEMP on Ubuntu 14.04 and click “Create Droplet”. Go grab some coffee while digitalocean setup everything for us

Once it is done you will see your droplet dashboard

  • Power: You can restart(Power Cycle) your droplet or do a shutdown (Power Off).
  • Access: This will open up a console VNC connection to your droplet.
  • Resize: Quickly add resources to your droplet to handle high traffic. You need to power off to add more resources like RAM or Cores.
  • Snapshots: It will create a current state copy of your server it can take more than an hour and you need to power off your droplet

Connect DigitalOcean to dploy.io

Let’s connect digital ocean to dploy.io, go to your dploy.io dashboard and click on integrations in the top menu. click on connect for the digital ocean, put any Account Name, you can find your Client Id and API Key by clicking here. If you have not generated API Key previously Click Generate New Key. Keep a note of client id and API key (I will only be visible once).

Go to your repository in dploy.io and click Create Environment & Server. Name your environment like development/production, select deployment mode (manual recommended)  and save.
On Add Server screen select Digital Ocean and click next step ,  enter server name and select latest revision , in remote path enter path of the server where your repository will be created and updated (typically it will be webroot /var/www/projectname like /var/www/kodeinfo) and click check connection. Oops, here’s the twist you have not inserted your dploy.io ssh key into digital ocean . Download Putty and go to Session, enter your droplet IP address(you can find IP address on the dashboard)  and click open, user and password you have that in your email when you first created a droplet. Once you logged in it will ask you to set a new UNIX password.

Now first things will be to create /var/www/kodeinfo

mkdir /var/www
mkdir /var/www/kodeinfo

Or

sudo mkdir -p /var/www/kodeinfo

On the screen where you checking the connection to digital ocean on dploy.io you will see “copy the key” below droplet selection when you click it will show you ssh key, copy the key and we have to paste it into ~/.ssh/authorized_keys file of your droplet

nano ~/.ssh/authorized_keys

It will open the file then paste your key(if ctrl+v do not work right click will paste the clipboard content) and save the file. you need to restart your droplet for the key to take effect.
Now go to dploy.io and click “Check connection” and it should take you next screen. You can skip this screen. Now click deploy, select the latest revision and click Review deployment check if everything good and click deploy/redploy all files. Initial deployment can take time now go to ssh agent and check if it is done

cd /var/www/laravel
ls

Now run composer install and your migrations (php artisan migrate) but we don’t have composer installed so let’s update our droplet and install php-cli and composer

sudo apt-get update

sudo apt-get install php5-cli

curl -sS https://getcomposer.org/installer | php

sudo apt-get install php5-mcrypt

sudo php5enmod mcrypt

php composer.phar install

If you get below error

PHP Fatal error:  Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory' in phar

then follow this steps to create a swap file.

We are updating our ubuntu to get latest packages, after that, we need to install PHP command line interface, install composer, install mcrypt , enable mcrypt extension, run composer.phar install and last migrate

When you first connect to the server using SSH agent it will show you Mysql password in the welcome screen. So note down your password because next, we will be setting up MySQL . Install mysql-server and activate it

sudo apt-get install mysql-server php5-mysql
sudo mysql_install_db

Finish Installation by running the setup script

sudo /usr/bin/mysql_secure_installation

The prompt will ask you for your current root password.
Type it in.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Then the prompt will ask you if you want to change the root password. Go ahead and choose N and move on to the next steps.

It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the new changes. Now

Configure Web Server

The first thing that we need to do is open the main PHP configuration file for the PHP-fpm processor that Nginx uses. Open this with sudo privileges in your text editor:

sudo nano /etc/php5/fpm/php.ini

We only need to modify one value in this file. Search for the cgi.fix_pathinfo parameter. This will be commented out and set to “1”. We need to uncomment this and set it to “0”:

cgi.fix_pathinfo=0

This tells PHP not to try to execute a similarly named script if the requested file name cannot be found. This is very important because allowing this type of behavior could allow an attacker to craft a specially designed request to try to trick PHP into executing code that it should not.
When you are finished, save and close the file.

Now, we can restart the php5-fpm service in order to implement the changes that we’ve made:

sudo service php5-fpm restart

The next item that we should address is the web server
Open the default server block configuration file with sudo privileges:

sudo nano /etc/nginx/sites-available/default

Upon installation, this file will have quite a few explanatory comments, but the basic structure will look like this:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;

        server_name localhost;

        location / {
                try_files $uri $uri/ =404;
        }
}

This provides a good basis for the changes that we will be making.

The first thing we need to change is the location of the document root. Laravel will be installed in the /var/www/kodeinfo directory that we created.

However, the base files that are used to drive the app are kept in a subdirectory within this called public. This is where we will set our document root. In addition, we will tell Nginx to serve any index.php files before looking for their HTML counterparts when requesting a directory location:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/kodeinfo/public;
    index index.php index.html index.htm;

    server_name localhost;

    location / {
            try_files $uri $uri/ =404;
    }
}

Next, we should set the server_name directive to reference the actual domain name of our server. If you do not have a domain name, feel free to use your server’s IP address.

We also need to modify the way that Nginx will handle requests. This is done through the try_files directive. We want it to try to serve the request as a file first. If it cannot find a file of the correct name, it should attempt to serve the default index file for a directory that matches the request. Failing this, it should pass the request to the index.php file as a query parameter.

The changes described above can be implemented like this:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/kodeinfo/public;
        index index.php index.html index.htm;

        server_name server_domain_or_IP;

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

Finally, we need to create a block that handles the actual execution of any PHP files. This will apply to any files that end in .php. It will try the file itself and then try to pass it as a parameter to the index.php file.

We will set the fastcgi_* directives so that the path of requests are correctly split for execution, and make sure that Nginx uses the socket that php5-fpm is using for communication and that the index.php file is used as the index for these operations.

We will then set the SCRIPT_FILENAME parameter so that PHP can locate the requested files correctly. When we are finished, the completed file should look like this:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/kodeinfo/public;
    index index.php index.html index.htm;

    server_name server_domain_or_IP;

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

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Save and close the file when you are finished.
Because we modified the default server block file, which is already enabled, we simply need to restart Nginx for our configuration changes to be picked up :

sudo service nginx restart

Fixing Permissions

This is really quite an easy fix:

chgrp -R www-data /var/www/kodeinfo
chmod -R 775 /var/www/kodeinfo/app/storage

Thanks

KodeInfo

Author avatar

admin

http://www.kodeinfo.com
Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Post a Comment

Your email address will not be published. Required fields are marked *