Deploying Laravel 5 applications on shared hosting without the use of SSH

March 3rd, 2018
3
min read
Laravel Banner
Laravel Banner

First, it is well discouraged to use a shared hosting for your Laravel 5 app. There are other very cheap web hosting starting from USD $7 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 particularly had to host an app recently on a shared host. I noticed there were fragmented and incomplete articles on how to this on the Internet. So I decided to come up something complete.

I will cover the following here:

  • How to deploy your app to shared hosting.

  • How to migrate your databases without running migration commands!

Step one:

I assume you've finished building your app – at least a functional app that is working on your localhost. I also assume you are using Laravel 5.0 – although this article would be relevant for Laravel 5.1.

Let's say your laravel project is named laravel50 with the following folder structure:

Your Project Structure

Note: I advise you to leave everything as it is including the .htaccess file.

  1. Compress the entire project folder on your local machine. You'll get a zip file – laravel50.zip

  2. Open your shared hosting cPanel. cPanel Home on shared hosting

  3. Click on ‘File Manager’

  4. Click on ‘Upload’

  5. Upload the laravel50.zip to the root directory – not the public_html.

  6. Extract the laravel50.zip. Your cPanel file manager should be looking something close to this:

cPanel File manager

  1. Open the laravel50 folder and MOVE the CONTENTS of the public folder to your cpanel’s public_html folder. You can as well delete the empty public folder now.

  2. Navigate to the public_html folder and locate the index.php file. Right-click on it and select Code Edit from the menu.

  3. This will open up another tab showing the cpanel code editor.

  4. change the following lines (22 and 36) from

index.php
require __DIR__.'/../bootstrap/autoload.php';
        ...
$app = require_once __DIR__.'/../bootstrap/app.php';

        to

require __DIR__.'/../laravel50/bootstrap/autoload.php';
        ...
$app = require_once __DIR__.'/../laravel50/bootstrap/app.php';

NB: According to your projects folder name

  1. Please do not change the contents of your .htaccess file (Unless you know what you are doing 🙂) the .htaccess file should look something like this.
.htaccess
<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>
  1. 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.

Migrating your tables

One sweet thing about Laravel Framework is that it enables you to quickly setup defined databases with a single command php artisan migrate. Since we are using shared hosting, we can not do this without the use of SSH. This can be done easily if you have up to 10 tables. Its straightforward so let's start:

Create a database on your web host.

  1. Most cPanel comes with PHPMyAdmin and Mysql Database Wizard. Use the Mysql Database wizard to create a [Database and User] then assign the user to the database allowing all privileges. Note down the username and password you'll need that soon.

  2. Use the cpanel's PHPMyAdmin to create your tables. To do this efficiently, open up PHPMyAdmin on your local machine. For each table structure create the exact structure on your cPanel’s PHPMyAdmin. This is an Example

  3. There's another way of importing and exporting databases from a local machine. login to PHPMyAdmin from your computer locate the database your project uses, from the toolbar find the export tool.

This is an Example After exporting move over to your shared hosting and do the same, but this time we are using the import tool

This is an Example If successfully uploaded you'll see the 'Success Message"

  1. On your cpanel file manager, navigate to laravel50 (or your laravel project directory). Go to config/database.php . Right-click and select ‘Code Edit’. Locate line 55 – Your MySQL configuration Section.

Change the username and password to your MySQL username and password from your cpanel (You noted Earlier).

Ensure that these details are entered correctly.

If all goes well, you should have your site working fine now. So go ahead and try it out.