Uploading / Configuring a Laravel 4 app on cPanel type hosting ( public_html )

Problem: This morning I wanted to quickly move a little Laravel test project onto my live server. The default file structure of Laravel stores all the framework resources in a vertical tree of files and directories, and then the ‘public’ directory is adjacent to these. Like so:

- [app]
- [bootstrap]
- [public] etc

To host it on my CentOS box running cPanel I want to adhere to the following (standardish) structure:

- [.htpasswds]
- [etc]
- [mail]
- [public_ftp]
- [public_html] etc etc varies depending on server

I could just upload everything in my Laravel project to the ‘public_html’ directory… but then to run the app I’d need to browse to http://example.com/public/. Worse still, my whole framework file-structure will be visible if I browse to http://example.com

Solution: With a little path remapping in two files, this can be easily achieved. Here’s what I did:

1) Create a new directory called ‘laravel4’ adjacent to ‘public_html’ so that your remote file structure now looks like this:

- [.htpasswds]
- [etc]
- [laravel4]
- [mail]
- [public_ftp]
- [public_html] etc etc varies depending on server

2) Upload the contents of your local ‘public’ directory to the remote directory ‘public_html’. Note: Be sure not to copy the whole ‘public’ directory, JUST it’s contents.

3) Upload everything else within your local Laravel project to the new remote ‘laravel4’ directory. Check: Now when you view the ‘laravel4’ folder over FTP/SSH/Whatever, you should see all your ‘app’ & ‘bootstrap’ directories along with all the other files and directories, but NOT ‘public’.

4) Edit the remote file ‘public_html/index.php’ and make the following change:

//from:
require __DIR__.'/../bootstrap/autoload.php';
//and
$app = require_once __DIR__.'/../bootstrap/start.php';

//to:
require __DIR__.'/../laravel4/bootstrap/autoload.php';
//and
$app = require_once __DIR__.'/../laravel4/bootstrap/start.php';

5) Edit the remote file ‘laravel4/bootstrap/paths.php’ and make the following change:

//from:
'public' => __DIR__.'/../public',
//to:
'public' => __DIR__.'/../../public_html',

6) That should be it. In my case, that was enough to restructure everything. Routes are working with the default .htaccess file. If you have problems, let me know, or ask on the ever-helpful

Comments

4 responses to “Uploading / Configuring a Laravel 4 app on cPanel type hosting ( public_html )”

  1. Jorge Murta avatar

    And if i want to update laravel with composer? Is there any problem?

  2. Andrés avatar

    hi, your configurations works me in my local server, but don’t works in my cpanel hosting :s may u help me?

  3. mohsen avatar

    hi…i did it and i get 404 error in cpanel…what should i do?

  4. Vivek avatar
    Vivek

    Hi,
    what about
    hosting-laravel-5-on-cpanel-type-hosting-public_html

    Laravel 5 Directory structure is different. Please elaborate on this.

Leave a Reply

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