How to deploy Laravel in a shared hosting in 5 easy steps

I’ve read a lot of deployment method for Laravel, but most of them don’t completely fit the environment we have. So In this tutorial, I’d like to show 5 easy steps to deploy your Laravel project to a shared hosting.

First, my goal is to deploy expose only the public folder and have the Laravel related files in another secure folder. I’m using fastcomet for deployment since this is what the team decided to use.

Typically, your public folder will fall under the directory:

/home/genuineprogrammer/public_html/

or

/home/genuineprogrammer/www/

1. Git Setup on Server

The very first thing you need to setup is the how you will transfer your files to your server. Git is my prepared process since the post-processing and hooks a very useful when it comes to automated deployment.

Open your Git Bash, and log in via your shared hosting’s account via ssh.

SSH
SSH

Once you logged in, go ahead create a folder in the root directory. this will be our git repository directory.

mkdir repos

go inside the repos directory

cd repos

Create a git folder. You can create multiple git folders if you have other domains or project to deploy later on.

mkdir dev_genuineprogrammer.git

Then, go inside the git folder and initialize the git repository using minimal setup

git init --bare

— bare doesn’t contain any working directory for you to develop on. You will only use this folder for the latest deploy source code on your server. All development will still be done on your computer. After the commands above your git repository should be similar to this one:

Git Bare
Git Bare
Git Bare Folders
Git Bare Folders

2. post-receive hook

After setting up the git repository, we will now create the hook script. One of the hooks you can use is the post-receive hook. This enables you to do additional processing and execute shell script commands every time there’s a new push to the git repository.

To do this, you must create a post-receive file in the hooks folder.

cd hooks

then

nano post-receive

Copy the contents below, replace the appropriate folders and save the file

Don’t forget to make this file executable

chmod +x post-receive

Ideally, you would end up in a folder like this

post-receive
post-receive

3. Laravel Related Files

We are still missing some important files for our project deployment to be completed server-side.

If you notice from above, there’s <LARAVEL_APP_FOLDER>. This is the folder that will contain all Laravel files for our project, that the post-receive will checkout from the latest push. Laravel files like app, bootstrap, config, etc. The Laravel folder is empty initially, since we haven’t execute the hook script yet.

laravel_app folder
laravel_app folder

We also need to create the .env file after we have created the <LARAVEL_APP_FOLDER>. So configure your .env file and don’t forget to update the database connection as well.

env
env

In addition to this, we also need to create an index.php file which contains the adjusted bootstrap directory inside the /public_html or /www folder. Basically, we just pointed the publicly exposed file to this <LARAVEL_APP_FOLDER>, check line 22 and 36.

4. Adding remote URL to local git project

Since we have completed the prep work for deployment on our server, we can now proceed to add the remote URL. To add the remote URL, open your local git project and add the following:

git remote add

Example:

git remote add dev_remote dev_genuine@sg3.fcomet.com:17177/home/hom_path/repos/dev_genuinprogrammer.git

5. Git push

From here, you can now execute the git remote push.

git push master

You can also check the remote URLs via

git remote -v

In conclusion…

The automation and usage of hooks really help us the old method of copy-pasting files manually. This also helps keeps the source code latest on every deployment.

You can also do other post-processing like regenerating the cache etc. or modify the hook script according to your needs.

Let me know what you think in the comment section below. 🙂

Looking for SAPUI5 related tutorials? check it here.

Share this...
Share on Facebook0Tweet about this on TwitterShare on LinkedIn0Email this to someonePrint this page

Leave a Reply