前不久宝塔面板某个版本有安全漏洞导致phpmyadmin可以被未授权访问到,由于貌似与本站关系无关就没有在本站水相关内容了。
但是我这次想说的是一直以来我使用VestaCP面板搭建环境时都未对该面板环境的phpmyadmin进行过修改或者鉴权操作,除了某次的搬家后未能成功安装而无法访问到默认路径,其他情况下算是直接对外可以直接访问的。虽然需要输入数据库的密码才能登陆进去但是却不得不防止出现配置文件意外泄漏了数据库密码或者phpmyadmin有安全漏洞之类的。
起码在几天前又折腾过一次修改默认路径的方法,但是错折腾的是Nginx的配置导致只有重命名或者删除phpmyadmin的程序目录来假装没有,就在今天突然在Apache目录中找到了配置文件并且成功进行了给phpMyAdmin的安全加道菜。

本次以Centos7系统搭建的VestaCP面板Nginx+Apache模式为例子,其他情况对应修改。(Debian系统的修改追加在下面)
一、修改默认目录同时给目录加上密码鉴权
修改/etc/httpd/conf.d/phpMyAdmin.conf文件,
1、如果想修改默认的http(s)://url/phpmyadmin为其他目录,比如:“https://www.cyzwb.com/php-myadmin”可以修改为
Alias /php-MyAdmin /usr/share/phpMyAdmin Alias /php-myadmin /usr/share/phpMyAdmin
重启Apache
service httpd start
2、给目录加上密码鉴权
在<Directory /usr/share/phpMyAdmin/>与</Directory>闭合内加上或者新增一个<Directory /usr/share/php-MyAdmin/></Directory>Apache的鉴权
<Directory /usr/share/php-MyAdmin/> #这个相当于打开认证的开关 AllowOverride AuthConfig #自定义认证的名字,作用不大 AuthName "ChiuYut Login" #认证的类型,一般为Basic AuthType Basic #指定密码文件所在位置 AuthUserFile /etc/httpd/conf.d/.htpasswd #指定需要认证的用户为全部可用用户 require valid-user </Directory>
创建密码文件与用户名ChiuYut
htpasswd -c -m /etc/httpd/conf.d/.htpasswd ChiuYut
重新加载配置-t,graceful
apachectl -t apachectl graceful
此时再打开https://www.cyzwb.com/php-myadmin就需要输入用户名和密码才能进入到phpMyAdmin的界面了。
二、绑定一个单独的域名
对于每个绑定在VestaCP上面的域名都可以访问到phpMyAdmin页面表示强迫症犯了,虽然可以改成自定义的目录但是也很是不爽啊。
于是乎就决定绑定一个单独的域名只单单提供phpmyadmin的服务就好了。
我的做法是在面板上面新增一个站点然后在修改起的配置文件(/home/admin/conf/web/phpmyadmin.cyzwb.com.httpd.conf):
修改默认的DocumentRoot为
DocumentRoot /usr/share/phpMyAdmin/
把
<Directory /home/admin/web/speedtest.chiuyut.com/public_html>
AllowOverride All
Options +Includes -Indexes +ExecCGI
php_admin_value open_basedir /home/admin/web/speedtest.chiuyut.com/public_html:/home/admin/tmp
php_admin_value upload_tmp_dir /home/admin/tmp
php_admin_value session.save_path /home/admin/tmp
</Directory>
<Directory /home/admin/web/speedtest.chiuyut.com/stats>
AllowOverride All
</Directory>
替换成
<Directory /usr/share/php-MyAdmin/>
#这个相当于打开认证的开关
AllowOverride AuthConfig
#自定义认证的名字,作用不大
AuthName "szl.com user auth"
#认证的类型,一般为Basic
AuthType Basic
#指定密码文件所在位置
AuthUserFile /etc/httpd/conf.d/.htpasswd
#指定需要认证的用户为全部可用用户
require valid-user
</Directory>
<Directory /usr/share/php-MyAdmin/>
Order Deny,Allow
Deny from All
Allow from All
</Directory>
<Directory /usr/share/php-MyAdmin/scripts/>
Order Deny,Allow
Deny from All
Allow from All
</Directory>
# This directory does not require access over HTTP - taken from the original
# phpMyAdmin upstream tarball
#
<Directory /usr/share/ph-pMyAdmin/libraries/>
Order Deny,Allow
Deny from All
Allow from None
</Directory>
# This configuration prevents mod_security at phpMyAdmin directories from
# filtering SQL etc. This may break your mod_security implementation.
#
#<IfModule mod_security.c>
# <Directory /usr/share/phpMyAdmin/>
# SecRuleInheritance Off
# </Directory>
#</IfModule>
<Directory /usr/share/php-MyAdmin/setup>
Order Deny,Allow
Deny from All
Allow from None
</Directory>
重启Apache,然后访问绑定的域名输入用户名和密码鉴权就可以了。
三、心得
本次是自己瞎折腾,不知道会不会导致其他风险,本质上来讲由于是自己一个人使用所以那个专属域名可以不进行解析需要使用时再指定IP访问就可以了。甚至还可以在面板上面暂停这个站点需要是才启用然后发现相关配置文件修改的部分被删除了。所以暂停这步行不通还是不解析加鉴权才能访问吧。
四、对于Debian系统的修改
在/etc/apache2/conf.d目录中发现一个名为phpmyadmin.conf的软链接到/etc/phpmyadmin/apache.conf,重命名/etc/phpmyadmin/apache.conf发现Apache无法启动,修改/etc/apache2/conf.d/phpmyadmin.conf重启后还是可以访问到phpmyadmin,于是只好删除/etc/apache2/conf.d/phpmyadmin.conf这个软链接了。然后重启Apache
service apache2 restart
参考:
LAMP构建-Apache用户认证(输入密码才能访问)
https://blog.51cto.com/shuzonglu/2082186
关于Linux面板7.4.2及Windows面板6.8紧急安全更新
https://www.bt.cn/bbs/thread-54666-1-1.html
明明是4号折腾的却因为跨时间到5号才写完只能署下5号的戳。
ChiuYut
2020年9月5日