大网数据广告活动
服务器运维问答

How to prevent PHP on a virtualhost/website from writing to another virtualhost's/website's directory on the same Apache server?

· 52CE 我爱测

I want to prevent virtualhost #1 from writing or reading to virtualhost #2 without losing writing access to the www-data at all (because the applications need to write to the directory). My current site configuration looks like t…

问题原文

原帖链接 · Ebbez · CC BY-SA 4.0

I want to prevent virtualhost #1 from writing or reading to virtualhost #2 without losing writing access to the www-data at all (because the applications need to write to the directory).

My current site configuration looks like this:

<VirtualHost *:80>
  ServerName example.com
  ...
  DocumentRoot /var/www/example.com/public/
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName example.org
  ...
  DocumentRoot /var/www/example.org/public/
  ...
</VirtualHost>

Both have the following kind of permissions:

drwxrws--- webmaster-com www-data example.com
drwxrws--- webmaster-org www-data example.org

My problem is that PHP code/scripts executed on example.org can read configurations of the example.com website, with often MySQL passwords in it.

已采纳解答

原帖链接 · Ondřej Xicht Světlík · CC BY-SA 4.0

Use PHP configuration directive open_basedir.

open_basedir string

Limit the files that can be accessed by PHP to the specified directory-tree, including the file itself. This directive is NOT affected by whether Safe Mode is turned On or Off.

To configure it per virtualhost, use php_admin_value configuration directive like this:

<VirtualHost *:80>
  ServerName example.com
  ...
  DocumentRoot /var/www/example.com/public/
  php_admin_value "open_basedir" "/var/www/example.com/"
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName example.org
  ...
  DocumentRoot /var/www/example.org/public/
  php_admin_value "open_basedir" "/var/www/example.org/"
  ...
</VirtualHost>

Be sure to use php_admin_value and not plain php_value because:

php_admin_value name value

Sets the value of the specified directive. This can not be used in .htaccess files. Any directive type set with php_admin_value can not be overridden by .htaccess or ini_set().

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-12-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。