PHP 项目的 /var/www/ 文件夹结构
我仅将 MAMP 用于我的开发环境,并没有真正理解 Web 服务器上 /var/www/project-name/ 的文件夹结构。 我想知道您是否打算将 Web 服务器指向 htdocs 文件夹,或者应该指向项目文件夹的根目录?
我问的原因是因为出于安全目的,库应该存在于 htdocs 文件夹之外,但它们是如何从 Web 应用程序本身指向的? 如果 Web 服务器指向 Web 应用程序的 htdocs 文件夹,那么 Web 应用程序肯定无法访问 htdocs 文件夹之外的文件夹吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一个简单的解决方案是采用如下所示的文件夹结构:
将 apache2
DocumentRoot
指向webroot
目录。 将您不希望从网络访问的所有库保留在libraries
目录中。 在您的 php 代码中,使用 include 指令来访问库代码。诀窍是要了解 php 可以包含系统上它具有读取权限的任何文件。 浏览您网站的人只能访问您的 webroot 目录中的文件。
A simple solution is to have a folder structure like so:
Point your apache2
DocumentRoot
to thewebroot
directory. Keep all the libraries that you don't want accessible from the web in thelibraries
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.
如果同一服务器上有多个虚拟主机,则通常将每个站点都放在
/var/www
下的目录中,并且每个站点都有一个htdocs
文件夹,其中被安装为 Web 根目录。 然后,您可以将日志和特定于应用程序的库放在 Web 根目录上方的文件夹中。 例如。: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 ahtdocs
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.:PHP 可以访问文件系统中 apache 服务器用户具有正确权限的任何文件。 在运行没有虚拟主机的 apache 的 Linux 机器上,/var/www 是 htdocs 目录的常用位置。
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
,一般用户无法访问。
You place the libraries in PHP's
include_path
which is inaccessible to the general user.以与域工作相同的方式将本地网站映射到目录中是一个好主意。
通常,您在一个网络托管帐户上有多个网站,因此设置虚拟主机来镜像设置。
如果您的共享主机是:
在本地 PC 上创建 3 个虚拟主机,但保持相同的文件结构。
此外,根据服务器文件结构,使用配置文件中的条件来设置不同的站点,以确保相同的配置文件适用于两种设置。 这意味着您可以保留一步构建过程。
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:
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.