SurajKumar's avatar

How to deploy laravel 9 on shared hosting

I want to deploy it on shared hosting. can anyone tell me the procedure?

0 likes
21 replies
vincent15000's avatar

@tisuchi I only use shared hosting (because I don't need more for very little applications) and it works very well.

1 like
vincent15000's avatar

It's not difficult.

Assuming :

  • you can connect to your shared webhosting using ssh, but if not, you have to retrieve all files from your project via FTP
  • you have your code on a git repository
  • your shared webhosting has PHP 8 or later

Here are the steps you should follow.

// clone your repo
git clone your_repo_link

// go to the app folder
cd your_app_folder

// install composer https://getcomposer.org/download/
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

// install the laravel packages
composer install

// configure the .env file, here you have to configure the database credentials, it depends on your webhoster

// then you have to link your domain with the public path your app
3 likes
kokoshneta's avatar

@vincent15000 It also assumes you have access to anything outside your public folder (many hosts do not allow this on shared hosting). Without that, you’re more or less screwed, at least if you want a secure install.

2 likes
SurajKumar's avatar

@jlrdw i want to configure it on apache based shared hosting like hostinger

1 like
SurajKumar's avatar

@vincent15000 this is folder older version laravel 8. Laravel 9 has vite intsalled for breeze. The path is broken

1 like
SurajKumar's avatar

when i changed the public directory outside my laravel app in shared hosting. My vite files are not rendering and my breeze is not running

1 like
kokoshneta's avatar

@SurajKumar What exactly do you mean by “changed the public directory outside my laravel app in shared hosting”?

  • When you say ‘public directory’, do you mean the document root, i.e., the directory where the server looks for files that should appear in / on the website (and often called public on servers)?
  • Did you change this in the server configuration, so that the server uses a different directory than the original one – and if so, which directory have you set it to now? Or did you rename the directory? Or something else?
  • Where on the server did you install Laravel?
1 like
SurajKumar's avatar

@kokoshneta My folder structure in shared hosting

Public_html -index.php -assets -build -.ht-access Laravel App -.env file -routes -resourses -views -storage

now i have installed breeze package in it. in layouts folder app.blade.php file has vite path @vite(['resources/css/app.css', 'resources/js/app.js'])

when i run it on production it show vite path error.

I think the build file in public has to do with as i changed my structure in shared hosting as shown above. Please help me out?

1 like
kokoshneta's avatar

@SurajKumar It’s quite hard to read your list of directories because they’re all on one line – you can use three backticks (```) before and after a block of text to make it appear as a code block where line breaks are honoured.

If I understand you correctly, your document root is public_html (this is common), so when you visit www.yoursite.com, it opens public_html/index.php. And then you have Laravel installed directly inside the public_html folder – is that correct? So then what is Laravel App, and where is your Laravel app’s public folder?

If my guess is correct, your installation is completely insecure! You should never install Laravel like that. That is precisely why many shared hosting servers are not suitable for Laravel.

If you install Laravel inside your document root, everything becomes accessible, including your config files, .env files, resources, anything you have passwords in, etc. HUGE security hole. Laravel relies on having a lot of stuff outside the document root, where it cannot be reached from the browser.

You need to find out whether you have access to either of the following on your server:

  1. Can you access the folder above public_html? If you can, that is where you should install Laravel.
  2. Can you change the document root on your server? If you have some sort of admin panel that allows you to change server settings, it may be possible to change which directory the server uses as its document_root. If you can do that, you should point it to your Laravel app’s public folder (which appears to be missing in your directory list above).
1 like
SurajKumar's avatar
Public_html 
		-index.php 
		-assets 
		-build 
		-.ht-access 
Laravel App 
		-.env file 
		-routes 
		-resourses 
		-views 
		-storage

This is my directory structure

1 like
kbzone's avatar

@vincent15000 i did it, but i think that the principal problem is my folder structure.

i followed some tutorials to upload my project into a shared hosting, and i have this:

...

[laravel-project] <-- all laravel files, except "/public" folder

[public_html] <-- all /public folder content

...

i edited my index.php in /public_html/index.php to pointed to /laravel-project but still @vite tries to search the manifest.json file in /laravel-project/public/build/manifest.json, that actually is in /public_html/build/manifest.json

1 like
hassanshahzadaheer's avatar

@kbzone please follow this for resolving both npm run build and htaccess issue to I just lunch my app to Hostinger shared hosting env https://jobs.aheer.me/

.htaccess Configuration

My Issue was : 403 Forbidden Error

.htaccess File:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirect to the public directory
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ /public/ [L,NC]

    # Handle Laravel routing
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ public/index.php [L]
</IfModule>

**For the next problem **

My Issue was : Vite manifest not found at: /public/build/manifest.json

Since, Hostinger or other shared hosting providers may allow to run npm commands directly on the server, In this case we need to build the production assets locally and then upload them.

Steps:

  1. Run Vite Build Locally:

    On your local development machine, navigate to your Laravel project's root directory and run:

    npm run build
    

    This command generates the necessary production files in the public/build directory.

  2. Upload build Directory:

    Use the file manager provided by your hosting control panel to upload the entire build directory in to your public folder public/build directory from your local machine.

please give it a try and thank for your time

1 like
dedanirungu's avatar

Add .htaccess file on the project root folder with below content

RewriteEngine On
RewriteBase /

RewriteRule ^$ public/index.php [L]

RewriteRule ^((?!public/).*)$ public/ [L,NC]
1 like
Jle's avatar

@dedanirungu I ran the command you suggested but I had problems. How to go back to how it was before?

1 like

Please or to participate in this conversation.