Apache-2.2

僅允許在指定的虛擬主機上訪問 PhpMyadmin

  • April 14, 2016

我正在開發一個多虛擬主機環境。我已經為 Mysql Remote Control 安裝了 PhpMyadmin。

環境配置如下:

one.domain.com
two.domain.com
onlyphpmyadmin.domain.com

現在,如果我訪問三個域之一

http://one.domain.com/phpmyadmin/
http://two.domein.com/phpmyadmin/
http://onlyphpmyadmin.domain.com/phpmyadmin/

結果是一樣的,允許訪問 Phpmyadmin。

目標是獲得像下面這樣的情況

http://one.domain.com/phpmyadmin/ --> access denied
http://two.domein.com/phpmyadmin/ --> access denied 
http://onlyphpmyadmin.domain.com/phpmyadmin/ -->access allowed

沒有類似的hack

<?php 
if($_SERVER['HTTP_HOST'] != 'onlyphpmyadmin.domain.com')
die('access denied');

...
?>

在一些 Phpmyadmin 文件上。


這是我的 Phpmyadmin 配置文件

Alias /phpmyadmin /usr/share/phpmyadmin

<Directory /usr/share/phpmyadmin>
   Options FollowSymLinks
   DirectoryIndex index.php

   <IfModule mod_php5.c>
       AddType application/x-httpd-php .php

       php_flag magic_quotes_gpc Off
       php_flag track_vars On
       php_flag register_globals Off
       php_admin_flag allow_url_fopen Off
       php_value include_path .
       php_admin_value upload_tmp_dir /var/lib/phpmyadmin/tmp
       php_admin_value open_basedir /usr/share/phpmyadmin/:/etc/phpmyadmin/:/var/lib/phpmyadmin/
   </IfModule>

</Directory>

# Authorize for setup
<Directory /usr/share/phpmyadmin/setup>
   <IfModule mod_authn_file.c>
   AuthType Basic
   AuthName "phpMyAdmin Setup"
   AuthUserFile /etc/phpmyadmin/htpasswd.setup
   </IfModule>
   Require valid-user
</Directory>

# Disallow web access to directories that don't need it
<Directory /usr/share/phpmyadmin/libraries>
   Order Deny,Allow
   Deny from All
</Directory>
<Directory /usr/share/phpmyadmin/setup/lib>
   Order Deny,Allow
   Deny from All
</Directory>

刪除別名聲明

Alias /phpmyadmin /usr/share/phpmyadmin

從伺服器上下文並將其放入相關的虛擬主機上下文中

<VirtualHost *:80>
   ServerName onlyphpmyadmin.domain.com
   .
   .
   .
   Alias /phpmyadmin /usr/share/phpmyadmin
</VirtualHost>

將整個 phpmyadmin 配置包含到相關的虛擬主機中可能更容易和更可取

<VirtualHost *:80>
   ServerName onlyphpmyadmin.domain.com
   .
   .
   .
   include /path/to/phpmyadmin.conf
</VirtualHost>

然後從伺服器上下文中刪除該包含並重新啟動 apache 以使更改生效。

引用自:https://serverfault.com/questions/487335