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:
Note: I advise you to leave everything as it is including the .htaccess file.
Compress the entire project folder on your local machine. You'll get a zip file – laravel50.zip
Click on ‘File Manager’
Click on ‘Upload’
Upload the laravel50.zip to the root directory – not the public_html.
Extract the laravel50.zip. Your cPanel file manager should be looking something close to this:
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.
Navigate to the public_html folder and locate the index.php file. Right-click on it and select Code Edit from the menu.
This will open up another tab showing the cpanel code editor.
-
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
-
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>`
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.
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.
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.
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.
After exporting move over to your shared hosting and do the same, but this time we are using the import tool
If successfully uploaded you'll see the 'Success Message"
- 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)
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
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
Nope, I didn't. :(
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...
Illuminate \ Database \ QueryException (1045)
SQLSTATE[HY000] [1045] Access denied for user 'rozeline_row'@'localhost' (using password: YES) (SQL: select * from
users
wherenid
= 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
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,
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.
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.
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 artisanLet 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:
Output // http://www.your-domain.com/img/filename.jpg
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
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