Web Hosting Knowledge Base / Hosting and Websites

How to install Laravel on shared hosting?

This month I got a couple of clients asking me to recommend VPS or dedicated servers' providers because they want to install Laravel and they thought it's impossible to do that in a shared hosting environment like what we offer at 2MHost! They tried to upload the entire Laravel application from their computer to their hosting account using FTP, the process was long and without success at the end.

The good news is our shared hosting account is equipped with many hidden features, like shell terminal access, PHP Composer and Git. With such tool's using FTP is no longer necessary.

So, in this short guide, we will go through how to set up a new Laravel 9 application on regular shared hosting account, it will be surprisingly easy without anything to upload.

It's important to mention that Laravel is made to run on the root folder of your website, not on a subdomain or a folder. I presume that you will set it up on fresh hosting account.


Let's start, there are 2 ways to install Laravel

A. Install Laravel using Composer

Here we will install a fresh copy of Laravel using PHP Composer.

1. Laravel 9 requires PHP 8 and above, so you need to use your cPanel to change the PHP version of your website to version 8.

2. Login to your new hosting account via terminal, you can use the terminal in your cPanel or any other terminal software like PuTTy. Login with your cPanel user name and password and remember that we use port 5555 for SSH in all servers.

3. Create a new Laravel application using Composer, we will install Laravel in a folder called my-app, you can choose any other name

composer create-project laravel/laravel my-app

It will take seconds and you are done, there is nothing to upload, only a single command line!

4. Now the trickiest step, Laravel has a folder called public, this folder contains the index.php file, the my-app/public folder is one level under cPanel's public folder public_html, so what we need to do is to let cPanel's public_html folder loads the content of Laravel's public folder.

First, we will delete the entire public_html folder (its new hosting account, so nothing to worry about):

cd ~ rm -fr public_html

then create a symbolic /public_html to link to /my-app/public:

ln -s my-app/public public_html

That's it. Now, your Laravel website is running smoothly from a shared hosting!

Laravel setup on shared server

B. Install Laravel - or - Deploy your Laravel app using Git

Here we will deploy the application that you are already developing onto your local computer, in this tutorial, I'll presume that you already have a remote repository set up on Github for your application and we will pull the changes from it to the website.

1. Like the first method, change the PHP version to 8 and login to your account via terminal.

2. Create a new folder for your application, my-app or any other name, and change directory to it.

mkdir my-app cd my-app

3. Initiate Git, and add your application repository as remote origin

git init git remote add origin https://github.com/2mhost/my-app.git

4. Pull a copy of the remote repository

git pull origin main

5. Install Laravel dependencies using Composer

composer install

6. Create a local .env file

mv .env.example .env

7. Create a new Laravel key

php artisan key:generate

8. Finally, Delete /public_html folder and create a symbolic link to Laravel public folder

rm -fr public_html ln -s my-app/public public_html

We are done, your website will load your application.

Like Laravel setup on any environment, you will need to create a symbolic link for the public storage desk:

php artisan storage:link

And create a MySQL database in your cPanel and configure it in /my-app/.env file.

There is only one known limitation, you can't run a Laravel Queue Worker monitor, if you need to set up one, then please contact us to help you.

Litespeed hosting and Laravel performance.

Personal shared hosting account has enough resources (RAM/CPU) to run Laravel application during development, however, it's highly recommended to choose high resources hosting package on a litespeed server when website go live.

Litespeedtech, the company behind Litespeed server has released a special package for Laravel caching. With the LSCache package, your application will deliver better performance and superior user experience.

If your Laravel application is hosted on Litespeed server, then remember to install the caching package

composer require litespeed/lscache-laravel
Last update: Jul 11, 2022 20:12