How to run Laravel application from public_html directory on shared hosting or xampp.
cd /
mkdir www
cd www
composer create-project laravel/laravel wow.test
code wow.test
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
<?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';
// });
}
Add provider in bootstrap/providers.php if not exists.
<?php
return [
App\Providers\AppServiceProvider::class,
App\Providers\ChangePublicDirectoryProvider::class,
];
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
In file C:\Windows\System32\drivers\etc\hosts
127.0.0.66 wow.test www.wow.test
Zmień plik C:\xampp\apache\conf\extra\httpd-vhosts.conf
# Load vhosts from directory
Include "conf/extra/vhosts/*.conf"
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>
# Then run in browser
http://wow.test
# 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]