Upload Laravel Codes on a Shared Hosting
Introduction
For some reason you want to upload Laravel codes on a shared hosting. Maybe just to upload a sample application, a prototype, or you don’t want to use a cloud based hosting like Digital Ocean and find a cheaper hosting. In this tutorial we will go through the steps on how to upload a Laravel Application to a cPanel.
Steps
First is to zip the necessary files. Using FTP application to upload codes takes a lot of time if not zipped. There are also some files and folder you can exlude.
- app
- bootstrap
- config
- database
- node_modules – Most likely you are compiling using Gulp, Laravel Elixir, or Laravel Mix so you can just exclude this folder.
- public – Exlude this since files from here will be just copied to public_html (sub-domain directory if you are uploading to sub-domain).
- resources
- routes
- storage
- tests – You can ignore this.
- vendor
- .env
- .env.example – You can ignore this.
- .gitattributes – You can ignore this.
- .gitignore – You can ignore this.
- artisan – You can ignore this.
- composer.json – You can ignore this.
- composer.lock – You can ignore this.
- package.json – You can ignore this.
- package-lock.json – You can ignore this.
- phpunit.xml – You can ignore this.
- readme.md – You can ignore this.
- server.php – You can ignore this.
- webpack.mix.js – You can ignore this.
After zipping these necessary files, upload it using FTP. You can just upload all of it inside public_html but I prefer uploading it outside like below.
- (other files/folders)
- myapp.zip
- myapp (after extracting)
- public_html
- (other files/folders)
We can now start working on the public folder of Laravel. Before we upload the public files, we need to edit the index.php.
<?php error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); /** * Laravel - A PHP Framework For Web Artisans * * @package Laravel * @author Taylor Otwell <taylor@laravel.com> */ define('LARAVEL_START', microtime(true)); /* |-------------------------------------------------------------------------- | Register The Auto Loader |-------------------------------------------------------------------------- | | Composer provides a convenient, automatically generated class loader for | our application. We just need to utilize it! We'll simply require it | into the script here so that we don't have to worry about manual | loading any of our classes later on. It feels great to relax. | */ $path = dirname(__DIR__); require $path.'/vendor/autoload.php'; /* |-------------------------------------------------------------------------- | Turn On The Lights |-------------------------------------------------------------------------- | | We need to illuminate PHP development, so let us turn on the lights. | This bootstraps the framework and gets it ready for use, then it | will load up this application so that we can run it and send | the responses back to the browser and delight our users. | */ $app = require_once $path.'/bootstrap/app.php'; $app->bind('path.public', function() { return __DIR__; }); /* |-------------------------------------------------------------------------- | Run The Application |-------------------------------------------------------------------------- | | Once we have the application, we can handle the incoming request | through the kernel, and send the associated response back to | the client's browser allowing them to enjoy the creative | and wonderful application we have prepared for them. | */ $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); $response = $kernel->handle( $request = Illuminate\Http\Request::capture() ); $response->send(); $kernel->terminate($request, $response);
Notes on the highlighted lines. Make sure the $path
variable is set to where the laravel files are located. The $app->bind...
codes tells laravel where is the public directory (public_html where index.php currently resides).
That is it. You should be able to load your website on the browser.
Thanks for reading!