Ubuntu Apache 2.4 配置-虛擬站點、禁止IP訪問
阿新 • • 發佈:2018-12-16
配置環境
- 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.com
和www.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.test.com正常
訪問192.168.1.216禁止
附【清除Apache 2 配置檔案】
# 少數模組配置資訊無法刪除
apt-get autoremove --purge apache2
參考連結
- 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/