Skip to content

Instantly share code, notes, and snippets.

@atomjoy
Last active April 29, 2025 09:28
Show Gist options
  • Save atomjoy/63e9e8675bcb7119192bc3cc0fbae3d0 to your computer and use it in GitHub Desktop.
Save atomjoy/63e9e8675bcb7119192bc3cc0fbae3d0 to your computer and use it in GitHub Desktop.
How to run Laravel application from public_html directory on shared hosting or xampp.

Change Laravel public directory

How to run Laravel application from public_html directory on shared hosting or xampp.

Cmd project

cd /
mkdir www
cd www

composer create-project laravel/laravel wow.test

code wow.test

Create service provider

Create symlinks from public to public_html directory in config/filesystems.php and link public dir to public_html on shared hosting.

php artisan make:provider ChangePublicDirectoryProvider

Update service provider

<?php

public function register(): void
{
    config(['filesystems.links' => [
        public_path('storage') => storage_path('app/public'),
        base_path('public_html') => base_path('public')
    ]]);

    $this->app->usePublicPath(app()->basePath('public_html'));

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

Register provider

Add provider in bootstrap/providers.php if not exists.

<?php

return [
    App\Providers\AppServiceProvider::class,
    App\Providers\ChangePublicDirectoryProvider::class,
];

Create symlinks

You must be able to add files to the directory below public_html on the server.

# In ssh terminal
php artisan storage:link

# If errors remove links first
find . -type l -ls
unlink ./public
rm -r ./public_html

Xampp local domain

In file C:\Windows\System32\drivers\etc\hosts

127.0.0.66 wow.test www.wow.test

Xampp apache2 konfiguracja vhosts

Zmień plik C:\xampp\apache\conf\extra\httpd-vhosts.conf

# Load vhosts from directory
Include "conf/extra/vhosts/*.conf"

Xampp local domain virtual host

Create directory C:\xampp\apache\conf\extra\vhosts and new file wow.test.conf

<VirtualHost 127.0.0.66:80>
    DocumentRoot "C:/www/wow.test/public_html"
    DirectoryIndex index.php index.html
    # Doamin here
    ServerName wow.test
    ServerAlias www.wow.test

    # Create first files for logs
    #ErrorLog "C:/www/wow.test/storage/logs/wow.test.error.log"
    #CustomLog "C:/www/wow.test/storage/logs/wow.test.access.log" common

    # Non-www
    #RewriteEngine On
    #RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    #RewriteRule ^(.*)$ https://%1$1 [R=301,L]

    # Redirect ssl
    #RewriteEngine On
    #RewriteCond %{HTTPS} off
    #RewriteRule (.*) https://%{SERVER_NAME}$1 [R,L]

    <Directory "C:/www/wow.test/public_html">
        #Options -Indexes -MultiViews +SymLinksIfOwnerMatch
        Options -Indexes -MultiViews +FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Allow from all
        Require all granted
    </Directory>

    <Files .env>
        Order allow,deny
        Deny from all
    </Files>

    <FilesMatch "^\.">
        Order allow,deny
        Deny from all
    </FilesMatch>

    <FilesMatch ".(jpg|jpeg|png|gif|ico|webp)$">
        Header set Cache-Control "max-age=86400, public"
    </FilesMatch>
</VirtualHost>

<VirtualHost 127.0.0.66:443>
    DocumentRoot "C:/www/wow.test/public_html"
    # Doamin here
    ServerName wow.test
    ServerAlias www.wow.test

    SSLEngine on
    SSLCertificateFile "conf/ssl.crt/server.crt"
    SSLCertificateKeyFile "conf/ssl.key/server.key"

    <Directory "C:/www/wow.test/public_html">
        #Options -Indexes -MultiViews +SymLinksIfOwnerMatch
        Options -Indexes -MultiViews +FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Allow from all
        Require all granted
    </Directory>

    <Files .env>
        Order allow,deny
        Deny from all
    </Files>

    <FilesMatch "^\.">
        Order allow,deny
        Deny from all
    </FilesMatch>

    <FilesMatch ".(jpg|jpeg|png|gif|ico|webp)$">
        Header set Cache-Control "max-age=86400, public"
    </FilesMatch>
</VirtualHost>

Restart xampp and browser

# Then run in browser
http://wow.test

Php version .htaccess

# Php 8.2
AddType application/x-httpd-php82 .php

# Remove last slash
    # DirectorySlash Off

    # Move to index.php
    # RewriteEngine On
    # RewriteRule ^index\.php$ - [L]
    # RewriteCond %{REQUEST_FILENAME} !-f
    # RewriteCond %{REQUEST_FILENAME} !-d
    # RewriteRule . /index.php [L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment