Hi, everyone!
In this article, Iβll try to explain how you can deploy your laravel application on shared hosting.
Iβll use hostinger.com.
It is well discouraged to use shared hosting for your Laravel 8 app. There are other very cheap web hosting starting from $5 per month.
But some clients (- very low budget clients ) or circumstance (for instance, you want to have something tested and move on) might call for you to use shared hosting.
I will cover the following here:
How to deploy your app to shared hosting (Hostinger).
Step 1) Go to Add website section under Domain's section on panel(Hostinger Control Panel) and add your domain.
Step 2) Click Manage on the domain which you want to use.
Step 3) Now you have to upload all source code of an application to Hostinger using SFTP, SSH access or Hostinger File Manager in the respective domain directory.
I assume you've finished building your app β at least a functional app that is working on your localhost.
Let's say your laravel project is named laravel with the following folder structure:
Using Hostinger File Manager: compress the entire project folder on your local machine. You'll get a zip file β laravel.zip and upload it on the root directory of your project and extract files in laravel folder.
Using SSH: You can download the project using git in the root directory of your project using ssh client like putty.
Using SFTP: You can upload project files on the respective file directory using FileZilla.
after that, your file structure should look like this.
Step 4) Open the laravel folder and MOVE the CONTENTS of the 'public' folder to your panel's public_html folder. You can as well delete the empty public folder now.
your public_html folder should look like this.
Step 5) locate the index.php file from the public_html folder. Right-click on it and select Code Edit from the menu.
Change the following code from index.php file
<?php
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.
|
*/
require __DIR__.'/../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 __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
to
<?php
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.
|
*/
require __DIR__.'/../laravel/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 __DIR__.'/../laravel/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
If everything went well, going to http://yourdomain.com should throw database errors (if you have models running on your app). Not to worry! The next phase is migrating your databases to your shared hosting.
Step 6) If Models are running on your application then you have to migrate tables to the database. I'm using sqlite database for migrating tables to the database.
Use ssh access or file manager create a 'database.sqlite' file in the 'laravel/database' directory. (Make Sure to give write and read permission)
Now change 'DB_DATABASE' in .env file of your laravel folder.
Ensure that these details are entered correctly.
Now using SSH in your laravel directory migrate all the files to a database.
If all goes well, you should have your site working fine now. So go ahead and try it out.
If you have questions please let me know in comments.
Thank You!
Top comments (27)
There's no way am complicating something so basic. What if you have the website working locally in a public folder rather than public_html and then you have to make changes to your local branch and re-deploy to git? You'll face errors when your local branch is not in sync with your remote branch.
The best way to do is:
In the above lines you should replace the following: your-domain.com β Type your own domain name and also replace your-laravel-folder with your folder name.
Hello I would like to have your help I used your path to deploy my laravel application on hostinger but I have a 403 error
I have followed the same process but now my images are not showing, It says 404 error
Which Images are you talking?
Assets or user storage?
User storage, because after uploading image,it again creates folder of public. so maybe it is better not moving files to public_html. Then What way should we do???
Oops ;-( ,
I completely missed that. you have to create one symbolic link from storage folder to public_html folder.
I Have faced the same issue with the storage folder and when I try to create a symbolic link of storage folder it shows the error -
symlink(): No such file or directory.
Please help me out with this.
solv the issue with artisan route:cache and route:clear
i use git instead comprezz and upload, but i have same exact folder struckture,
/home
-user
-laravel
-public_html
UnexpectedValueException
Invalid route action: [C:32:"Opis\Closure\SerializableClosure":210:{a:5:{s:3:"use";a:0:{}s:8:"function";s:44:"function () { return \view('welcome'); }";s:5:"scope";s:37:"Illuminate\Routing\RouteFileRegistrar";s:4:"this";N;s:4:"self";s:32:"000000001ac450da000000003105971b";}}].
i have try
Route::view('/', 'welcome');
or
Route::get('/', function () {
return 'Hello World';
});
it stille the same error.
by the way, my installation running well in my local computer. and there is another laravel installation in my hosting, but laravel 7 and running with no problem.
thanks in advance
I don't understand the question . Can you please post this on stackoverflow and share the link here?
sorry my bad english.
my index always return
UnexpectedValueException
Invalid route action: [C:32:"Opis\Closure\SerializableClosure":210:{a:5:{s:3:"use";a:0:{}s:8:"function";s:44:"function () { return \view('welcome'); }";s:5:"scope";s:37:"Illuminate\Routing\RouteFileRegistrar";s:4:"this";N;s:4:"self";s:32:"000000001ac450da000000003105971b";}}].
but i have solve it by artisan route:cache and route:clear
It is well discouraged to use shared hosting for your Laravel 8 app, WHY ? Can you explain ?
laravel app needs more processing power.
Shared hosting has not that memory. if you have some complex application your application will be really slow.
But if you have some small CRUD system your application will work like a charm.
The Laravel application needs dedicated CPU and RAM based on your application traffic and concurrent session for consistent performance. On the other hand, server resources are shared between multiple applications in shared hosting, resulting in high page load time and inconsistent performance. Also, many developers need control over the server to run specific services, Php modules, change MySQL configuration or install additional software services based on application requirements.
In shared Laravel hosting, you can't change the server and software services configuration. Also no customization is allowed or significantly less control on the server.
I personally suggest Managed Laravel Hosting over cloud servers like AWS to all the developers, so you can get the freedom you want without worrying about the hosting complexities. Try Devrims Managed Laravel Hosting.
devrims.com/laravel-hosting/
how do i auto deploy my laravel project which is in github on hostinger
Very informative!
I read about 20 pages and nothing worked, you did the right solution here, everything's working, thanks man!
Glad ! You like it.ππ
Thanks β€οΈ
hello
i have a hostinger's shared hosting
can u please provide the steps how to create symbolic link?
Login to SSH and run this command.
ln -s souce-directory targetdirectory
create file in public_html
<?php symlink('/home/user/laravel/storage/app/public','/home/user/public_html/storage');
and excute it in browser
This helped me to solve the issues of displaying assets (images) but problem still persists for uploading the images.
Hi, thanks for you explanation. How about change in bootstrap/ config.php? There is to several routs, isn't it? I would like to see them:) Thanks.
PHP Fatal error: Uncaught Error: Call to undefined method Composer\Autoload\ClassLoader::make() in /home/supervi7/public_html/index.php:49
Can you share more details if possible?
Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more