Its possible if you set everything right, do you already have experience with deploying laravel to production?
how to run laravel app in wordpress subfolder?
I've been asked to develop a laravel app and to run it inside an existing wordpress project. (possibly staying on the same domain) How could I achieve that? I am using Laravel 10, in a Linux based system, using Apache server. Thanks in advance
Sadly no, as I'm a junior and this is only my second project
@lilsOfi No problem, the deployment steps for your Laravel application within a subfolder of a WordPress project are not significantly different from a regular Laravel deployment. The only difference is that the Laravel project needs to be accessible from a subfolder instead of the main public root.
Here's a small summary of the steps you can follow:
Create a Folder: Outside the public root, create a folder for the Laravel project and place the entire Laravel application within it.
Move Files: Transfer the files from your Laravel app's public folder into the target folder within your WordPress project.
Update Paths: Modify the paths in the index.php file to direct to the Laravel app outside the public root.
Symlink Creation: If necessary, create a symlink between the folder in your WordPress project and the Laravel public folder outside the public root.
These steps should give you an idea on how to do this. On the internet there are multiple guides available on how to deploy laravel to production, checking these out can also give you a better idea on how it works.
If you get stuck or have more questions don't hesitate to reach out.
@gych Hello, sorry for bothering you again with this issue. I can't seem to set the paths right within my folders (never done it before). So what I understood is: I need to point my wordpress index.php file to the index.php file in my public folder, correct? The public folder should be placed in the root folder of my wordpress folder, right?
@lilsOfi So this is what you need to do:
-
Laravel folder needs to be uploaded outside of the public root, depending on your host for example outside/one folder up from the public _html folder
-
Then you add the contents of your public folder to the wordpress subfolder
-
After you've done that, you edit the index.php that was moved from the public folder to the subfolder. Change the paths in the index.php file so it points correctly to the laravel application.
-
You can also make a symlink between the subfolder and the public folder
@gych Okay, thanks. I get confused by the folder's name as i don't have a public_html folder. this is what my folders look like -wordpress -wp_admin -wp_content -wp_includes -index.php -.htaccess -ecc So I get easely confused due to this
@lilsOfi Yes it depends on your hosting environment, but the laravel folder has to be outside of the public root, this is very important for the security of your application.
In which folder is your wordpress folder placed?
@gych right now is in the c:\wamp64\www\wordpress
@lilsOfi oke so in this case seems like your public root is www, the lavavel project folder needs to be outside the www folder. So you can add the folder of your laravel project in wamp64
@gych okay so i made a few chages and now i think i placed the folders correctly what i have is:
-plublic_html
-wordpress files
-laravel public folder
-mylaravelapp
i think i have a problem with my htaccess config cause now when i try to access www.root it shows me my wordpress but if i call to my laravelapp i get a 500 server error
@lilsOfi Did you change the paths correctly in the index.php file?
@gych i think so.. in the index.php of wordpress i pointed to my public/index.php but do i have to change the one inside my public/index to point to something?
ps: the error i'm getting right now is: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
@lilsOfi What did you change exactly in the index.php of your wordpress site?
You should change that paths in the index from the public folder public/index.php The paths in this index should point to mylaravelapp outside of the public root
@gych in the wordpress index.php i just added this line
require __DIR__ . '/public/index.php';
to point to the index of my laravel app
in the index of my public folder i pointed to my folder ouside the public like so
require __DIR__.'/../../../welfare-hub-services/vendor/autoload.php';
@lilsOfi Can you share the full code of your 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__.'/../../welfare-hub-services/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__.'/../../welfare-hub-services/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__.'/../../welfare-hub-services/bootstrap/app.php';
$kernel = $app->make(Kernel::class);
$response = $kernel->handle(
$request = Request::capture()
)->send();
$kernel->terminate($request, $response);
@lilsOfi Try to replace the paths with this
if (file_exists($maintenance = __DIR__.'../../../welfare-hub-services/storage/framework/maintenance.php')) {
require $maintenance;
}
require __DIR__.'../../../welfare-hub-services/vendor/autoload.php';
$app = require_once __DIR__.'../../../welfare-hub-services/bootstrap/app.php';
Did you also ran npm install and composer require after you moved your laravel app to the host?
@gych i had the paths like this like 10 minutes ago... didn't work. but my composer wasn't updated. I'll try it now
UPDATE I tried it but it doesn't work
@lilsOfi So you did composer require and npm install for the laravel app in the welfare-hub-services folder right?
Which error do you get when trying to visit the app, still the same?
@gych it gives me a 500 server error for too many internal redirects but i tried to navigate directly to my public folder (in case i was wrongingly navigating to my welfare-hub-services) and now obviously it wants me to run npm run dev and i did, but i still get Vite manifest not found at: C:\wamp64\www\welfare-hub-services\public\build/manifest.json
oh, and yes, i did that for my welfare-hub-services folder
@lilsOfi Which hosting environment do you use?
@gych as for right now i'm just trying to make it work locally using wamp, since it doesn't with the new folder setup
I will deploy this in a shared server that allows me to navigate a folder up from public_html
@lilsOfi Ok I see
From this line I see that you're laravel app is inside of www
C:\wamp64\www\welfare-hub-services\public\build/manifest.json
If you want to replicate a situation like in a production environment. The laravel app folder should be outside of www, this means you'll add it inside wamp64
C:\wamp64\welfare-hub-services
Then you'll add the public folder or contents from welfare-hub-services/public inside www
C:\wamp64\www\yourpublicfolder
I've never had to test my Laravel applications this way locally since I can create staging environments on my server. But I think it should be possible.
@gych okay so now it partially works, but i need to understand how to configure vite properly
@lilsOfi What do you need to change in vite config?
@gych dont know yet, but when i try to open http://wfhub.local/ it gives me Vite manifest not found at: C:\wamp64\www\welfare-hub-services\public\build/manifest.json even though i ran npm run dev on my welfare-hub-services folder
@lilsOfi You should use npm run build
npm run dev is for when you're working on the project and want to see live changes.
@gych I did that.. so now i have the manifest.json and the assets folder int the public directory but i still get the same message Vite manifest not found at: C:\wamp64\www\welfare-hub-services\public\build/manifest.json and laravel suggests me to run npm run dev
Also, I thought that run build was for deploying(?)
@lilsOfi Yes it is for deploying and all the instructions I gave you are for when you deploy your app to production. To me it seemed like you're trying to test the deployment structure locally.
But if you want to use this as development environment you should not move the files from the public folder to another folder. Tha's overcomplicating things and something that's done when deploying the project not in the development process.
First you should finish the Laravel app, when your app is done you can test the deployment and deploy to the server.
Why are you trying to set it up this way as development environment?
@gych because i needed to know if it would work on deploy, with all the changing of folders paths eccetera before i could finish my app... thats what my senior told me to do today. He said too see if I could run the app within wordpress before continuig working on it. And since today I found out we're deploying it on a shared server I had to make sure it was possibile, otherwise I would have lost a month of work
@lilsOfi Yes ofcourse that's what I also thought, that you were testing but you confused me about the npm run dev part because that's not used in production or while deploying. Only for local development.
Is your issue solved now ?
@lilsOfi Yes it should work when deploying but it should also be possible to get it working locally. If you check the public folder is there still a file with the name 'hot' inside ?
yes but i just noticed that the path we put in the index.php pointed to another project and not the right one, so I changed it again in this
<?php
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
define('LARAVEL_START', microtime(true));
if (file_exists($maintenance = __DIR__.'/../../welfare-hub-services/storage/framework/maintenance.php')) {
require $maintenance;
}
require __DIR__.'/../../welfare-hub-services/vendor/autoload.php';
$app = require_once __DIR__.'/../../welfare-hub-services/bootstrap/app.php';
$kernel = $app->make(Kernel::class);
$response = $kernel->handle(
$request = Request::capture()
)->send();
$kernel->terminate($request, $response);
and now there's something wrong with the path i think gives me this
Warning: require(C:\wamp64\www\wfhub\public_html\public/../../../welfare-hub-services/vendor/autoload.php): Failed to open stream: No such file or directory in C:\wamp64\www\wfhub\public_html\public\index.php on line 34
Fatal error: Uncaught Error: Failed opening required 'C:\wamp64\www\wfhub\public_html\public/../../../welfare-hub-services/vendor/autoload.php' (include_path='.;C:\php\pear') in C:\wamp64\www\wfhub\public_html\public\index.php:34 Stack trace: #0 C:\wamp64\www\wfhub\public_html\index.php(18): require() #1 {main} thrown in C:\wamp64\www\wfhub\public_html\public\index.php on line 34
i really am sorry of taking up so much of your time, and thank you for what you do
@lilsOfi No problem, You get this error because it can't find any of the files.
So the paths are not pointing to the correct file paths.
@gych yep, i imagined, changed the paths, it works still have to figure out why id display laravel and wordpress on the same page but thats a start
@lilsOfi Ok so the issue you're having now is that the laravel project and wordpress project are both displaying on the same page?
@gych yes, and also it displays only my welcome page but when i try to reach other section of the app it returns me the 500 server error
edit: other pages works if i change my uri from http://wfhub.local/ to http://wfhub.local/public/ but isn't it a safety issue?
@lilsOfi You'll have to remove this from you wordpress index.php
require __DIR__ . '/public/index.php';
@gych Amazing, thanks a lot, truly
@lilsOfi No problem :) Did this solve your issues?
@gych yess I can see everything, i am just worried about safety cause to be able to see my whole app i have to change my uri from http://wfhub.local/ to http://wfhub.local/public/ but isn't it a safety issue?
@lilsOfi No that's not an issue and its also because your app index.php is currently inside the public folder. So when you later move the laravel app to a wordpress subfolder it will be accessible from there like you stated in your original post.
This folder being accessible is not a safety issue. Its only important for the folder that hosts your laravel app and env file not be be accessible, which why that folder needs to be outside of the public root.
If this solved your issues don't forget to close the post again, if you have more questions don't hesitate to reach out.
@gych fantastic, thanks a lot again!
Please or to participate in this conversation.