0

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

14
  • 1
    Run composer install. Commented Jun 15, 2024 at 18:42
  • 1
    share your index.php and it's location Commented Jun 16, 2024 at 5:33
  • 1
    have you tried to run composer install before uploading to cpanel ? Remember, you have to run composer install before uploading the app to the panel, because the CPANEL doesn't have composer Commented Jun 16, 2024 at 8:11
  • 1
    If you use inertia with vue then you also have to run npm run build before uploading your app to the CPANEL. The steps would be: run composer install -> run npm run build -> delete node_modules folder -> upload the app to the panel using the same directory that you use before in your question Commented Jun 16, 2024 at 8:57
  • 1
    Let me know whether you're success or not. Also remember, that you don't have to do anything to the .htaccess file Commented Jun 16, 2024 at 8:58

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

there is a subtle difference between your approach and mine, I didn't delete the node modules folder, rest was the same
You don't need the node_modules folder after running npm run build. If you don't believe me, you can test it out in your local computer. Just run npm run build and delete your node_modules folder. And see that your app is still working. That's why I always recommend to delete node_modules, because it's heavy and makes your deployment slower

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.