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.