Laravel Deployment on Shared Hosting - php

I'm trying to deploy my laravel project on shared hosting Cpanel, I divide my laravel project into 2 folders (i do this so the user can't check out my .env file):
laravel (all laravel files except public)
public (I moved all the contents of the file to public_html)
I put both of them to public_html, and I wonder what I have to do with htaccess file to point it to the public folder?
My htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
# 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 ^ public/ [L]
</IfModule>
Btw, someone told me not to change the structure of the PHP file

How to deploy a Laravel project?
Copy all the contents of the /your_laravel_project/public/ in public_html
Copy the other files and folders from /your_laravel_project to a directory which is before public_html, let's call it /core
in public_html, edit the routes in index.php file.
I'm not sure about the older versions but in Laravel 8.x and 9.x you only need to edit 3 lines in index.php file:
if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {... to if (file_exists($maintenance = __DIR__.'/../core/storage/framework/maintenance.php')) {
require __DIR__.'/../vendor/autoload.php'; to require __DIR__.'/../core/vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php'; must change to $app = require_once __DIR__.'/../core/bootstrap/app.php';

You need to Edit the index.php and .htaccess file in your public folder.
Why you need this changes because of now your files are outside of Laravel Project folder and for mapping the project you need to give a new path to this files.
You only need to change code in your public/index.php
<?php
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/
require __DIR__.'/../laravel/vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
And in your .htaccess file you need to add this lines of code
<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 (.+) /public/$1 [L]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /public/index.php [L]
</IfModule>

Related

How to integrate laravel8 project inside wordpress project cpanel?

I've wordpress project in (html_public) folder, and my domain run project fine, I want integrate laravel8 project inside the wordpress, so that both run in the same domain like That:
WordPress URLs:
https://example.com/wordpressHomepage
https://example.com/AnotherwordpressPage
Laravel URLs:
https://example.com/public/LaravelPage
https://example.com/public/LaravelPage/Id
Wordpress && LaravelApp path in cpanel:
public_html/
wp-admin
wp-content/
wp-includes/
... other wordpress files ...
laravelApp/ <-- laravel application folder
|-> app/
|-> bootstrap/
|-> config/
|-> public/
|-> ... other laravel files and directories ...
Wordpress .haccess:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Laravel .haccess in /public:
<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]
</IfModule>
wordpress index.php:
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* #package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* #var bool
*/
define( 'WP_USE_THEMES', true );
/** Loads the WordPress Environment and Template */
require __DIR__ . '/wp-blog-header.php';
Laravel index.php in App/public/index.php:
<?php
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Check If The Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is in maintenance / demo mode via the "down" command
| we will load this file so that any pre-rendered content can be shown
| instead of starting the framework, which could cause an exception.
|
*/
if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
require $maintenance;
}
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/
require __DIR__.'/../vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request using
| the application's HTTP kernel. Then, we will send the response back
| to this client's browser, allowing them to enjoy our application.
|
*/
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Kernel::class);
$response = $kernel->handle(
$request = Request::capture()
)->send();
$kernel->terminate($request, $response);
please any help?
Create one folder in the root directory and dump your laravel project folder in that and create .htaccess inside laravel project folder then point that .htaccess to the laravel public directory index file. your project will run smoothly.
But my suggestion is to go with VPS hosting which will have you root access.
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
Assuming your Laravel .htaccess file is inside the /public subdirectory then simply remove (or comment out) the first two rules.
For example:
#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
This does assume that server.php is also located inside the /public subdirectory.

Laravel make routing to subfolder on deploy

I have a Laravel Apps and a shared hosting. I wanted to deploy my Laravel apps to cpanel, and got an error
404 resources not found when deployed. I have put the laravel public folder into the root folder, and the laravel file into subfolder /laravelApp.
So my hosting structured like this :
bdd.services
|--> public_html
|----> myLaravelApp
|------> css
|------> js
|------> laravelApp
|--------> app
|--------> /* and all the laravel apps inside here */
|------> .htaccess
|------> index.php
|
|
|----> someonesProject
When I tried to test the app, I see the request was requesting to
https://mydoamin.com/login/auth where it should be https://mydoamin.com/myLaravelApp/login/auth. And it shows an error 404 resources not found.
So how to make the routing always go to https://mydoamin.com/myLaravelApp/? or maybe there's another solution?
here's my .htaccess
<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_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I've also set the APP_URL at the .env.
APP_URL=https://mydoamin.com/myLaravelApp/
In this case you should ensure that you have
APP_URL="http://example.com/folder"
In your .env file
Then instead of doing an href like this
href="/login"
Give your route a name
Route::get('login', ...)->name('login');
and do your href like this
href="{{ route('login') }}"
This way it will always use the full url

Laravel application on sub-directory

