Get dynamic server document root in CakePHP 4.x

Hi there!

In paths.php, the www_ROOT is assigned in a quite fixed way:

define('WWW_ROOT', ROOT . DS . 'webroot' . DS);

Luckily enough, there’s a code (initially in comment) that makes the WWW_ROOT more dynamical if you want:

define('WWW_ROOT', rtrim($_SERVER['DOCUMENT_ROOT'], DS) . DS);

I need the dynamical approach because my hosting uses ‘www’ as public folder instead of CakePHP’s ‘webroot’ folder.

But unfortunately, when I use the dynamical rule from above instead of the fixed approach, I can’t start my local server anymore. I always got this error:

[Thu Mar 23 23:05:40 2023] PHP Warning: Module ‘openssl’ already loaded in Unknown on line 0
Directory does not exist.

Warning: Module ‘openssl’ already loaded in Unknown on line 0

I have my XAMPP running Apache, but the project itself is running with the CakePHP 4 Server Tool.
As a test, I checked XAMPP’s php.ini file. I don’t see a duplicate openssl rule I must say…

Still, I wonder if this is really the problem as I didn’t have the error before I’ve changed the WWW_ROOT.

Does anybody have a clue what’s going on here?

As always: thank you for reading and helping!

Kind regards,

So, your WWW_ROOT constant will now have something like /home/sam/www? What is in that folder? Where are the src, config, vendor, etc. folders from your Cake project?

Hi Zuluru, thanks for your reply!

My local WWW_ROOT is:


This is a screenshot from my project folders and files:

My server WWW_ROOT is:


The hosting initially had these folders and files:


As some additional info, when I use the following line, it works perfectly on the production environment:

define('WWW_ROOT', rtrim($_SERVER['DOCUMENT_ROOT'], DS) . DS);

But the code from above generates the ‘openssl already loaded’ error in my local environment.

Here’s what happens in my CLI:

I don’t know if this was the information you were looking for?

Thanks for helping me find some answers! :metal:

Your first (dev system) screenshot shows the webroot folder, at the same “level” as bin, config, logs, plugins, resources, src, templates, tests, tmp, vendor. Where are all those folders in your production environment?

Hi Zuluru!

Yes, those folders and files were also in the production environment. Here’s a screenshot of all folders and files on the production server:

I know this is bad behavior. Only the files in ‘webroot’ should be in the ‘www’ folder on the production environment. ‘config’, ‘src’, ‘templates’, ‘vendor’, … should be on the same level as the ‘www’ file.

But first, I try to have the dynamic WWW_ROOT so I can use the same setup in both environments :sweat_smile:

I believe that WWW_ROOT in this case should still be the webroot folder, but I’d really focus on getting the production site set up how it should be (i.e. all those other folders out of www), not on getting dev to match something that is going to need to change again anyway.

Hi @Zuluru! I don’t know how it works, but I could keep my original code in the development AND the production environment. So there was, surprisingly enough, no need to change the WWW_ROOT. Meanwhile, I’ve put all code outside the webroot folder :wink:

So my problem is solved! Thx for helping!