loading...

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

asapabedi profile image Abednego Edet Updated on ・3 min read

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

Posted on Mar 3 '18 by:

Discussion

markdown guide
 

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.

 

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.

 

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

 

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

package to run migrations and upload files without ssh

 

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

 

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.

 

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.

 

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

 
 

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

 

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
 

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.

 
[deleted]
 

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

 

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,

 

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

 
 

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?

 

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

 

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.

 

HI,
Thank you for the tutorial. But as I did the same that you have mentioned, all my css, js, images are not getting displayed on the browser. What should I do, as I have checked my path also. Please help.

 

how to generate key ,

as per instructions this command is needed Run this command for key generation
php artisan key:generate
Migration and seeding
php artisan migrate --seed
Installing dependencies
composer install
Folder Permissions
sudo chmod -R 777 storage bootstrap/cache

 

i am trying to deploy a laravel application on hostinger.in. everything is working fine except one thing.the image uploaded by the user is stored on my files but cannot be viewed.i have stored all the files in the public folder into public_html folder and rest of the files are on the root directory i tried symlink yet it was of no use,please help

 

I am deploying on a sub domain using laravel 5.7, but I am unable to locate the index.php file in root directory of subdomain.

 

Ok, the index.php is located in public directory, but the listed changes is not making the app running.

 

Hi, Thanks for saving my day...!
I have an ultimate (goDaddy) plan where i host multiple web apps. That is add on domains are added and those directories are inside public_html. What will I do in this case? Where i need to put project folder and contents in public directory?

 

Thanks for the great article. I will say right away that Pkhp and Laravel are not the best option for web development at the moment. But he himself came across this and worked. But personally, I used hosting host4.biz/en/hosting/shared-hosting (ssd web hosting). So I will say that I did not experience problems with the installation of Laravel. Although a lot depends on the hosting and their configuration.

 

SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO) (SQL: select * from sessions where id = eRLLuTsdfGhYugsApdfgsdfgujtikyuh2ygfiAZ9ePXig limit 1)

 

Thanks a lot. I was having a problem deploying a laravel project of mine. But now it's fixed.

 

You are welcome, am glad to hear that,
Cheers :)

 

Nice! This article really helped out, Thanks

 

Cheers. Am glad it worked for you

 

when i'm public folder content move public_html but more than 2 project how to handle this

 

add

Order allow,deny
Deny from all

to the .htaccess file to prevent your .env file from being publicly accessible

 
 

If we're making changes to the path of Laravel's public directory. Wouldn't it be wise to let Laravel know about it by binding the paths as discussed in the given below article?

shade.codes/how-to-deploy-laravel-...

 
 

Thank you! I needed it to start a project. I explain to clients that a shared hosting has its limitations, it is good to start but I also make recommendations to improve over time.

 
 

This really help me on a no-framework php I created.

 

Glad this is working for us 😊

 

Totally works like a charm ... Thanks for sharing

 

Am glad it also worked for you. Cheers :)

 

Hi. I tried this. but when i open the page, it is blank. I checked source, and It did not render my component

 

I've Laravel 5.6 application and I was following your steps, but there is nothing showing when I open my application site in browser. It's blank. Can you please help me?

 

Try to view source code in browser emulator