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

来源:互联网 发布:windows cmd shell 编辑:程序博客网 时间:2024/05/16 01:30

  • 配置环境
    • 安装Apache2备份原有配置
    • 配置虚拟站点禁用IP直接访问
      • 修改 etcapache2sites-availablewwwexamplecomconf
      • 修改 etcapache2sites-availablewwwtestcomconf
      • 启用配置文件wwwexamplecom和wwwtestcom
    • 测试访问Windows
      • 修改host文件指向域名到wwwexamplecomwwwtestcom
      • 测试域名访问
  • 附清除Apache 2 配置文件
  • 参考链接

配置环境


  • 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 updateapt-get install apache2# 禁用默认站点配置a2dissite 000-default.confservice apache2 reload# 以默认配置为模板,创建 www.example.com, www.test.comcp /etc/apache2/sites-available/000-default.conf \ /etc/apache2/sites-available/www.example.com.confcp /etc/apache2/sites-available/000-default.conf \ /etc/apache2/sites-available/www.test.com.conf# 分别创建网站根目录mkdir /var/www/html/www.example.commkdir /var/www/html/www.test.comchown www-data:www-data /var/www/html/www.example.comchown 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.confa2ensite www.test.com.confservice apache2 reload

测试访问Windows

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

192.168.1.216 www.example.com192.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/
原创粉丝点击