laraveldeploymentlaravel-7

Deploy Laravel app on shared hosting properly?


What am I doing

  1. Open FTP -> public_html -> laravel folder.
  2. Copy all files from local directory to laravel folder
  3. Open URL of the site and get this on the screen

https://i.sstatic.net/IbsvQ.png

Problem is simple, it opens directory, and not lead to index.php in public/ directory.

I tried everything I found in Google, like:

Move .htaccess file from /public to laravel directory and change it, like this:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]
    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php

It works, but not completely, it load site, but it doesn't load .css and .js (static files) from public directory, which is normally, after I moved out .htaccess files, so I should change asset() everywhere in the project and add "public/", which doesn't look OK for me.

Instead of moving .htaccess I tried to edit index.php file, like most of the tutorials show:

require __DIR__.'/../vendor/autoload.php';

to

require __DIR__.'/../laravel/vendor/autoload.php';

and

$app = require_once __DIR__.'/../bootstrap/app.php';

to

$app = require_once __DIR__.'/../laravel/bootstrap/app.php';

It doesn't work.

Is there a way to handle this, without edit asset() everywhere in my project ?


Solution

  • Option 1

    The best way to deploy, if you do not have access to the ssh but the ftp is:

    1. Copy and paste the project on the parent folder of public_html
    2. Wait for the upload, I recommend you to separate in an appropiate mode your dependencies in order to no install devDependencies in your prod site (this also applies for your JS assets).
    3. once the entire laravel site is uplodaded, then you should have to delete the public_html folder and then from public make a symlink this will create a reference from your public folder and apache will try to access to the reference associated with the new public_html symlink.

    Option 2

    If you have access to your server through SSH, then I strongly recommend you to check if you have git available on your server, then you would be able to pull your master branch (this should be prepared for production). And also create the symlink using command line. Also if you have SSH you will be able to prepare your project as it should with the commands you need to make it work.