Deploying Laravel on Shared Hosting
There are many guides available on the internet concerning deploying Laravel on shared hosting, but they are not very precise or out of date in respect of the latest changes in Laravel.
The good news is it’s very easy to publish Laravel based sites on cPanel hosting or any other type of shared hosting.
Even better is that we are going to show you how to install Laravel on shared hosting. So let’s dive in and have a look at how we can do that.
Backups
First thing first, make sure you backup your existing site source if you have one before pushing your locally tested Laravel application live. If it all goes wrong rollback.
Server Environment
It’s most likely that your shared hosting provider will not give you necessary permissions, command line access or freedom to deploy your Laravel app.
So we are going to assume that command line tools line Composer, Node or NPM are not available to compile your assets or install composer packages.
This means you have to compile all your assets locally and install all composer packages locally still you will be able to FTP in using an FTP Client and make changes to PHP source code or Blade templates. As shared hosting is a low-cost option so you should expect show restrictions.
Start Here
Let’s assume you are working in a LAMP environment locally running Apache Web Server. We are going to work with your ready to go live application copy as we will need to rearrange folders and files and make some changes to source files.
Before proceeding, let do some application clean up. Run below command (We recommend you to use Git Bash for running below commands.)
php artisan cache:clean php artisan view:clear rm -rf storage/sessions
Create a copy of .env
file with named .env.production
with the local development configs (at this stage you probably better off change all config values to shared hosting ones in this file like a database). Copy the whole application in a new folder named deploy.
Exclude the node_modules folder as its very huge and you won’t need it on the server. If you want a cleaner code base on the server then you can remove composer.json, webpack.mix.js, yarn.lock, composer.lock, gulpfile.js etc.
Earlier we created a file .env.production which should contain shared hosting configs. Let’s make this default environment within deploy folder.
cd deploy rm .env mv .env.production .env
Change Laravel Paths
As on your shared hosting public_html is your public directory from where your website will be loaded.
Therefore within deploy folder copy all files and folders from public folder to deploy and delete your public folder. So all content of public folder like index.php , CSS folder , js folder etc. will be copied directly into deploy folder.
With that in mind, we need to change Laravel paths.
In /deploy, edit the file index.php
and make the following changes.
Change: require __DIR__.'/../bootstrap/autoload.php'; to: require __DIR__.'/bootstrap/autoload.php';
Change: $app = require_once __DIR__.'/../bootstrap/app.php'; to: $app = require_once __DIR__.'/bootstrap/app.php';
Move to Server
Finally, we need to upload the whole folder to the shared hosting server using an FTP Client. Make sure you upload the whole folder to root folder.
Final Step
If your Laravel application use a database, export your database using phpMyAdmin and import it back in your cpanel phpMyAdmin.
Ensure your production .env
file has the correct configuration/credentials for accessing MySQL on the hosting server.
Point your browser at your domain and vola your Laravel application will appear.