How to Deploy a Laravel 6 Project?

Freddy Marroquín
7 min readJun 25, 2021

--

Practice and learn how to configure a L.A.M.P Stack and deploy your Laravel project on Digital Ocean.

Laravel (PHP framework) + Digital Ocean (Cloud Infrastructure Solutions)

DigitalOcean provides developers cloud services that help to deploy and scale applications that run simultaneously on multiple computers.

Digital Ocean App Platform is a good choice for a fast deploy but this means an increment in the droplet(Scalable Virtual Machine) price of $12usd/month. By now, we’ll deploy the app without App Platform for $6usd/month. Cool right.

You also can find the LAMP Stack One-Click on Digital Ocean MarketPlace. I don’t recommend it to beginners. It's a Configured Droplet with The LAMP Installed.

The only way you learn how things work it's doing it by yourself so let's Start.

Step 1:

Create an Account on Digital Ocean you also can use my referral code https://m.do.co/c/a35e0e93a2cc and get started with $100(60 days period). Thank you for your support, Let’s start.

Step 2:

Create a droplet(Scalable Virtual Machine)

Create a droplet button and then a droplet option

Choose an Image, in this guide we’ll be using Ubuntu 20.04 because is the latest and stable version.

Choose an operating image

Choose a Plan, okay, this depends on your app and traffic. Premium Intel with NVMe SSD $6/mo is okay for me. You also can choose Regular Intel with SSD for $5/mo.

Digital Ocean Plans

Choose a Region.

Secure your droplet with an SSH key. Create a new SSH Key

Windows Users try PuTTY

Guide Link: https://docs.digitalocean.com/products/droplets/how-to/add-ssh-keys/create-with-putty/

For Linux and Mac Users we’ll be using OpenSSH

Open your terminal and write ssh-keygen.
“Enter file in which to save the key”: leave it blank
“Enter a passphrase and enter the same passphrase again”

I will strongly recommend you to use a passphrase.
The purpose of the passphrase is usually to encrypt the private key. This makes the key file by itself useless to an attacker. It is not uncommon for files to leak from backups or decommissioned hardware, and hackers commonly exfiltrate files from compromised systems.

This will generate 2 files id_rsa and id_rsa.pub.

cat ~/.ssh/id_rsa.pub
this will display the ssh key copy and paste it on Digital Ocean name and save it.

Next, you will able to create the Droplet, Congratulations!🎉🥳

Last step for droplet creation

STEP 3:

Log In to your droplet.

Copy your droplet IP

Next, type: ssh root@dropletIP then continue typing yes

We’re In👨‍💻

hackerman mem

STEP 4:

Installing system requirements, Apache.

  • Linux (Droplet)✔
  • Apache - Next
  • MYSQL
  • PHP

As you can see, we need to install Apache, MySQL, and PHP.

First, we’ll update the package list that needs an upgrade. Run:

sudo apt update

Next, we proceed to install Apache

sudo apt install apache2

Configure the firewall to allow external access to default web ports and the SSH Connection.

sudo ufw app list
sudo ufw allow Apache
sudo ufw allow OpenSSH
sudo ufw enable
sudo systemctl status apache2

Go to the web browser and type yourdropletIP in the URL and TADA! 🎉 you successfully configured Apache. Easy Right.

STEP 5:

Installing system requirements, MySQL

  • Linux (Droplet)✔
  • Apache✔
  • MYSQL - Next
  • PHP

Databases, databases everywhere!

We’re installing MySQL with the next command

sudo apt install mysql-server
sudo mysql_secure_installation

Validate Password Component please press y, there are 3 levels of password validation policy, I recommend the number 2 (Strong). you need to insert a password with at least one Upper Case, Lower Case, Number, and a Special Character

Remove anonymous users?: y
Disallow root login remotely?: y
Remove test database and access to it?: y
Reload privilege tables now?: y
and that’s it all done!

By now we have auth_socket access what it means?

The server-side auth_socket authentication plugin authenticates clients that connect from the local host through the Unix socket file.
Easily you will be able to type sudo MySQL and you’re in so we need to configure a user and password and a database for the project. Let’s Start.

sudo mysql
SELECT user,authentication_string,plugin,host FROM mysql.user;

Let’s create the user admin

CREATE USER 'admin'@'localhost' IDENTIFIED BY 'YourPassword';
GRANT ALL PRIVILEGES *.* TO 'admin'@'localhost';
FLUSH PRIVILEGES;
SELECT user,authentication_string,plugin,host FROM mysql.user;

Log out using exit or \q to root user and log in to your new user.

mysql -uadmin -pYourPassword
CREATE DATABASE yourdatabasename;
SHOW DATABASES;

STEP 6:

Installing system requirements, PHP

  • Linux (Droplet)✔
  • Apache✔
  • MYSQL✔
  • PHP - Next

We’re almost done, Install PHP it's the last step for complete the LAMP Stack.

sudo apt install php php-mbstring php-xml php-bcmath
php -v

STEP 7:

Install Composer.

Composer is a PHP dependency manager.

sudo apt install php-cli unzip

Be sure to be in the root directory, use ls to know where are you at

Next type the following commands

curl -sS https://getcomposer.org/installer -o composer-setup.php
HASH=`curl -sS https://composer.github.io/installer.sig`
echo $HASH

Output:

756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3

Next, run the following command to verify if the composer installation is safe.

php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

Now, Install Composer Globally

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Try Composer running composer in the terminal

Be careful using composer as root/super, to use composer safe use composer with no plugins and no-scripts flag. For Example:

composer install --no-plugins --no-scripts ...
composer update --no-plugins --no-scripts ...

STEP 8:

Downloading Laravel, we’re so close.

Go to the public directory

cd /var/www

Download Laravel 6, using composer to create the project. Easy!

you also can use git to clone your project. We’ll be doing it at STEP 9.

composer create-project --prefer-dist laravel/laravel nameofyourproject "6.*"

So now, we need to adjust directory permissions. Run the next command:

chown -R www-data:www-data /var/www/project/

We need the document root points to our project to run the following command:

nano /etc/apache2/sites-enabled/000-default.conf

Adjust the document root with the correct path.

DocumentRoot /var/www/yourproject/public
<Directory /var/www/yourproject/public>
Require all granted
AllowOverride All
</Directory>

Enable mod_rewrite

a2enmod rewrite

That’s all for apache configuration, now restart apache.

sudo systemctl restart apache2

Go to your project path and just right there create a .env file

cd nameofyourproject
nano .env

Paste the following text to the .env file.
Example for Laravel 6
Source: https://github.com/laravel/laravel/blob/6.x/.env.example

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stackDB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Ctrl + x and type y to save it

As you can see App Key is missing, Let's create it.

php artisan key:generate 

Your Laravel Web App is Online.

STEP 9:

Deploy your project using git

You have learned how to configure an apache server and how to place a Laravel web application, now we are going to clone our git repository.
git comes by default in DO droplets to make sure it is installed, we are going to run the following command.

sudo apt install git 

Clone your repo inside the www folder. for example

git clone https://github.com/newtella/stokes.git

go to the project folder and run

composer install

this will download all the Laravel dependencies,

Follow the instructions in STEP 8 to configure your apache server to point your project.
You're now able to deploy a Laravel project.

--

--