Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

henrique-ferrolho's avatar

Deploy Laravel 5 to blocked DocumentRoot with only two folders: private and public

Alright, sorry if the title was confusing... Here is a more clear explanation:

I need to deploy a Laravel 5 project but I don't have write access to the DocumentRoot. The DocumentRoot has two folders: private and public, and I do have read/write access to these folders.
So, basically, I need to move every folder except the Laravel public folder, to the private folder. I have already done that and I have edited the following lines in public/index.php:

require __DIR__.'/../private/bootstrap/autoload.php';
$app = require_once __DIR__.'/../private/bootstrap/app.php';

However, when I run php artisan serve, I get the following error:

[ErrorException]
chdir(): No such file or directory (errno 2)

Thanks in advance!

0 likes
4 replies
tiagotavares's avatar

Hello!

Explanation

As the fire command used in php artisan serve command uses your laravel/application instance to change to the public folder (with chdir) and it returns an Exception because the folder does not exist in your base directory.

Illuminate\Foundation\Console\ServeCommand

...
public function fire()
    {
    // This changes the work directory. Accessing method publicPath in Class Illuminate\Foundation\Application
        chdir($this->laravel->publicPath());
    
        ...
    }
...

Illuminate\Foundation\Application

class Application extends Container implements ApplicationContract, HttpKernelInterface
{
    ...
    public function publicPath()
    {
        return $this->basePath.DIRECTORY_SEPARATOR.'public';
    }
    ...
}

Possible Solution

One of the alternatives is extending the Illuminate\Foundation\Application and overwrite the method publicPath() in your new application Class (Tested with Laravel 5.1).

You can create a new file for in your app folder for example app/MyApplication.php extending Illuminate\Foundation\Application

<?php namespace App;

use Illuminate\Foundation\Application;

class MyApplication extends Application
{

    public function publicPath()
    {
        // Your new public path
        return realpath($this->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'public');
    }
}

After this you only need to change the Class to create your app in bootstrap/app.php

// Replace this
/*$app = new Illuminate\Foundation\Application(
    realpath(__DIR__ . '/../')
);*/

$app = new \App\MyApplication(
    realpath(__DIR__ . '/../')
);

Hope it helps!

Regards
Tiago Tavares

2 likes
Snapey's avatar

I assume that this is just a development system then with you using php serve?

1 like
Snapey's avatar

well it seems odd that you are on a server with only access to public and private folders (so I am thinking shared server at this point) but then you say you get the error when you run Php artisan serve - which should be just for development?

Please or to participate in this conversation.