Laravel In Shared Hosting The Easy Way

Custom Artisan Command in Laravel

Laravel is a great PHP framework to work with. It simplifies the web application development by easing the routing, caching, dependency management, sessions and authentication. It is very easy to install and deploy laravel applications in server as it has very simplified dependency management tool. But it might feel not much simple when it have to be deployed in shared hosting where you don’t have SSH access to the server. The only way to access server is via FTP. So, here we are going to deploy laravel application in shared hosting via FTP.

First prepare our project in local machine to export to remote server. Here I’m referring to shared hosting server by remote server. Get data dump of our database from local machine including schema and data. Now login to cpanel of remote server and goto Mysql Database wizard. Here, create a database and database credentials required for our application. Now after teh database has been setup, go to phpMyAdmin from cpanel. Select the database we just created and import the sql dump we had from our local machine.

Migration

Now, connect to remote server via any FTP client and create a folder in the root folder, in same level as of public_html folder. Name the folder as laravel or anything you would like. Now upload all the files and folders of laravel application from our local machine to this folder including the content of vendors folder, except the public folder. We will move all files of public folder of laravel app to public_html folder of remote server as it will be publicly accessible resource.

root
 |-public_html
 |-laravel

We can zip the files and folder of the project to transfer to remote server via FTP, as is more convenient transferring single file that transferring each files individually. Later we’ll unzip it in the remote server after uploading.

Now grant write access to storage folder inside laravel folder.

Then update .env file with the configurations required for remote production server including database connection details.

Then update index.php file on public_html folder of remote server and update the path to autoload.php and app.php files to point to the laravel directory we created. And also bind public path to public_html folder that points to public folder in laravel by default.

require __DIR__.'/../vendor/autoload.php';
require __DIR__.'/../bootstrap/app.php';

to

require __DIR__.'/../laravel/vendor/autoload.php';
require __DIR__.'/../laravel/bootstrap/app.php';

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

The is server.php inside laravel folder, update public file path to public_html

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

to

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

Now access your domain in your web browser and you must get your laravel application up and running in your shared hosting server.

While browsing your domain, you may likely get 404 on routes other than ‘/’. To fix that, add following code inside .htaccess file in public_html folder and  try without index.php.

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule ^ index.php [L]
</IfModule>

*Updated: 25 Aug, 2019

You May Also Like