简体   繁体   中英

/var/www/ folder structure for PHP project

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?

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.

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

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.

您将库放在PHP的include_path ,这是普通用户无法访问的。

It is a googd idea to map your local websites in directories in the same way as your domains work.

Often you have multiple websites on a single web hosting account, so setup virtual hosts to mirror the setup.

If your shared hosting is:

/var/www/root
 /var/www/root/website1
 /var/www/root/website2
 /var/www/root/website3

Create 3 vitual hosts on your local PC but keep an identical file structure.

Also, use conditions in your config files to setup the site deifferently depending on the server file structure, to ensure the same config file works on both setups. This means you keep your one-step build process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM