How to deploy Laravel App on Shared Hosting.

There can be various reasons that can lead you to use a Shared hosting to deploy Laravel App (application), though it’s not recommended. Shared hosting providers bunch or group many of their clients into a single server to process requests for each of them, thus called Shared Hosting. So, it’s hard for them to change the behaviour of the server for a single client. Since that can affect the other clients using the same instance of the server and lead them into trouble. Sometimes opening a ticket to your hosting provider works as they transfer you to another server that can run your stack or application. Although if they’re unable to do this, relax, You can still use that same server to deploy your Laravel app.

There are many ways, methods or tutorials available on the internet to make it possible. Some of them make use of .htaccess file whereas some prefer to move the files of the public directory to the root directory of the Laravel application. Although doing any of the listed above procedure is dangerous and can make your website vulnerable to attacks. The public directory was chosen as an entry point on purpose to avoid inappropriate access to the existing files. While moving files out to the root directory would just break this barrier the Laravel developers have set. And it will lead your Laravel application to vulnerabilities and might break down the application itself.

Many shared hosting makes use of public_html or www directory to serve the application. So it would make more sense to utilize this directory provided by the shared hosting and use it instead of the public directory.

First and foremost, begin with uploading your project to the very root directory of the hosting where the public_html directory is located. Like in the example given below, I’ve uploaded my demo project. Sure, your project name could be anything.

Laravel Shared Hosting Deployment.
Laravel Shared Hosting Deployment.

Once uploaded, we’re good to move to the next step which is to copy all the files located in the public directory of your Laravel application to the public_html directory. So, the index.php file of the public directory of your application serves as the index.php file of the public_html directory. In the PHP application, index.php file is always the entry point.

After copying the files of your public directory to the public_html directory. We need to make a few changes into your application to make it work. We need to somehow tell Laravel about where to look for the application files from the public_html directory and vice-versa. Open up the index.php file you just copied along with the other files to the public_html directory in your favourite editor and change the line that says.

require DIR.'/../vendor/autoload.php';

To,

require DIR.'/../demo/vendor/autoload.php';

So, it looks something in the screenshot given below. Of course, replace demo with your project name. Here I have set demo based on my project.

Laravel Shared Hosting Deployment - Index.php
Laravel Shared Hosting Deployment – Index.php

Next, proceed and do the exact same thing for the line that says.

$app = require_once __DIR__.'/../bootstrap/app.php';

To,

$app = require_once __DIR__.'/../demo/bootstrap/app.php';

Make sure to replace the demo with your project name.

Laravel Shared Hosting Deployment - Index.php
Laravel Shared Hosting Deployment – Index.php

Once done, we need to explain Laravel application about the new path of the public directory that we want to set is public_html. That can be achieved by adding the code below right after the application is bootstrapped on line number 39 in index.php.

$app->bind('path.public', function() {
    return __DIR__;
});

After making the above changes the code inside the index.php file would look something like this.

Laravel Shared Hosting Deployment - Index.php
Laravel Shared Hosting Deployment – Index.php

We’re almost done. Last-step would be to do a quick change in the server.php file located inside your application. Go back to the project’s root directory and look for the file server.php. Once found, open it up in your favourite text editor to explain the application’s server about the public_html directory. Look for the code given below in the server.php file.

if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}

require_once __DIR__.'/public/index.php';

And replace the public directory with the new path of the public_html directory.

if ($uri !== '/' && file_exists(__DIR__.'./../public_html'.$uri)) {
    return false;
}

require_once __DIR__.'./../public_html/index.php';

Finally, the code inside the server.php file might look like the one given below.

Laravel Shared Hosting Deployment - Server.php
Laravel Shared Hosting Deployment – Server.php

After making these changes your application would be all set and be ready to work. Though you might need to do some more steps such as Installing Composer Packages, Installing Javascript Packages, Bundling up Javascript Packages, Database migration, Database seeding, Symlink, etc. depending upon your application.

Sometimes, your Shared hosting provider does not provide the Composer and NPM libraries preinstalled in the server. In that case, You can always look for the packages Composer & NodePHP developed by me to solve these issues respectively. These packages install a copy of Composer and Node JS right in your project. So, You don’t have to have these preinstalled in the server.

That’s it my friends. Let me know, how was your journey of deploying your Laravel application on Shared Hosting in comments below. Last but not least a tight hug for deploying your new Laravel application.

via GIPHY