How to Deploy Laravel to a Shared Hosting Server
Posted by Elusoji Sodeeq
You’re done crafting your artisan-esque laravel app on your local environment (be it homestead, laragon, xampp, valet or whatever) and now you need to show your app to the world.
There are cloud hosting solutions like Heroku, DigitalOcean etc that makes it easy to deploy laravel apps, however, we might not always have those options available to us which brings our old friend — cpanel shared hosting — into the picture.
Regardless of what you might have read out there, you can fully deploy Laravel to a shared hosting environment, and have it run fine.
For the purpose of this blogpost, I’m going to assume your host allows you SSH access, and you can use git (you really should be).
Steps:
- Push your project to your remote git repo
- Using a tool such as putty or bitvise, SSH into your server. Now, a terminal should open for you to use.
You can run few terminal commands to be sure you’re good to go. e.gls
to list directories.
You should be able to see the files and folders on your server including www (alias public_html). - Now, get the url for your remote repo, like: https://github.com/sdkcodes/samplelaravelrepo.git
- Then in your server terminal, clone the repository: git clone https://github.com/sdkcodes/samplelaravelrepo.git
- Because your .env file won’t be committed, you need to create one on your server, run
touch .env
to create yourenv
file and copy the contents from your local env to the remote one. Updating values where necessary.
PS: remember to set environment value to production and update the app url. - Run
composer install
orcomposer update
for composer to install your dependencies. - Now, your laravel app should be on the server. To be sure, you can run an artisan command such as php artisan inspire
- Copy (not cut) all the contents of your laravel’s public folder (i.e css/, js/ index.php etc) to the public_html folder of your server.
- If you try to access your url now, you should get an error. That’s because the path to your bootstrap.php and autoload.php files are not properly set.
- To fix that, open the public_html/index.php file, then change lines
require __DIR__.’/../bootstrap/autoload.php’;
To
require __DIR__.’/../projectfoldername/bootstrap/autoload.php’; - AND
- $app = require_once __DIR__.’/../bootstrap/app.php’;
To
$app = require_once __DIR__.’/../projectfoldername/bootstrap/app.php’; - Access your app’s url now, it should be accessible.
- From your server’s terminal, you can run all artisan’s commands and even git commands.
Anytime you make changes to the project locally, all you just need to do is push to git, then do git pull in your server’s terminal (and run composer update) if necessary.
Note that, if you make any changes to the public folder locally, then you’d have to manually update the public_html content.
I hope this helps you.
Originally published at sdktalks.blogspot.com on November 16, 2017.