Skip to content

Instantly share code, notes, and snippets.

@Repox
Last active November 11, 2023 11:59
Show Gist options
  • Save Repox/d63e2a173c5eb6f4d2ae4ad52ef7c2e9 to your computer and use it in GitHub Desktop.
Save Repox/d63e2a173c5eb6f4d2ae4ad52ef7c2e9 to your computer and use it in GitHub Desktop.
Laravel 7 - Alternative public folder for shared hosting

The point of this Gist is to help setting up Laravel on a shared hosting setup, where you'd might need to have your ./public folder in another structure due to the hosting setup, but still keep the application itself out of the document root/public html folder.

The directory structure presented below is just an example, showing the document root/public html folder next to the Laravel application. The configurations displayed in the files below matches this structure.

Additionally, the changes in server.php allows for you to have the same directory structure when working locally with artisan serve (if you are using this).

I've added // comments in the files below on the lines where I changed any references and I've marked, in the tree structure below, where you find the files you need to update.

shared_hosting/ <- Your root directory (not document root/public html folder).
├── laravel/
│   ├── README.md
│   ├── app/
│   │   └──Providers/
│   │       └── AppServiceProvider.php <- Change this file
│   ├── artisan
│   ├── bootstrap/
│   ├── composer.json
│   ├── composer.lock
│   ├── config/
│   ├── database/
│   ├── package.json
│   ├── phpunit.xml
│   ├── resources/
│   ├── routes/
│   ├── server.php <- Change this file
│   ├── storage/
│   ├── tests/
│   ├── vendor/
│   └── webpack.mix.js
└── public_html/
    ├── favicon.ico
    ├── index.php <- Change this file
    ├── robots.txt
    └── web.config
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
// Override the default public_path()
$this->app->bind('path.public', function () {
return base_path().'/../public_html'; // Ensure that the path reaches your new document root/public html folder
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <[email protected]>
*/
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/
require __DIR__.'/../laravel/vendor/autoload.php'; // Point this to the Laravel folder in your shared hosting directory
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../laravel/bootstrap/app.php'; // Point this to the Laravel folder in your shared hosting directory
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <[email protected]>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/../public_html'.$uri)) { // Needs to point to your new new document root/public html folder
return false;
}
require_once __DIR__.'/../public_html/index.php'; // Needs to point to your new new document root/public html folder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment