Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

/var/www/ folder structure for PHP project

Tags:

php

mamp

I'm using MAMP just for my development environment and haven't really understood the folder structure of /var/www/project-name/ on a web server. I'm wondering whether you're meant to point the web server to the htdocs folder or it's supposed to point to the root of the project folder?

The reason I ask is because libraries are meant to exist outside of the htdocs folder for security purposes, but how are they pointed to from the web application itself? Surely the web application can't access folders outside of the htdocs folder if the web server is pointing to the htdocs folder for the web application?

like image 907
Kezzer Avatar asked Apr 10 '09 17:04

Kezzer


3 Answers

A simple solution is to have a folder structure like so:

/var/www/project-name/
    + webroot/
    + libraries/

Point your apache2 DocumentRoot to the webroot directory. Keep all the libraries that you don't want accessible from the web in the libraries directory. In your php code, use the include directive to access the libraries code.

The trick is to understand that php can include any file on your system it has read access to. A person browsing your website can only access files inside your webroot directory.

like image 151
lzhang Avatar answered Oct 05 '22 10:10

lzhang


If you have multiple vhosts on the same server, it's pretty common to have each site in a directory under /var/www, and each of these have a htdocs folder, which is mounted as the web root. You can then have logs and application-specific libraries in a folder above the web root. Eg.:

/var/www/lolcats.com
/var/www/lolcats.com/htdocs
/var/www/lolcats.com/htdocs/index.php
/var/www/lolcats.com/lib
/var/www/lolcats.com/log
like image 32
troelskn Avatar answered Oct 05 '22 10:10

troelskn


PHP can access any file in the filesystem for which the apache server user has the correct permissions. On a linux box running apache without virtual hosts, /var/www is a common place to use for your htdocs directory.

like image 20
Codingscape Avatar answered Oct 05 '22 11:10

Codingscape