The laravel application seems to have routes issue with the sub-directory on server. With the running application on local machine does not work on temporary domain in sub-directory on live server. Playing around .htaccess seems to have different results.
This question is almost identical to what I would be looking for but he hasn't got any solution while I have got some work around.
The .htaccess within the laravel public have been modified to something like:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase /~timely/email-client //<--base has been modified
# 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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
The application will redirect you to host gator 404 page.
Also I've added .htaccess to the main directory which will forward every request to public directory.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Upon removing the home/main .htaccess just works for / route: e.g
http://gator4057.temp.domains/~timely/email-client/public will work but all other routes are redirected to the same host gator 404 page.
Your comments and answers will be appreciated.
Thanks
Step 1. Move all email-client/public to email-client/. Now we're getting somewhere. Warning: Don't forget to copy the .htaccess file too
Step 2. Open email-client/index.php in your favorite editor and change
$app = require __DIR__.'/../bootstrap/app.php';
to
$app = require __DIR__.'/../email-client/bootstrap/app.php';
or may be
$app = require __DIR__.'/email-client/bootstrap/app.php';
and change:
require __DIR__.'/../bootstrap/autoload.php'
to
require __DIR__.'/../laravel/bootstrap/autoload.php'
or may be
require __DIR__.'/laravel/bootstrap/autoload.php'
Run command:
php artisan cache:clear
php artisan config:clear
php artisan optimize
OR
If you dont wana to do more changes try this:
Put this in index.php file
$app->bind('path.public', function() {
return __DIR__;
}
Hope work for you.

Deploying Laravel on a shared hosting sub-directory

I'm trying to deploy laravel 5.5 inside a WordPress sub-directory on a shared hosting. I tried too many suggestions from google but I still can't make it work. It always returns a 404 error. WordPress is installed inside /public_html
and I need to install Laravel on /public_html/my-laravel-app
Here's what my .htaccess files look:
/public_html/.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# protect wpconfig.php
<files wp-config.php>
order allow,deny
deny from all
</files>
# protect the htaccess file
<files .htaccess>
order allow,deny
deny from all
</files>
# disable the server signature
ServerSignature Off
/public_html/my-laravel-app/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my-laravel-app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /my-laravel-app/public/index.php [L]
</IfModule>
/public_html/my-laravel-app/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
/public_html/my-laravel-app/public/index.php (some part of index.php)
require __DIR__.'/../bootstrap/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../bootstrap/app.php';
I forgot to update this question, but I already solved this problem.
It's not actually about WordPress' .htaccess, I get rid of my custom .htaccess settings.
What I did was, create a new folder on public_html/laravel-app then inside my laravel-app,
this is where I cloned the repo, so it became, public_html/laravel-app/my-app
then, I copied the files(except the folders) inside my-app/public directory and moved it to laravel-app/.
$ cd /public_html/laravel-app
$ cp -a my-app/public/*.* ./
Then I created a symbolic link for the directories inside my-app/public by running
$ ln -sf my-app/public/*/ ./
And lastly, I modified the public_html/laravel-app/index.php
on line 22 and line 36,
require __DIR__.'/my-app/bootstrap/autoload.php';
$app = require_once __DIR__.'/my-app/bootstrap/app.php';
Then it's done.
Laravel doesn't work on subfolders out of the box. You will need to tweak some files in order to get it to work. Have a look at the following article on laravel-news.com
My suggestion would be to re-consider your approach and see if there's a different way to achieve your goal that doesn't require a subfolder.
Edit:
I'd like to include more steps from the article just in case it gets removed in the future but it's just too many steps.

.htaccess not working with laravel on production server-Shared hosting

I have this .htaccess in the laravel installation directory
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
And this .htaccess in the public directory of the laravel installation directory (this one came with the laravel)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
So, if I access the root/installation directory in the browser, like this http://something where something is the installation directory and has it's own virtual host on localhost, then it works and if I access it like this http://something/public then it works too. But this setup doesn't work on the production server. If I access my website(which is a subdomain) on production server like this http://sub.something.com then it doesn't work, this shows me Page not found error, but this works http://sub.something.com/public
What am I doing wrong?
I have namecheap.com shared hosting
On share hosting subdomain, it's good your folder structure look like this:
I assume it's a linux server (Apache)
--public_html
----subdomainFolderName (content of public folder)
-------.htaccess
-------index.php
-------etc
-------laravelFolder [Public folder will not be inside here]
----------app
----------bootstrap
----------config
----------etc
Edit subdomainFolderName/index.php
Change line 22
require __DIR__.'/../bootstrap/autoload.php';
TO
require __DIR__.'/laravelFolder/bootstrap/autoload.php';
Change line 36 $app = require_once __DIR__.'/../bootstrap/app.php';
TO
$app = require_once __DIR__.'/laravelFolder/bootstrap/app.php';
This way you will not have issues with .htaccess and your URL will not contain /public

Categories