DEV Community

Abednego Edet
Abednego Edet

Posted on • Updated on

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

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:

  1. How to deploy your app to shared hosting.

  2. 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

    `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.

                     `<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>`
    
  2. 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.

Top comments (60)

Collapse
 
bocanhcam profile image
bocanhcam

It's ok on my local. but when i upload it on my hosting, I have this error.

Forbidden
You don't have permission to access /index.php on this server.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

Can you please tell me how to fix it. thank you.

Collapse
 
rolmax1 profile image
Romax

I had this error. Firstly, there could be a file on the host that is already rerouting you via the .htaccess. After sorting this, then my host required a php.ini in the root directory and this needed to be a "production one" as opposed to a local one. Then bootstrap/cache/config.php had hard-coded references to my local PC, but not compatible with the hosting platform. With that the 500 Internal Service error was gone.

Collapse
 
edet profile image
Abednego Edet

Change the file access permissions for public folder, the permission error will disappear, everything should work normally now, unless you missed a step :)

Collapse
 
faryar76 profile image
faryar

also you can use this --> github.com/faryar76/lrm

package to run migrations and upload files without ssh

Collapse
 
eflames profile image
eflames

Hi,

How about having more than 1 laravel app in the same shared hosting?

I'm trying to upload a second app using a subdomain for example:

test.mydomain.com points to public_html/test

But does not works for me... but if I upload a HTML file with dummy text and I type mydomain.com/test it's ok!

How can I have more than 1 laravel app (different files, different public folder, different everything) in the same shared hosting (CPANEL)

Thanks u and sorry for my poor english.

Collapse
 
marvelxy profile image
Marvelous Akpotu • Edited

You can do this with a subdomain or addon domain. When you have your subdomain or addon domain ready, follow the author's step. The only difference is that you do everything in the directory created by your subdomain or addon domain.

Don't forget to edit these two lines of codes to match your directory structure:

require DIR.'/../vendor/autoload.php';
$app = require_once DIR.'/../bootstrap/app.php';

I strongly recommend setting this up on a local server like xampp before uploading it to your server.

Collapse
 
mahmudulhassan5809 profile image
Mahmudul Hassan

Hello did you solve it if any one have more than one app

Collapse
 
eflames profile image
eflames

Nope, I didn't. :(

Collapse
 
_shahroznawaz profile image
Shahroz Nawaz

I never recommend shared hosting for Laravel applications. There are so many problems with it. Instead you can use DigitalOcean, Linode or Vultr. Amazon web services is a good choice. You can also checkout Cloudways for one click deployment of Laravel Applications:

cloudways.com/blog/install-laravel...

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
rownak1812 profile image
Rownak Afrin

Illuminate \ Database \ QueryException (1045)
SQLSTATE[HY000] [1045] Access denied for user 'rozeline_row'@'localhost' (using password: YES) (SQL: select * from users where nid = 55555555 limit 1)
... I have got stuck here. rozeline_row is granted with all the privileges. what to do now?

Collapse
 
edet profile image
Abednego Edet

The error is self explanatory, it means you’re using a wrong password for the specified database user, make sure the password matches the database configuration in your .env file

Collapse
 
vicoerv profile image
Vico

I have error Class 'PDO' not found, is that exception related to this tutorial?

Collapse
 
edet profile image
Abednego Edet

Nah, I think this is as a result of bad PDO installation or php.ini config file, try to install or reinstall PDO extension then restart server.
The error actually means PDO class does not exist,

Collapse
 
riyadhahmed profile image
Riyadh Ahmed • Edited

Hello,
I have developed a laravel project first time. It works fine in local.

I have VPS-1000 hosting from inmotion inmotionhosting.com/managed-vps-ho...

Which method i should follow. is it a shared hosting?

So far i tried your mentioned technique

create a folder in root folder laravel and upload all my projects files into it
then move public folder to public_html/subdomain folder

change the autoload and bootstrap path in index file (followed this 4. change the following lines (22 and 36))

update my env and config/database file with my database credential

it given an error : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema =

used localhost instead 127.0.0.1 and also tried to change port 3306 to 33060 but failed

also i have tried.. upload full projects into public_html/subdomain folder and use htaccess to locate public index file. But same error.

Can you please tell me how to fix it. thank you.

Collapse
 
nepktoab profile image
Aneel

It seems to be working fine but one problem is that while I am trying to delete some resource lets say Post. I have used:

if($editor->image != null) {
unlink(public_path().'/uploads/editor/'.$editor->image);
}

I am unable to go to that path where the image is located. How to reach that path so that I can unlink it.

Collapse
 
silverman42 profile image
Sylvester Nkeze

After using your method to deploy my laravel app, all the pages loaded clean and easy. Big thanks for that bro. Although i have an issue with files i store in the storage/app/public directory. Uploading images to that directory is always successful but it seems impossible for me to view images from that directory to the blade view

Storage::disk('local')->url('file.jpg') seems to work on my local server but does not work on the remote server.

I did not use asset('storage/file.jpg') because I am not permitted to create a symlink on the host with ssh or php artisan

Collapse
 
edet profile image
Abednego Edet • Edited

Let us take a look at the default Laravel 5 application structure:

app //directory
contains restricted server-side application data

app/storage //directory
contains a writeable directory used by Laravel 5 and custom functions to store data ( i.e. log files, etc... )

public //diectory
this directory is accessible to anyone on the web.

If I were you, I would upload the file to the public directory directly.

Try this:

  1. Store image here: public_path() . 'img/filename.jpg'
  2. Save the 'img/filename.jpg' in database
  3. Generate the image URL with url('img/filename.jpg') Output // http://www.your-domain.com/img/filename.jpg
Collapse
 
arif98741_47 profile image
Ariful Islam • Edited

Great man. I finally uploaded using this way. Everything was ok. But server gives me error 500. I didn't find out. So I go to config/app.php and made debug true to find out error and set up app-key by hard coding. Then running ok

Collapse
 
edet profile image
Abednego Edet

Great!

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