配置Apache使未设置的子域名加载默认页面而非phpMyAdmin
配置Apache使未设置的子域名加载默认页面而非phpMyAdmin
嗨,我完全懂你的困扰——现在只要新添加的子域名指向你的Ubuntu服务器,就自动跳转到phpMyAdmin页面,这不仅暴露了你的管理工具,也不符合你想展示默认页面的需求。咱们可以通过调整Apache虚拟主机的优先级或者匹配规则来解决这个问题,具体步骤如下:
方法一:调整配置文件加载顺序(推荐)
Apache在sites-enabled目录下是按文件名的字母顺序加载虚拟主机配置的,第一个被加载的虚拟主机会成为服务器的默认站点。如果你的phpMyAdmin配置文件名(比如001-phpmyadmin.conf)比000-default.conf排序靠前,那所有未匹配到具体域名的请求都会优先走phpMyAdmin的配置。
- 先查看当前
sites-enabled目录下的文件排序:ls -l /etc/apache2/sites-enabled/ - 如果phpMyAdmin的配置文件确实排在
000-default.conf前面,先修改它在sites-available里的文件名(因为sites-enabled是软链接):sudo mv /etc/apache2/sites-available/001-phpmyadmin.conf /etc/apache2/sites-available/010-phpmyadmin.conf - 禁用旧的软链接,启用新的:
sudo a2dissite 001-phpmyadmin.conf sudo a2ensite 010-phpmyadmin.conf
方法二:给默认虚拟主机添加通配符匹配
如果不想改文件名,也可以直接在你的000-default.conf里添加通配符别名,让它明确匹配所有未指定的域名:
打开000-default.conf,在ServerAdmin下方添加ServerAlias *,修改后的配置如下:
<VirtualHost *:80> ServerAdmin manoj@mydomain.com ServerAlias * # 这一行会匹配所有未找到对应虚拟主机的域名 DocumentRoot /var/www/html DirectoryIndex index.html index.php <Directory "/var/www/html"> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined ErrorDocument 403 /exceptions/custom_403.php ErrorDocument 404 /exceptions/custom_404.php ErrorDocument 500 /exceptions/custom_50x.php ErrorDocument 502 /exceptions/custom_50x.php ErrorDocument 503 /exceptions/custom_50x.php ErrorDocument 504 /exceptions/custom_50x.php </VirtualHost>
最后一步:验证配置并重启Apache
不管用哪种方法,都要先检查Apache配置是否合法:
sudo apache2ctl configtest
如果输出Syntax OK,就重启Apache服务让配置生效:
sudo systemctl restart apache2
现在再访问你的新子域名http://newsubdomain.mydomain.com,应该会加载/var/www/html下的默认页面,而不是phpMyAdmin了~
备注:内容来源于stack exchange,提问作者Mano Mahe
相关产品推荐
相关产品推荐

