Deploying a Laravel 5 application with Git on a cPanel hosting

This article explains how to deploy an application developed with Laravel 5 on cPanel hosting using Git to facilitate the deployment process.

1. Configure SSH and Git

SSH is enabled by default onLaravel hosting packages. You can import your public key via cPanel > Advanced > SSH/Shell Access, or you can log in with your cPanel password each time.

Check SSH connection:

ssh [cpanel-user]@[domaine.com]

Check that Git is installed on your hosting space:

git --version

Create a bin folder in your root directory:

mkdir ~/bin

2. Configure PHP

On your Nindohost hosting account:

cd ~
vim .bash_profile

Replace :

PATH=$PATH:$HOME/bin

by :

PATH=$HOME/bin:$PATH

This prioritizes the bin directory over other paths.

Next, link the correct PHP version to your bin directory:

cd ~/bin
ln -s /usr/local/php56/bin/php-cli php

Close the terminal and reconnect, then test the PHP version:

php --version

This should display a similar result:

PHP 5.6.30 (cli) (built: Jan 19 2017 12:09:47)

For Laravel 5.2, we'll need at least PHP 5.5.9.

3. Create the application directory

From the console, create the [app] directory in your root, where [app] is the name of your application. Don't forget to replace it for all the following commands:

cd ~
mkdir [app]

This directory will host your Laravel application.

You can install the application on the main domain http://domain.com or a subdomain like http://[app].domain.com. Follow the instructions in the next section based on your choice:

Main domain

Replace the public_html directory with a symbolic link to [app]/public (make sure  public_html is empty before deleting it, as its contents cannot be restored):

rm -rf public_html
ln -s [app]/public public_html

Subdomain

Create a subdomain to configure the root directory to /[app]/public:

  • Go to cPanel > Subdomains
  • Add a subdomain and configure the root directory as follows:
    • Subdomain: [app]
    • Root directory : /[app]/public

4. Download Composer

On your Nindohost client area:

cd ~/bin
curl -sS https://getcomposer.org/installer | php
ln -s ./composer.phar composer

Verify that it has been successfully installed:

composer --version

5. Configure Git for automatic deployments

Create the Git repository on your Nindohost hosting space:

# Créez le répertoire git où le dépôt sera hébergé et maintenu
cd ~
mkdir git

# Créez le dépôt
cd git
git init --bare --shared [app].git

# Créez le script de traitement post-receive
cd [app].git/hooks
touch post-receive

# Rendez le hook exécutable
chmod +x post-receive

# Configurer l hooke
vim post-receive

Put all the commands and operations to be executed after the push in the post-receive file :

~/git/[app].git/hooks/post-receive
#!/bin/sh

# Configurer et exporter la variable PATH
PATH="/home/[siteground-user]/bin":$PATH
export PATH

# Répertoires de l'applications
APP_WEB_DIR="/home/[cpanel-user]/[app]"
APP_GIT_DIR="/home/[cpanel-user]/git/[app].git"

# Checkout le dernier commit dans le répertoire de l'application
git --work-tree=${APP_WEB_DIR} --git-dir=${APP_GIT_DIR} checkout -f

# Nettoyer le répertoire de l'application
# Utilisez -e "[pattern]" pour exclure certains fichiers ou répertoires du nettoyage,
# comme ils le sont dans le fichier .gitignore, par exemple :
# git --work-tree=${APP_WEB_DIR} clean -fd

# Lancez composer
cd ${APP_WEB_DIR}
composer install
   
# Vérifiez que le répertoire storage a bien les permission d'écriture pour le groupe
chmod -R g+w storage

# Optimisations
echo "Lancement des optimisations"
php artisan config:cache
php artisan route:cache

# Faîtes vos autres traitements ici, par exemple effectuez les modifications de connexion à la base de données...
# php artisan migrate
# ...

6. Add the "production" information to your repository

On your computer:

cd /chemin/a/votre/projet
git remote add production ssh://[cpanel-user]@[domaine.com]/~/git/[app].git

Now you can deploy your project on your Nindohost hosting space (by pushing to production) with:

git push production master

Your application code will appear in the /[app] directory on your Nindohost hosting space.

Leave a comment

Your e-mail address will not be published. Required fields are marked with *.

Other articles selected for you
Sovereign cloud in Morocco

Sovereign Cloud in Morocco

At last! Cloud Morocco is becoming a reality. Not without some delay, our country now recognizes the importance of having Sovereign Cloud infrastructures, which