To address the issue of deploying a Laravel application on a VPS where you cannot change the document root from public_html to app/public, you can follow these steps to ensure that your assets are correctly referenced and served.
Step-by-Step Solution
-
Symlink the Public Directory: Create a symbolic link from the
app/publicdirectory to thepublic_htmldirectory. This way, the assets will be served correctly from thepublic_htmldirectory.ln -s /home/domain_name/app_name/public /home/home_name/public_html/public -
Update the
index.phpFile: Modify theindex.phpfile in thepublic_htmldirectory to point to the correct paths for the Laravel application.<?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__.'/../app_name/vendor/autoload.php'; /* |-------------------------------------------------------------------------- | 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__.'/../app_name/bootstrap/app.php'; /* |-------------------------------------------------------------------------- | 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); -
Configure Vite: Ensure that Vite is configured to output assets to the correct directory. Update your
vite.config.jsto set the correctoutDir.import { defineConfig } from 'vite'; import laravel from 'laravel-vite-plugin'; export default defineConfig({ plugins: [ laravel({ input: ['resources/js/app.js', 'resources/css/app.css'], refresh: true, }), ], build: { outDir: '../public_html/public/build', emptyOutDir: true, }, }); -
Update Asset URLs: Ensure that your asset URLs in your Blade templates are correctly referencing the
public_html/publicdirectory.<link rel="stylesheet" href="{{ asset('public/build/app.css') }}"> <script src="{{ asset('public/build/app.js') }}" defer></script>
By following these steps, you should be able to deploy your Laravel application on a VPS with the given directory structure and ensure that your assets are correctly referenced and served.