Hello!
Laravel is an awesome PHP framework created by Taylor Otwell. Actually, it is on the third version and it is one of the great PHP frameworks we have today.
As a lot of frameworks, we have to create our own Apache Virtual Host to point to the public dir to improve security and only allow access to really public files.
Some people asked me to create a blog post about how to deploy Laravel application to a shared host, like my post about how to Deploying Zend Framework applications on a shared host.
Laravel file structure
A basic Laravel application has the same file structure:
- application/: your app files, like controllers, routes, models, views, tasks, etc
- bundles/: the bundle’s files you’ve installed for your app
- laravel/: the Laravel’s files
- public/: your public files
- storage/: files about cache, session, logs, etc
- artisan: the Laravel command line file
- licence.txt: licence file
- paths.php: file with paths information
- readme.md: just a readme file with Laravel instructions
The reasoning is the same the Zend Frameworks’ post. I think when you login inside your shared host account you’ll see a lot of files and folders. You must save your application files inside a “public” dir. This folder’s name is something like public_html or www.
So, if you create a index.php file with the content “Hi. I’m here” inside the public_html (or www), when you visit http://example.com you’ll se that message. So, the TIP is: “The public_html folder is your Laravel public directory”.
Organizing shared host files
To give your shared host files organized, before the public_html folder (the public) we’ll create a folder called my_apps (or what you want). Inside that we’ll create another folder called my_first_app. Inside this last folder we’ll put all files and folders except the public dir. The public dir content will be placed inside the public_html dir. So we’ll have:
- my_apps/my_first_app/: here have now application, bundles, laravel and storage directories and artisan, licence.txt, paths.php and readme.md files.
- public_html/: img, js, css, index.php and .htaccess
Editing files to say Laravel where is your new files
We’ve changed the files paths, so, we must tell Laravel where is that files. Everything you need is tell Laravel where is the paths.php files, just it. So, open the public_html/index.php that is the old public/index.php file. It’s content must be something like above (take a look on 24th line):
<?php /** * Laravel - A PHP Framework For Web Artisans * * @package Laravel * @version 3.2.5 * @author Taylor Otwell <taylorotwell@gmail.com> * @link http://laravel.com */ // -------------------------------------------------------------- // Tick... Tock... Tick... Tock... // -------------------------------------------------------------- define('LARAVEL_START', microtime(true)); // -------------------------------------------------------------- // Indicate that the request is from the web. // -------------------------------------------------------------- $web = true; // -------------------------------------------------------------- // Set the core Laravel path constants. // -------------------------------------------------------------- // require '../paths.php'; // CHANGE THIS to above require '../my_apps/my_first_app/paths.php'; // -------------------------------------------------------------- // Unset the temporary web variable. // -------------------------------------------------------------- unset($web); // -------------------------------------------------------------- // Launch Laravel. // -------------------------------------------------------------- require path('sys').'laravel.php';
If you visit http://example.com you’ll render the index.php file, that was the Laravel public/index.php file. So anyone can access through the http://example.com URL the my_apps files, because them is now outside the public_html dir.
Summary
- Get the Laravel app
- The public dir content must be placed inside the public_html or www dir.
- Everything else (application, bundle, storage, etc) must be placed outside public_html dir, like my_apps/my_first_app/.
- Update the public_html/index.php file point the paths.php to it’s right path now.
Thanks for everyone. Thanks for reading and I hope the post is useful.
Hi,
I have read too many tutorials/articles/guides and also have watched to many videos about deploying/uploading Laravel project on Cpanel. And Believe me, your efforts are really BEST then all of others.
I really like the way you have explained in this tutorial/article.
Thank you so much for all of your efforts to make this one of the great guide.
Stay Blessed !
thanks for the words man!
Hello sir…
i have done all of these steps same as you said but i get http 500 error how can i solve it..
please some one help me as soon as possible
What’s the error?
I don’t get it. Why some webmasters still choose to deploy their website on shared hosting? It has serious security concerns and performance is not always good because the resources are shared. It might be cheap, but does it worth foregoing security and performance? There are other hosting options. You have DigitalOcean, which is really cheap. There is also Vultr. If deploying application on these hosting is difficult, then there is managed hosting platforms, like Cloudways, that have one click installation feature. I think webmasters should really keep shared hosting as their last option.
I agree @Smaug 😉 But sometimes this is not a webmaster’s decision but customer’s. I strongly suggest everyone to use at least a VPS server. Thanks for the comment.
Thank you @Junior Grossi:disqus. You’re a shared server hero 🙂
Thanks! You’re welcome!
I nedd to upload laravel 5 where i’m going to update paths. In bootsrtap folder i don’t have any paths.php file
Hi @Sukumar!
Check `public/index.php` file on L5 and you will see the path to change 😉
Thanks for the comment.
Thanks…
Thanks for the help on setting up for a shared hosting server. FYI, the latest version of Laravel has the paths.php file located under the bootstrap folder.
Hi David! Yes there are some differences but the thought is the same. The index.php file on public folder will require a file inside other folder. You only have to change that to the correct path. 🙂 Thanks for the comment.
Hi, i have host my laravel app on godaddy. everything works fine on local. but Auth::check is not working in view. before redirecting to view it return true but in view it always returns false. i totally messed up with with already wasted 4 days. and ya.. it doesn’t store any session cookie as well. please help me if it’s possible.
Hi Hiren!
Have you checked your configuration files? What environment are you using?
Thanks for the comment.
i’m facing the same problem now is there any solution for this………
Hi @vasu!
I’ve never had this problem before, but I think this is not a Laravel problem, but a session/cookie problem. Maybe your storage directory is not writable. Please check.
Thanks!
Thank you too much guy!! you just saved my day 🙂
Thank you Swang! Regards!
Hi, This post helped me a lot. Please post more on various uses of Laravel. I simply got addicted. Have a nice time. Sanjib
Hi Sanjib!
Thank you for the positive feedback! Welcome to the blog!
Best regards!
hi it’s me again. i have a problem when i upload the other laravel files like application etc.
the artisan,license,paths.php,readme won’t show. but in the filezilla i see it succesfully uploaded. outside htdocs. home/apps/XXX/ there’s only the folders no artisan etc, application,bundles,laravel,storage.
so i upload the laravel files inside the htdocs. like this
/htdocs/home/XXX/apps/paths.php. all files is complete uploaded but i can access the splashpage
when i go to the domainname/logi it redirect to other page.
i think the paths.php has a problem like the chdir()
i edit
Hi man!
The ideia is: your htdocs is your public folder. So it’s content must be only files and directories from your public folder.
All other files and folders must be outside the htdocs. So on the same folder you have application, bundles, etc, you must have the files too, like artisan, paths.php, etc.
🙂
hi i wondering when i upload the paths.php, etc outside the public_html
thanks for the step by step.
Thanks for the feedback Romualdo! Be welcome!
Hey this is somewhat of off topic but I was wondering if blogs
use WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding knowledge so I wanted to get guidance from someone with
experience. Any help would be greatly appreciated!
Also visit my blog: Wizard101 Crown Generator download
Hi!
I am using WordPress as blog platform with Markdown plugin. So I write the content using markdown 😀
Regards.
Wow, amazing blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your site is wonderful,
as well as the content!
Also visit my blog Despicable Me Minion Rush Hack
Hi! Thank you for your compliment.
My blog has about 2 years. This layout was created by Kovshenin. I just made only some CSS changes to me.
Welcome to the blog! Regards.
RewriteRule ^(.*)$ /index.php/$1 [R,L]
Add this in your .htaccess file. It solved the problem.
your folder structure working perfect. but when i run a function in my program it gives following error.
“No input file specified. “
can you pls help me solve this problem?
Hi Kolitha,
First thank you for the comment. I need more information to help you. Maybe you can send me some files, like the function file, a error print screen, etc. You can use http://pastebin.com.
Thanks and welcome.
hello i get this error message
Unhandled Exception
Message:
Undefined offset: 6
Location:
/usr/lib/php/head.php on line 90
Hi Luke!
You’re trying to get a array position that does not exist. Check your code and if want you can send me a link with it in pastebin.com.
Thanks for the comment!
Just an update for anyone using Laravel 4 -> http://elena-nosnaweb.tumbl…
Só um “update” para quem usa Laravel 4 -> http://elena-nosnaweb.tumbl….
Seu post me salvou Junior. Obrigada.
De nada Elena!
Seja bem vinda ao blog e qualquer dúvida estamos ai.
Um abraço e obrigado pelo comentário.
Hi My friend. Can You do that for a Laravel4.1 to ?
Thanks
Gyorgy from Hungary
Hi Gyorgy!
Sure! Tomorrow I’ll create a post about deploying in Laravel 4/4.1 too!
Thanks for the comment.
Hi Grossi. Did you ever figure out what marrons problem is. I’m getting that
Undefined offset: 6
/usr/lib/php/head.php on line 90 error as well
Hi Brod!
I think this is not a framework problem my yes a problem with your environment. If you want help send me parts of the code, your file structure and I can check that for you.
Thanks for the comment!
Dude, thx a lot for the tips.
Welcome Davis! It’s a pleasure! It’s good to read these types of comments. Thanks again man!
Thank you!! This worked perfectly.
Welcome David! I’m glad to hear this post was good for you! Thanks for the comment.
Hi grossi,
I have some problems… my hosting firm simply gives me a what they are calling “root” folder where my site resides. They dont have public_html or public etc… all i see before aploading any files is a folder with a “/” meaning root. I’m unable to go up this folder. Now i have laravel folder which contains the:
application/
bundles/
laravel/
public/
storage/
artisan/
licence.txt
paths.php
readme.md
Obviously my .htaccess resides inside “public” folder which comes by default with laravel. But apache hits the “root” of my site. How can i apply what you just said in your blog?
1st Question:
Someone suggested creating another .htacces file to redirect into the public folder (making the ‘public’ folder the ‘RewriteBase’ and leave the .htaccess file within intact to now handle all redirected requests. Is this the same as what you talked about in your blog?
2nd Question:
Would you rather I create a new folder and simply move all application files into this new folder and leave the public folder as is? I’m totally confused.
I went with the first option and currently there is no error but the page is simply blank. I’m totally bewildered !
Hi Tendai!
First you have to know how this “root” folder works. You have the / path and can create what you want inside it, right? Is there any way to, for example, create a *htdocs* folder and set it as your public folder? Is there any way to do that? If the answer is yes, I think, for security purposes, the best approach is that, you create a “public” folder, where you will put your Laravel's public content files and create another “app” folder (under /) that will store other Laravel's folders and files.
So you'll have:
If your host does not allow you to set the “public” folder, the last chance is using .htaccess, you're right. In this case you must set the new “RewriteBase” as you said. But really, because of security, if you can create a “public” path is much better.
Thanks again.
Hi Grossi
i followed your tutorial but this is what came out
Warning: chdir() [function.chdir]: No such file or directory (errno 2) in /home/a3205545/apps/laravel/paths.php on line 97
Fatal error: require() [function.require]: Failed opening required ‘/home/a3205545/public_html/laravel/laravel.php’ (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/a3205545/public_html/index.php on line 34
Hi Jim!
Thanks for comment.
First, I think you’re with paths problems. The tutorial says you must put only the content of your “public” folder inside /home/XXXXXX/public_html. After that you must create a new folder, for example “apps” in /home/XXXXXX/apps and inside that the rest of Laravel files and folders, making you having, for example, /home/XXXXXX/apps/laravel folder. Inside /home/XXXXXX/public_html edit index.php file and set the corret path to “paths.php”, that will be, in this example, “require ‘../apps/paths.php’;”.
I hope help you. If you have any questions contact me.
Regards.
http://pastebin.com/sA5vjV7c
Do you have in your root dir “core/app/application”, right? If you have I think it’s a problem with your shared host, because /usr/lib/php/head.php is a file outside your root dir. Contact me in me@juniorgrossi.com and we’ll talking about that.
Thanks.
Hi.. i got this error when i followed your instruction
Unhandled Exception
Message:
Undefined offset: 6
Location:
/usr/lib/php/head.php on line 90
Thanks
Hi Marron,
Thanks for the comment. Did your change the “path” correctly? Please, send me your index.php file through pastebin.com.
Thanks!
Thanks a lot. This really helped me. 🙂
Hi Swaroop SM. Welcome! Thanks.
When i enter to index.php y receive: Warning: require(/home/cristian/myapps/laraveltest/paths.php) [function.require]: failed to open stream: Operation not permitted in /home/cristian/public_html/laravel/index.php on line 24
Hi Cristian,
Did you change the paths.php path? Paste here the link to pastebin with your index.php content and your project structure.
Regards.
Awesome post. Nice and clear. I thought I’d throw out another helpful tidbit. I just setup a site on bluehost and had to change my PHP from 5.2 to 5.3 for my Laravel 3.2.12 installation to work.
Thank you John. I hope help you. Any question please comment or send message. Regards.
Awesome, got it working on my shared hosting service too. Thx, man. Cheers
Thanks Daniel. I hope helped you. Thanks for the comment.
What hosting did you use?
Hi Morda! I am actually using a VPS at DigitalOcean.com. Regards!
Good article!
But if you are on a linux machine and allowed to use symbolic links i would suggest that, so you wont have to change the code when deplying from development to production.
Hi Mitchell.
The ideia of this article is to deploy to shared hosts, where you are limited. If you can create symbolic link you can create a Apache virtual host, that you have more options and is better than sym links. When using shared host sometimes you don’t have access to shell so this article is for that.
Thanks for the comment.
Nice post!
I’m using shared host. so, It was very useful for me!
Thanks!
Hi haydn! Thanks for the comment.
I wish it had been very useful as you said.
Thanks.
Obrigado Junior, seu post me ajudou, mas estava pensando, não seria melhor fazer um symbolic link?
Ola Lucas!
Obrigado pelo comentario.
Sim seria, mas criar symbolic link quem tem hospedagem compartilhada nao é possivel. Se voce puder criar um vc tem acesso a shell. Neste caso é melhor ja criar um virtual host no Apache de uma vez, que te dará mais opcoes até.
Abs