Moving Laravel 3 public folder on shared hosting

Mar 29, 2013

As the official Laravel server configuration documentation suggests, your server should be configured to serve files from your application's public directory. On hosts that don't allow you to change the document root (e.g. on shared hosting), alternative solutions are provided. These either require shell access, or installing Laravel to a publicly visible directory, which might pose a security risk.

This article will explain how you can move Laravel's public directory to the public directory of your hosting account, but keep your application and framework code private without requiring administrator or shell access.

Moving the files

For the purposes of this article, it is assumed that the public directory of your hosting account is public_html, and the name of your application is mywebapp.

  1. Go into your hosting account and open the file manager
  2. Create a directory with the name of your project (e.g. mywebapp) inside your home directory
  3. Copy everything from your Laravel application, except the public directory, to mywebapp
  4. Copy all files from Laravel's public directory to the public_html directory
  5. Edit the paths.php and index.php as noted below

You directory structure should look similar to this:

  • /home3/username/ (your home directory)
    • mywebapp/ (directory you created)
      • application/
      • laravel/
      • storage/
      • paths.php (edit this)
      • ...
    • public_html/
      • css/
      • img/
      • js/
      • index.php (edit this)
      • ...

Editing the paths

With the files in place, you will need to edit the paths.php and index.php as following:

mywebapp/paths.php

// --------------------------------------------------------------
// The path to the public directory.
// --------------------------------------------------------------
$paths['public'] = '../public_html';

public_html/index.php

// --------------------------------------------------------------
// Set the core Laravel path constants.
// --------------------------------------------------------------
require '../mywebapp/paths.php';
Note: Replace mywebapp with the name of your application.
comments powered by Disqus