Puankare's avatar

Renaming "public" to "public_html"

I am very new with laravel. For my first laravel 11 project (with vsc, vite and git), I needed to modify "public" folder to "public_html", since that's what I have on my hostinger cloud folder. I found too many different recommendations on this (may be because different methods for older laravel versions?) But this is what worked for me.

In bootstrap/app.php I added:

  ->registered(function ($app) {
    $app->usePublicPath(path: realpath(base_path('public_html')));
  })

In vite.config.js I added:

  build: {
    outDir: "public_html/build",
  },

Now, I can work project locally, "npm run build", push the changes to git via vsc, and "git pull origin main" on server to update production. My only problem is when I try to "npm run dev" locally. It still tries to access "public" instead of "public_html". Here's the error:

Error: ENOENT: no such file or directory, open 'D:\xampp\htdocs\Projects\mytest\public\hot'
    at Object.writeFileSync (node:fs:2346:20)
    at Server.<anonymous> (file:///D:/xampp/htdocs/Projects/mytest/node_modules/laravel-vite-plugin/dist/index.js:103:14)
    at Object.onceWrapper (node:events:633:28)
    at Server.emit (node:events:531:35)
    at emitListeningNT (node:net:1932:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:81:21) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'D:\xampp\htdocs\Projects\mytest\public\hot'
}

My questions:

  1. Did I overlook anything in my "public" -> "public_html" renaming in general from security perspective?
  2. How can I fix the "npm run dev" error?
0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

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_html directory 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.

Puankare's avatar

Ah, yes: "publicDirectory: 'public_html', // Add this line" fixed it :)

ciroDourado's avatar

I have followed those steps, but now my home route always opens on localhost/build.. what should I do to make it behave the same way, but on localhost/?

Please or to participate in this conversation.