Search code examples
laravelvue.jscpanelinertiajs

deploy laravel 11 (including inertia and vue) on shared hosting


this laravel project works smoothly on local but I cant figure out how to run it on server, I tried so many tricks, nothing worked

I uploaded the whole project on root(directory above the public_html) inside a folder named laravel,I did everything about database and .env

then I moved content of public folder to public_html directory and set the path inside index.php __DIR__.'/../laravel/vendor/autoload.php';, and also for path to bootstrap and storage

server runs on liteSpeed, here is the .httaccess codes

<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]

index.php

<?php

use Illuminate\Http\Request;

define('LARAVEL_START', microtime(true));

// Determine if the application is in maintenance mode...
if (file_exists($maintenance = __DIR__.'/../laravel/storage/framework/maintenance.php')) {
    require $maintenance;
}

// Register the Composer autoloader...
require __DIR__.'/../laravel/vendor/autoload.php'; // bootstrap folder of laravel is ther

// Bootstrap Laravel and handle the request...
(require_once __DIR__.'/../laravel/bootstrap/app.php')
    ->handleRequest(Request::capture())

;

but I still get HTTP ERROR 500 on web page and siteurl/public automatically adds to URL(/public)!

and inside cPanel in a txt file named error_log, I get

[15-Jun-2024 15:14:17 UTC] PHP Warning:  require(/home/klassir2/public_html/../laravel/vendor/autoload.php): Failed to open stream: No such file or directory in /home/klassir2/public_html/index.php on line 13

has anybody successfully deployed laravel11 with inertia.js on shared hosting?

thank you


Solution

  • This process is the same for deploying all of laravel application, whether it's in cpanel, cyberpanel, or hpanel. But with inertia, you just have to build and bundle the frontend before deploying.

    All you have to do is to do this step, one after another (in sequence),

    1. build and bundle your frontend by running command npm run build
    2. Install the php dependencies by running composer install
    3. Delete your node_modules folder

    After do all the steps above in sequence, you can zip up your laravel project, inside your root directory(not inside public html) make a folder name it laravel, upload and unzip it there enter image description here

    move the content of public directory into public_html(but don't delete the empty public folder which is inside the laravel folder) enter image description here

    then create your database and its user and password, import your databse to phpmyadmin then update the .env

    if you use mysql you can edit your .env file with your database info:

    DB_CONNECTION=mysql
    DB_HOST=localhost
    DB_PORT=3306
    DB_DATABASE="mydatabase"
    DB_USERNAME="user"
    DB_PASSWORD="password"
    

    then update the index.php inside the public_html directory refering to your laravel project folder that you've uploaded, like this:

        if (file_exists($maintenance = __DIR__.'/../laravel/storage/framework/maintenance.php')) {
        require $maintenance;
    }
    
        // refering to that specific folder
        require __DIR__.'/../laravel/vendor/autoload.php';
    
        (require_once __DIR__.'/../laravel/bootstrap/app.php')
    

    Now if you type the address of your site in browser you'll get vite manifest not found at: just copy the build folder from public_html directory to public folder of laravel directory(you unzipped there) just keep manifest.json file enter image description here

    you did it!

    kudos to this question author @ramin safari by being active and responsive author and making his time to making the basic of this guide (all i have to do is editing his guide). Just in hope that both of our efforts will be able to guide others in the future.