Deploy Laravel with Digital Ocean

Introduction

Digital ocean provide us our own linux server on cloud. They call it droplet. All operations that you do on local dev server you can do on droplet as well. Signup for Digital Ocean You can signup with you google account by visiting digital ocean website or you can use this link here.

If you use the link you will get extra $50 credit for free. After you signup, go to dashboard and hit create droplet. On droplet create page, go to one-click-apps tab and select LAMP on 16.04 Choose droplets size depending on your needs.

I will go with $5/mo option Choose datacenter region depending on your target audience location.

Now add New SSH Key(use public key). If you have no ssh key generated on your system.

Learn how to do here. Finally hit create to create new droplet.

 

Login to your server

Open terminal (cmder if on windows or use git bash)

ssh [email protected]_droplet_ipaddress

(example: ssh [email protected]) If you have multiple ssh file and get access denied error you have to specify path to file.

ssh -i path/to/private-ssh-file [email protected] //Example ssh -i c:/users/webdevmatics/.ssh/digital [email protected]

Congrats now you are logged in into your ubuntu server on cloud. Now you can install everything you want from terminal. At this point our ubuntu server got apache2, mysql and php installed. If you are deploying laravel project greater than 5.5 you need php7.2.

 

Install php 7.2 and all extensions required by Laravel

apt-get update && apt-get upgrade
apt-get install python-software-properties
add-apt-repository ppa:ondrej/php
apt-get update
apt-get install php7.2
apt-get install php-pear php7.2-curl php7.2-dev php7.2-gd php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml

Verify installation by running php -v If you get any error regarding php version refer this 

Create database

Since mysql is installed by digital ocean for us it saved mysql password. See password by running  cat /root/.digitalocean_password  copy it and save it somewhere. log into mysql

mysql -u root -p

You can replace root by username if you have created any. Not enter password that you just saved. Replace ecom with any database name you want.

create database ecom;

show databases;

exit

Install Laravel project from GitHub

Now we will pull in our repository from github using git clone command. But first we need to go to www/html directory

cd /var/www/html

then remove all content from this folder by running

rm *

git clone [email protected]:webdevmatics/ecom.git

cd ecom

composer install

Create .env file

cp .env.example .env

sudo vim .env

set db name, username: root, password: your mysql password that you saved

php artisan key:generate

php artisan migrate

Apache configuration

#go to apache folder

cd /etc/apache2

Open apache config file

cd /etc/apache2/sites-available

sudo vi 000-default.conf

Change content like this Of course replace ecom with your project folder name that is inside /var/www/html/ folder

<VirtualHost *:80>

ServerAdmin [email protected]

DocumentRoot /var/www/html/ecom/public

<Directory /var/www/html/ecom/public>

Options Indexes FollowSymLinks

AllowOverride All

Require all granted

</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

<IfModule mod_dir.c>

DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm

</IfModule>

</VirtualHost>

Save and quit from vim. codess esc key and then :wq

Restart apache

sudo a2enmod rewrite

sudo service apache2 restart

Give permission to storage folder Make sure you cd into your project folder and run. If you need to give permission to any other folder just replace storage with path to the folder.

sudo chmod -R 777 storage

Thats it you should have your project up and running. Visit your droplets ip address to verify. If you have custom domain name you can point that to digital ocean server. Article on how to do that here.

For pulling latest changes just to normal git pull from html/ecom folder

cd /var/www/html/ecom

git pull

Thanks you for reading :)