How to Relocate the ‘public’ Directory in Laravel

Updated: January 16, 2024 By: Guest Contributor Post a comment

Overview

When developing applications with Laravel, you might find yourself in a scenario where the default public directory structure does not align with your hosting environment or personal preferences. While Laravel ships with a prescribed way to structure your application’s directories, it’s flexible enough to accommodate changes, including moving the public directory. In this tutorial, we will cover how you can safely relocate the public directory in Laravel.

Understanding the Public Directory

The public directory in Laravel is the document root of your application. It is the entry point for all requests to your application and contains the front controller (index.php), assets (like CSS, JavaScript, images), and other public files.

Prerequisites

  • A Laravel application installed on your local development environment or a web server
  • Understanding of Laravel’s structure and how the routing works

Step-by-Step Instructions

Step 1: Create a New Public Directory

First, you need to decide where you want to move the public directory. For this tutorial, we’ll assume you’re going to move it to a directory named public_html.

mkdir public_html

Step 2: Copy Assets to the New Directory

Next, move or copy all the assets from the old public directory to public_html. This includes the .htaccess file, index.php, robots.txt, and the web.config file, if you’re using IIS.

cp -R public/* public_html/

Step 3: Update Index.php

In the public_html/index.php file, update the paths to autoload the Composer dependencies and to bootstrap the application.

<?php

require __DIR__.'/../vendor/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
)->send();

$kernel->terminate($request, $response);

Change the paths from __DIR__.'/../bootstrap/app.php' and __DIR__.'/../vendor/autoload.php' to the appropriate relative paths based on the new location of your public_html directory.

Step 4: Set the New Public Path

In your bootstrap/app.php, bind the new public path to the Laravel application.

$app->bind('path.public', function() {
    return base_path().'/public_html';
});

Step 5: Update Configuration for Assets

If you use the asset() helper function, no changes are necessary. Laravel’s asset() helper generates URLs for your application’s deployed assets, which typically are stored in the public directory. Since you’ve changed the public path, though, you may need to clear the cache:

php artisan config:cache

Step 6: Update the Server Configuration

This step will vary depending on your web server. For an Apache server, update your .htaccess file or server configuration file to point to the new public_html directory.

For Nginx, you would need to update your/nginx.conf file or any specific configuration files for your website to change the root directive.

Advanced Considerations

For more advanced configurations, such as when deploying with Docker or using virtualization technologies, the methods can be more complex and depend highly on the specific details of the environment.

Maintaining the Default Directory Structure

If you prefer to keep the default directory structure and are just looking to create an alias or symbolic link called public_html that points to the public directory, the approach is simpler:

ln -s public public_html

Conclusion

Relocating the public directory in Laravel can be a straightforward process when following these steps. Just remember to carefully update your server configurations and test thoroughly to ensure all assets and public-facing entry points are functioning correctly.