LARAVEL APPLICATION DEPLOYMENT AND MANAGEMENT WORKFLOW ON A SHARED HOSTING

Adetunji 'TeeJay' Opayele
HostCabal
Published in
5 min readFeb 11, 2018
Photo by Raul Varzar on Unsplash

A friend asked for a guide to deploy a Laravel app on our Shared Hosting plan. Instead of sending him off to get lost in the endless abyss of Google, I decide to document my own personal way of deploying a Laravel app to a Shared Hosting Service.

I love Lavarel cause it’s really sweet and fun to develop with, yea your deployment workflow should be as fun too. Infact deploying Laravel on a shared hosting is pretty dang easy and laravel-like!

Lets dive into it!

This is a complete workflow of the initial deployment and continued maintenance of your Laravel application codebase in a shared hosting environment.

What you need.

  1. A Locally developed Laravel application running on your development environment (xampp, valet, homestead)
  2. A Shared hosting account with SSH Access
  3. A Shared hosting service that supports Git, Composer, PHP 7.x.x
  4. Git installed on your local machine.

In this guide we will setup our deployment workflow to automatically push updates from the local development machine to the server via git.

Getting Started!

Prepping up on the hosting server

Contact your web hosting provider for your ssh credential

on the mac/linux open terminal and run

$ ssh -p 22 username@ipaddress

replace 22 with your ssh port, username with the ssh username and ipaddress with the server ip address.

on windows, Download and install putty or any other ssh client and do the equivalent.

Create a directory called repo/ for storing the app repository and another directory called app/ for storing the laravel app on the server.

$ mkdir repo app

Next navigate into the repo/ directory and create a new git repo there.

$ cd repo/
$ git init -— bare -— shared mywebsite.git

Prepping up your local app

Navigate into your Laravel application directory in your development environment and initialise a new git repo (if you don’t already have one) .

$ cd /username/projects/my-app/$ git init$ git add .$ git commit -m "Deploying my app to the shared hosting server"$ git remote add production ssh://username@ipaddress:port/~/repo/my-app.git

What you just did was to enter the my-app directory on our local machine, initialise a new git repo with the git init command. Add all the files into the git repo with the git add . command, then commit all the changes into the repo with a -m message and finally add a remote repo with the git remote command (make sure to replace username, ipaddress and port with your shared hosting credentials)

Now ssh back into your shared hosting account and set up the post receive git hook. This is a series of commands to run at a git repo receives a git push.

Navigate to the git directory repos/my-app.git

$ cd repos/my-app.git

There is a hooks directory in your my-app.git, that is where all the git hooks are stored.

Navigate into the hooks directory and create a post-receive file.

$ cd hooks$ cat > post-receive#!/bin/sh
git —-work-tree=/home/username/app/ --git-dir=/home/username/repo/my-app.git checkout -f
cd /home/username/app/composer install# Optimizationsecho “Running optimizations”php artisan config:cachephp artisan route:cacheecho “Done :)”

Hit control-c on the mac or control-d on windows to get back the shell prompt and run chmod +x post-receive to make sure the post-receive file has execute permission.

chmod +x post-receive

Confirm the post receive file has all the command written into it with cat post-receive, if it spits out all the command you put in from above then all is set to go!

Back on the local machine. Time to push!

$ git push production master

Enter the ssh password on the prompt and watch the repo get pushed to the remote git repo on the hosting server. After the push, the app files will be checked out in to the app/ directory as directed in the post-receive hook and you will see the familiar composer install running.

Last thing to do on the hosting server is to remove the public_html directory (fear not, the world will not end!)

$ rm -r public_html/

Then create a symlink between the Laravel public/ directory to the hosting public_html/ directory. (You only have to do this once)

$ ln -s app/public public_html

Yay!! We good to go! You did it!

Photo by Konstantin Planinski on Unsplash

Database set up.

Navigate to your cpanel and create a new mysql database and database users.

Back in your ssh window cd into the app/ directory.

$ cd app/

Create a .env file and fill in the database credentials.

$ cp .env.example .env$ nano .env

Fill in the .env with your app and database info.

APP_NAME=My-AppAPP_ENV=production
APP_KEY=somerandomstring
APP_DEBUG=false
APP_LOG_LEVEL=debug
APP_URL=http://yourdomain.com/
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my-app-db
DB_USERNAME=your-db-username
DB_PASSWORD=your-db-password

You should generate a new app key with

$ php artisan key:generate

Congratulations! I knew you could do it!

Photo by Jonathan Daniels on Unsplash

Time to test your app!

Come on now, don’t be shy. Navigate to your domain name in the browser.

If all went well, your Laravel app should load just fine.

So what happens when you make further changes to your applications source code?

All you have to do is to add it to the git repository and push it to production!

In you local app directory run.

$ git add . ##Adds all the updated file to the git repo$ git commit -m “Added an awesome new feature to app”$ git push production master ##deploys new changes to the server

You see, that way easier than copying hundreds of files via ftp! Cool right?

Note: If your hosting provider doesn’t have composer installed you can install it on your user with

$ wget https://getcomposer.org/composer.phar

or

$ curl -sS https://getcomposer.org/installer | php — –filename=composer$ chmod +x composer.phar

If php composer doesn’t still work Run

$ php -i | grep “php.ini”

To find your php configuration file and then run composer with the appropriate config file.

$ php -c php.ini composer

Watch out for another guide to create a [beta] testing environment on your shared hosting before pushing codes to production.

Cheers…

Questions, Comments and Contributions are welcome! If your find this useful please share and give a like!

--

--