Deploying Laravel applications on a shared host

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

  1. Get the Laravel app
  2. The public dir content must be placed inside the public_html or www dir.
  3. Everything else (application, bundle, storage, etc) must be placed outside public_html dir, like my_apps/my_first_app/.
  4. 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.

Published by

Junior Grossi

Senior PHP Engineer with TDD focus. Husband & Dad. Creator of Corcel PHP. Organizer of PHPMG. Software Engineer at InterNACHI. Loves coffee and music.

66 thoughts on “Deploying Laravel applications on a shared host”

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

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

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

  3. I nedd to upload laravel 5 where i’m going to update paths. In bootsrtap folder i don’t have any paths.php file

    1. Hi @Sukumar!

      Check public/index.php file on L5 and you will see the path to change 😉

      Thanks for the comment.

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

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

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

    1. Hi Hiren!

      Have you checked your configuration files? What environment are you using?

      Thanks for the comment.

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

  6. Hi, This post helped me a lot. Please post more on various uses of Laravel. I simply got addicted. Have a nice time. Sanjib

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

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

      🙂

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

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

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

  10. hello i get this error message

    Unhandled Exception

    Message:

    Undefined offset: 6
    Location:

    /usr/lib/php/head.php on line 90

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

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

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

    1. 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 &#42htdocs&#42 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&#039s public content files and create another “app” folder (under /) that will store other Laravel&#039s folders and files.

      So you&#039ll have:

      • app

      • application

    2. htdocs
      • img
      • js
      • index.php

      If your host does not allow you to set the “public” folder, the last chance is using .htaccess, you&#039re 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.

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

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

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

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

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

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

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

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *