Thursday 16 November 2017

How to Deploy Laravel to a Shared Hosting Server

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:

  1. Push your project to your remote git repo
  2. 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.g
    ls to list directories.
    You should be able to see the files and folders on your server including www (alias public_html).
  3. Now, get the url for your remote repo, like: https://github.com/sdkcodes/samplelaravelrepo.git
  4. Then in your server terminal, clone the repository: git clone https://github.com/sdkcodes/samplelaravelrepo.git
  5. Because your .env file won't be committed, you need to create one on your server, run touch .env to create your env 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.
  6. Run composer install or composer update for composer to install your dependencies.
  7. Now, your laravel app should be on the server. To be sure, you can run an artisan command such as php artisan inspire
  8. 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.
  9. 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.
  10. 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';
  11. Access your app's url now, it should be accessible.
  12. 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.

0 comments:

Post a Comment

Say something...