To address your questions and provide a solution for renaming the "public" directory to "public_html" in a Laravel project, let's break it down step-by-step.
1. Security Perspective
From a security perspective, renaming the "public" directory to "public_html" does not inherently introduce any security risks. However, you should ensure that:
- The
public_htmldirectory is the only directory exposed to the web. - Sensitive files and directories (like
.env,storage,vendor, etc.) are not accessible from the web.
2. Fixing the npm run dev Error
The error you're encountering when running npm run dev is because the Laravel Vite plugin is still trying to access the public directory instead of public_html. To fix this, you need to configure the Laravel Vite plugin to use the new public_html directory.
Here's how you can do it:
Step 1: Update vite.config.js
You already updated the outDir in vite.config.js, but you also need to update the laravel plugin configuration to use public_html.
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: ['resources/js/app.js'],
publicDirectory: 'public_html', // Add this line
}),
],
build: {
outDir: 'public_html/build',
},
});
Step 2: Update bootstrap/app.php
You already added the code to change the public path, which is correct. Ensure it looks like this:
$app->usePublicPath(realpath(base_path('public_html')));
Step 3: Update vite.config.js for Hot Module Replacement (HMR)
You need to ensure that the HMR server is aware of the new public directory. Add the following configuration to vite.config.js:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: ['resources/js/app.js'],
publicDirectory: 'public_html', // Add this line
}),
],
build: {
outDir: 'public_html/build',
},
server: {
hmr: {
host: 'localhost',
},
},
});
Step 4: Update .env File
Ensure your .env file has the correct APP_URL pointing to the new public directory:
APP_URL=http://localhost/public_html
Summary
By updating the vite.config.js file to include the publicDirectory option and ensuring the HMR server is correctly configured, you should be able to run npm run dev without encountering the ENOENT error.
If you follow these steps, your Laravel project should work correctly with the public_html directory both in development and production environments.