Deploying a Laravel 5 application on SiteGround’s shared hosting with Git

This post shows how to deploy a Laravel 5 application on a SiteGround‘s shared hosting service, using Git for simplify the deployment process.

Take a look to this other post for general methods about deploy Laravel on shared hosting services: Deploy Laravel Application On Shared Hosting.

1. Configuring SSH and Git

Enable SSH access from cPanel > Advanced > SSH/Shell Access, then import your public key.

Check the SSH connection:

$ ssh -p 18765 [siteground-user]@[domain-name.com]

Check that Git is correctly installed on SiteGround:

$ git --version

Create the bin directory on the user’s home:

$ mkdir ~/bin

2. Configuring PHP

On the SiteGround host:

$ cd ~
$ vim .bash_profile

Change:

PATH=$PATH:$HOME/bin

with:

PATH=$HOME/bin:$PATH

i.e. prepend your local bin directory to assign it first priority over other paths.

Then link the right php in your bin folder:

$ cd ~/bin
$ ln -s /usr/local/php55/bin/php-cli php

Close and reopen the SSH connection, then check php:

$ php --version

Should print something like:

PHP 5.5.31 (cli) (built: Jan  7 2016 18:09:47)

For Laravel 5.2 when need at least PHP 5.5.9.

3. Create the application folder

From SiteGround host, create the [app] folder in your home, where [app] is the name of your Laravel application:

$ cd ~
$ mkdir [app]

This folder will contains your application.

Depending if you want to install the application in the main domain, e.g. http://example.com, or in a subdomain, e.g. http://[app].example.com, do one of the following.

Main domain

Replace the public_html folder with a symbolic link to [app]/public (be sure public_html is empty before delete it):

$ rm -r public_html
$ ln -s [app]/public public_html

Subdomains

Create a subdomain setting the root folder to /[app]/public:

  • Go on cPanel > Subdomains
  • Add a subdomain and a set the document root, for example:
    • Subdomain: [app]
    • Document Root: /[app]/public

4. Get Composer

On the SiteGround host type:

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

Check it:

$ composer --version

5. Configuring Git for automatic deploy

Create the Git bare repository on SiteGround’s host:

$ # Create the git directory where the repository will be mantained
$ cd ~
$ mkdir git

$ # Create the repository
$ cd git
$ git init --bare --shared [app].git

$ # Create the post-receive hook file
$ cd [app].git/hooks
$ touch post-receive

$ # Make the hook executable
$ chmod +x post-receive

$ # Configure the hook
$ vim post-receive

Write in the file post-receive all the operations that will be performed after the push is done:

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

# Set up our PATH variable and export it
PATH="/home/[siteground-user]/bin":$PATH
export PATH

# App directories
APP_WEB_DIR="/home/[siteground-user]/[app]"
APP_GIT_DIR="/home/[siteground-user]/git/[app].git"

# Checkout the last commit inside the web app directory
git --work-tree=${APP_WEB_DIR} --git-dir=${APP_GIT_DIR} checkout -f

# Clean the app directory
# Use -e "[pattern]" to exclude some file or directory to be cleaned,
# as they are in the .gitignore file
# git --work-tree=${APP_WEB_DIR} clean -fd

# Run composer
cd ${APP_WEB_DIR}
composer install
   
# Ensure that storage's folder have write permission for the group
chmod -R g+w storage

# Optimizations
echo "Running optimizations"
php artisan config:cache
php artisan route:cache

# Do other things here, for example load database changes automatically
# php artisan migrate
# ...

6. Add the ‘production’ remote in your repository

From your PC:

$ cd /path/to/your/project
$ git remote add production ssh://[siteground-user]@[domain-name.com]:18765/~/git/[app].git

Now you can deploy the project on SiteGround (pushing it on the production remote) with:

$ git push production master

You should now be able to see your code on the folder /[app] on SiteGround.

References

http://www.rarst.net/wordpress/siteground/
http://nicolasgallagher.com/simple-git-deployment-strategy-for-static-sites/
http://www.sitepoint.com/one-click-app-deployment-server-side-git-hooks/

  • Samuel Gomes Huarachi

    caiu como uma luva

  • Ben

    Thanks for the helpful article, just wondering what do you do about npm? It seems siteground doesn’t support it on their shared servers https://www.siteground.com/kb/node-js-available/

    • Yes, indeed. We used npm on our local machine and all the compiled assets were versioned on git and then sent to Siteground.

  • Waleed Hamdy

    how to update to php 7.2 instead of 5.5 ?

    • Peter Harper

      You can run any supported version of php in terminal by changing the command: “php” = default, “php72” = 7.2, “php73” = 7.3

      To change it for the site just use the CPanel tool.

Categories

Category BootstrapCategory CoffeescriptCategory DrupalCategory GravCategory HTMLCategory JavascriptCategory JoomlaCategory jQueryCategory LaravelCategory MagentoCategory PHPCategory SharePointCategory SpringCategory ThymeleafCategory WordPressCategory Workflow

Comments

Developed and designed by Netgloo
© 2019 Netgloo