Ubuntu Apache 2.4 配置-虚拟站点、禁止IP访问

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LeoForBest/article/details/74940987

配置环境


  • Ubuntu Server 16.04
  • 域名 www.example.com,www.test.com
  • IP地址192.168.1.216

要求 输入域名 www.example.com,www.test.com能够正常访问,禁止直接输入IP地址访问。

安装Apache2,备份原有配置

apt-get update
apt-get install apache2
# 禁用默认站点配置
a2dissite 000-default.conf
service apache2 reload
# 以默认配置为模板,创建 www.example.com, www.test.com
cp /etc/apache2/sites-available/000-default.conf \
 /etc/apache2/sites-available/www.example.com.conf
cp /etc/apache2/sites-available/000-default.conf \
 /etc/apache2/sites-available/www.test.com.conf
# 分别创建网站根目录
mkdir /var/www/html/www.example.com
mkdir /var/www/html/www.test.com
chown www-data:www-data /var/www/html/www.example.com
chown www-data:www-data /var/www/html/www.test.com

配置虚拟站点,禁用IP直接访问

修改 /etc/apache2/sites-available/www.example.com.conf

# 禁止IP直接访问
<VirtualHost *:80>
        # ServerName 填写你服务器的IP
        ServerName 192.168.1.216
        DocumentRoot /dev/null
        Redirect 403 /
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# 配置虚拟站点 www.example.com
<VirtualHost *:80>
        ServerName www.example.com
        DocumentRoot /var/www/html/www.example.com
         # 禁止通过浏览器直接访问Log文件
        <Files ~ ".log">
                Order allow,deny
                Deny from all
        </Files>
        ErrorLog /var/www/html/www.example.com/error.log
        CustomLog /var/www/html/www.example.com/access.log combined
</VirtualHost>

修改 /etc/apache2/sites-available/www.test.com.conf

# 禁止IP直接访问
<VirtualHost *:80>
        # ServerName 填写你服务器的IP
        ServerName 192.168.1.216
        DocumentRoot /dev/null
        Redirect 403 /
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# 配置虚拟站点 www.test.com
<VirtualHost *:80>
        ServerName www.test.com
        DocumentRoot /var/www/html/www.test.com
        # 禁止通过浏览器直接访问Log文件
        <Files ~ ".log">
                Order allow,deny
                Deny from all
        </Files>
        ErrorLog /var/www/html/www.test.com/error.log
        CustomLog /var/www/html/www.test.com/access.log combined
</VirtualHost>

启用配置文件www.example.comwww.test.com

a2ensite www.example.com.conf
a2ensite www.test.com.conf
service apache2 reload

测试访问Windows

修改host文件指向域名到www.example.com,www.test.com

192.168.1.216 www.example.com
192.168.1.216 www.test.com

测试域名访问

# 复制默认index.html到两个站点下,测试访问
cp /var/www/html/index.html /var/www/html/www.example.com/
cp /var/www/html/index.html /var/www/html/www.test.com/

访问www.example.com正常
访问www.example.com正常
访问www.test.com正常
访问www.test.com正常
访问192.168.1.216禁止
访问192.168.1.216禁止

附【清除Apache 2 配置文件】

# 少数模块配置信息无法删除
apt-get autoremove --purge apache2

参考链接

  1. block-direct-ip-access-to-your-server-in-apache-2-4
    http://nikles.it/block-direct-ip-access-to-your-server-in-apache-2-4/

猜你喜欢

转载自blog.csdn.net/LeoForBest/article/details/74940987
今日推荐