atomjoy / laravel-change-public

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

Home Page:https://github.com/atomjoy/laravel-change-public

Repository from Github https://github.comatomjoy/laravel-change-publicRepository from Github https://github.comatomjoy/laravel-change-public

How to change Laravel public directory

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

Laravel application directory

Only if you can add Laravel files below public_html in your hosting using FollowSymlinks or SymLinksIfOwnerMatch.

# Xampp example
composer create-project laravel/laravel D:/www/example.org

Update AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
use URL;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        // Create symlinks from public to public_html directory in config/filesystems.php
        config(['filesystems.links' => [
            public_path('storage') => storage_path('app/public'),
            base_path('public_html') => base_path('public')
        ]]);

        // Rewrite public dir to public_html shared hosting
        $this->app->usePublicPath(app()->basePath('public_html'));

        // Or rewrite public dir to public_html shared hosting
        // $this->app->bind('path.public', function () {
        //     return base_path() . '/public_html';
        // });
    }

    public function boot(): void
    {
        // Laravel force https
        // if (App::environment(['staging', 'production'])) {
        //     URL::forceScheme('https');
        // }
    }
}

Create symlinks

If you don't have ssh and composer, execute it locally and copy the files to the server, first deleting public_html from the server. You must be able to add files to the directory below public_html on the server.

php artisan storage:link
php artisan config:clear

Xampp (LAMP)

Add local domain

Add host in: C:\Windows\System32\drivers\etc\hosts

# Local domain
127.0.0.100 example.org www.example.org

Include vhosts directory

C:\xampp\apache\conf\extra\httpd-vhosts

Include "conf/extra/vhosts/*.conf"

Domain local vhost

C:\xampp\apache\conf\extra\vhosts\example.org.conf

<VirtualHost 127.0.0.100:80>
    DocumentRoot "D:/www/example.org/public_html"
    DirectoryIndex index.php
    # Doamin here
    ServerName example.org
    ServerAlias www.example.org

    # Create first files for logs
    #ErrorLog "D:/www/example.org/storage/logs/example.org.error.log"
    #CustomLog "D:/www/example.org/storage/logs/example.org.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 "D:/www/example.org/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.100:443>
    DocumentRoot "D:/www/example.org/public_html"
    # Doamin here
    ServerName example.org
    ServerAlias www.example.org

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

    <Directory "D:/www/example.org/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>

Install Vue 3

How to add Vue 3 in Laravel application: https://github.com/atomjoy/laravel-vue3-vite-config

Hosting settings

You must be able to add files to the directory below public_html on the server. On small hosting when you add envs dir for the domain in the admin panel.

Laravel public directory .htaccess

# Run php 8.2, 8.3 or 8.4 on small hosting
AddType application/x-httpd-php82 .php

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

# Force https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

# Cache images, css, js
<filesMatch ".(jpg|jpeg|png|gif|ico|webp)$">
    Header set Cache-Control "max-age=86400, public"
</filesMatch>

# Laravel default .htaccess
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

About

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

https://github.com/atomjoy/laravel-change-public


Languages

Language:PHP 100.0%