r/PHPhelp • u/BigBootyBear • Jul 31 '25
Why are PHP apps split across various /** direcotries?
I've got handed a legacy PHP app and most of my work is JS web dev. I don't get why our servers have stuff just all over the place. Theres parts of the app under /usr/local/* or under /var/www/*, in addition to various other things nested under ~.
Im used to building something under /dist, and serving it via nginx. Or defining routes in a flask router.py and running it on gunicorn. Why do I need index.html to be in /var/www when I can serve index.html from route.serve(index.html) that points to something under /src?
Is this just legacy conventions, or is there a legitimate reason for exposing directories like /var/www insteading of keeping everything nice and tidy under /src?
11
u/colshrapnel Jul 31 '25 edited Jul 31 '25
Yes, it's a very old legacy convention, even I had no idea it existed until recently, when stumbled upon a project of same legacy.
Early PHP libraries, such as PEAR::DB, were distributed as regular packages, so you had to install it with
sudo apt-get install php-pear. And naturally, it was installed where other packages go. While your actual code that could have used this library, would go into /var/www/* - just as all good sites do.Regarding any code from the user's home directory I honestly have no idea. Due PHP's low learning curve, people tend to create web-projects with zero prior knowledge in the area, and that's probably it.
Anyway, nowadays it's nowhere like this. All the code goes into a subfolder in /var/www (or /srv/ or /opt or whatever to your liking), and it contains a file composer.json. Then we have to install Composer (either as a local php script or an OS package) and run
composer install. This command will download all dependencies and put them in thevendordirectory in the same folder. So all project files are strictly contained in its folder only.Given PHP is not a web-server, it needs a web-server to help its router. Usually it's
index.phpfile in /var/www/project/public folder (which serves as the project's document root), to which web-server redirects all requests to non-existent resources. Then index.php executes some code that stays in /var/www/project/src (and couple other folders under /var/www/project). Something like this5
u/martinbean Jul 31 '25
That’s just what the developer chose. It’s not an established convention in PHP, so it’s unfair to see “that’s PHP”.
Usually, you have an index.php file in a web-accessible directory such as /var/www/html that acts as your front controller/entry point. This index.php would then invoke scripts from a non-publicly accessible directory. It seems this developer has for some reason assumed the app was to be developed on the same machine it’s been served on, and storing such code under the /usr/local directory. That’s the first time in over 15 years I’ve seen an app organised like that. It’s more common to have a structure something like:
The reason you keep your actual scripts out of your public directory is to avoid them being accessed by users through vulnerabilities such as directory traversal or web server misconfiguration where PHP scripts are printed to the browser rather than parsed by the web server (it can happen, and famously did happen to Facebook back in like, 2008).
4
u/hackiavelli Jul 31 '25
Back in the day URLs were literally mapping to a file. So if you had a website
http://example.com/diablo/faq/cowlevel.htmlit was grabbing a static HTML file at/var/www/diablo/faq/cowlevel.html.When scripting languages came on the scene many web developers used that structure because that's what they were used to. That was reinforced by early PHP being embedded in HTML files like JS is today (it's why we have the
<?phptag).There have been many steps between that old-school filesystem routing and modern routers. Some legacy apps haven't kept up whether from devs not updating their skills, not having the org time or money to make the updates, or the overwhelming senior dev urge not to eff with a working website.
2
u/Objective_Sock_6661 Jul 31 '25
why do I need route.serve(index.html) when I can just serve index.html?
2
u/obstreperous_troll Jul 31 '25
The entry point to your app has to be accessible to the web server, because that's just how PHP rolls. Modern PHP apps have a single entry point, usually in a
public/index.htmlunder the app's root, and the webserver can only access files in public/. Legacy apps have an entry point for every page, so they typically expose the entire app under the web root so that any .php file can be loaded by the webserver. Wordpress still does it like this.You'd do yourself a big favor as a maintainer if you converted the app to use a single entry point, where the first thing that entry does is call
require_once(__DIR__.'/../vendor/autoload.php');1
u/przemo_li Jul 31 '25
You need index.html in /var/www because that's how web server is configured.
You could change config, but do take care to preserve old config for old frontend for as long as you need it.
/var/www is modeled after how apache would do its thing. Back in the day you would need Strict separation of code and web content, otherwise someone could use misconfiguration of an server to get app source code via browser!
Now the question is if you have someone smiled enought to either migrate config or at least set it up so that you can do your stuff your own way.
PS above is true for local development. Prods will serve stuff from /var/www anyway, it's convention, content is /dist is copied into relevant places
1
u/PickerPilgrim Jul 31 '25
Used to be advised to put files that didn’t directly serve content outside of the webroot for “security”. If you screwed up your Apache config you didn’t want to expose source.
1
u/phpMartian Aug 03 '25
I’m also working on a legacy project.
That’s some seriously old stuff. That’s not a PHP thing. Some person in the past decided to do it that way.