Apache 虚拟主机configuration
虚拟主机 is Apache 一个 important features, 它允许 in 同一台server on 托管 many 个网站. through虚拟主机, 您可以using不同 域名 or IP地址来访问不同 网站, 而不需要 for 每个网站配备单独 server. 本文将详细介绍Apache虚拟主机 两种configuration方式: 基于名称 虚拟主机 and 基于IP 虚拟主机.
虚拟主机 basicconcepts
虚拟主机 (Virtual Host) is 指 in 同一台物理server on run many 个网站 techniques. Apachesupport两种class型 虚拟主机:
- 基于名称 虚拟主机: using不同 域名来区分不同 网站, many 个网站共享同一个IP地址 and 端口.
- 基于IP 虚拟主机: for 每个网站分配不同 IP地址, 每个网站using相同 or 不同 端口.
选择虚拟主机class型
基于名称 虚拟主机 is 最常用 方式, 因 for 它可以 in 同一IP地址 on 托管 many 个网站, 节省IP地址resource. 基于IP 虚拟主机通常用于需要 for 每个网站分配独立IP地址 场景, 例such as需要using不同SSLcertificate 网站.
基于名称 虚拟主机configuration
基于名称 虚拟主机 is throughHTTPrequest头in Host字段来区分不同 网站 . 当客户端发送HTTPrequest时, 会 in request头inpackage含Host字段, 指定要访问 域名. Apache根据这个字段来确定要using哪个虚拟主机configuration.
configuration步骤
步骤1: creation网站Table of Contents
首先, for 每个虚拟主机creation一个网站Table of Contents:
# creation第一个网站Table of Contents
sudo mkdir -p /var/www/example.com/public_html
# creation第二个网站Table of Contents
sudo mkdir -p /var/www/test.com/public_html
步骤2: 设置Table of Contentspermission
设置适当 Table of Contentspermission:
sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/test.com/public_html
sudo chmod -R 755 /var/www
步骤3: creationtest页面
for 每个网站creation一个test页面:
# for example.comcreationtest页面
echo '<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example.com</title>
</head>
<body>
<h1>欢迎访问 Example.com!</h1>
<p>这 is a 基于名称 虚拟主机example. </p>
</body>
</html>' > /var/www/example.com/public_html/index.html
# for test.comcreationtest页面
echo '<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test.com</title>
</head>
<body>
<h1>欢迎访问 Test.com!</h1>
<p>这 is 另一个基于名称 虚拟主机example. </p>
</body>
</html>' > /var/www/test.com/public_html/index.html
步骤4: creation虚拟主机configurationfile
in Ubuntu/Debiansystem on , 虚拟主机configurationfile存放 in /etc/apache2/sites-available/Table of Contentsin:
# creationexample.com 虚拟主机configurationfile
sudo nano /etc/apache2/sites-available/example.com.conf
添加以 under configuration:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
同样, creationtest.com 虚拟主机configurationfile:
# creationtest.com 虚拟主机configurationfile
sudo nano /etc/apache2/sites-available/test.com.conf
添加以 under configuration:
<VirtualHost *:80>
ServerAdmin admin@test.com
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/public_html
ErrorLog ${APACHE_LOG_DIR}/test.com_error.log
CustomLog ${APACHE_LOG_DIR}/test.com_access.log combined
</VirtualHost>
步骤5: 启用虚拟主机
usinga2ensitecommands启用虚拟主机:
sudo a2ensite example.com.conf
sudo a2ensite test.com.conf
步骤6: testconfiguration并重启Apache
testconfigurationfile 语法 is 否正确:
sudo apache2ctl configtest
such as果configuration正确, 重启Apacheservice:
sudo systemctl restart apache2
步骤7: configurationDNS
for 了able tothrough域名访问虚拟主机, 需要configurationDNSserver, 将域名解析 to server IP地址. in testenvironmentin, 您可以modify本地计算机 /etc/hostsfile (Linux/macOS) or C:\Windows\System32\drivers\etc\hostsfile (Windows) :
# 添加以 under 行 to hostsfile
192.168.1.100 example.com www.example.com
192.168.1.100 test.com www.test.com
其in, 192.168.1.100 is 您 server IP地址.
步骤8: test虚拟主机
in 浏览器in分别访问http://example.com and http://test.com, 您应该能看 to for 应 test页面.
基于IP 虚拟主机configuration
基于IP 虚拟主机 is through不同 IP地址来区分不同 网站 . for 了using基于IP 虚拟主机, 您 server需要 has many 个IP地址.
configuration步骤
步骤1: for server添加 many 个IP地址
首先, 需要 for server添加 many 个IP地址. 具体method取决于您 serverenvironment, 以 under is in Linuxsystem on 添加临时IP地址 method:
# 添加第一个IP地址
sudo ip addr add 192.168.1.100/24 dev eth0
# 添加第二个IP地址
sudo ip addr add 192.168.1.101/24 dev eth0
注意: 这种method添加 IP地址 in server重启 after 会loss. 要永久添加IP地址, 需要modifynetworkconfigurationfile.
步骤2: creation网站Table of Contents
for 每个虚拟主机creation一个网站Table of Contents:
# creation第一个网站Table of Contents
sudo mkdir -p /var/www/ip1.com/public_html
# creation第二个网站Table of Contents
sudo mkdir -p /var/www/ip2.com/public_html
步骤3: 设置Table of Contentspermission
设置适当 Table of Contentspermission:
sudo chown -R $USER:$USER /var/www/ip1.com/public_html
sudo chown -R $USER:$USER /var/www/ip2.com/public_html
sudo chmod -R 755 /var/www
步骤4: creationtest页面
for 每个网站creation一个test页面:
# for ip1.comcreationtest页面
echo '<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IP1.com</title>
</head>
<body>
<h1>欢迎访问 IP1.com!</h1>
<p>这 is a 基于IP 虚拟主机example, IP地址 for 192.168.1.100. </p>
</body>
</html>' > /var/www/ip1.com/public_html/index.html
# for ip2.comcreationtest页面
echo '<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IP2.com</title>
</head>
<body>
<h1>欢迎访问 IP2.com!</h1>
<p>这 is 另一个基于IP 虚拟主机example, IP地址 for 192.168.1.101. </p>
</body>
</html>' > /var/www/ip2.com/public_html/index.html
步骤5: creation虚拟主机configurationfile
creation基于IP 虚拟主机configurationfile:
# creationip1.com 虚拟主机configurationfile
sudo nano /etc/apache2/sites-available/ip1.com.conf
添加以 under configuration, 指定IP地址 for 192.168.1.100:
<VirtualHost 192.168.1.100:80>
ServerAdmin admin@ip1.com
ServerName ip1.com
DocumentRoot /var/www/ip1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/ip1.com_error.log
CustomLog ${APACHE_LOG_DIR}/ip1.com_access.log combined
</VirtualHost>
同样, creationip2.com 虚拟主机configurationfile:
# creationip2.com 虚拟主机configurationfile
sudo nano /etc/apache2/sites-available/ip2.com.conf
添加以 under configuration, 指定IP地址 for 192.168.1.101:
<VirtualHost 192.168.1.101:80>
ServerAdmin admin@ip2.com
ServerName ip2.com
DocumentRoot /var/www/ip2.com/public_html
ErrorLog ${APACHE_LOG_DIR}/ip2.com_error.log
CustomLog ${APACHE_LOG_DIR}/ip2.com_access.log combined
</VirtualHost>
步骤6: 启用虚拟主机
usinga2ensitecommands启用虚拟主机:
sudo a2ensite ip1.com.conf
sudo a2ensite ip2.com.conf
步骤7: testconfiguration并重启Apache
testconfigurationfile 语法 is 否正确:
sudo apache2ctl configtest
such as果configuration正确, 重启Apacheservice:
sudo systemctl restart apache2
步骤8: test虚拟主机
in 浏览器in分别访问http://192.168.1.100 and http://192.168.1.101, 您应该能看 to for 应 test页面.
虚拟主机configuration best practices
- using单独 configurationfile: for 每个虚拟主机creation单独 configurationfile, 便于management and maintenance.
- using单独 logfile: for 每个虚拟主机configuration单独 errorlog and 访问log, 便于排查issues.
- 设置适当 Table of Contentspermission: 确保网站Table of Contents permission设置正确, 避免securityissues.
- 启用Table of Contentsindex: 根据需要启用 or 禁用Table of Contentsindexfunctions.
- usingServerAlias: for 每个虚拟主机设置适当 ServerAlias, processingwww and 非wwwversion 域名.
- testconfiguration: in 重启Apache之 before , 始终using
apachectl configtestcommandstestconfigurationfile 语法.
Notes
in configuration虚拟主机时, 应注意以 under 几点:
- 确保DNSconfiguration正确, 将域名解析 to server IP地址.
- such as果using基于名称 虚拟主机, 确保所 has 域名都指向同一个IP地址.
- such as果using基于IP 虚拟主机, 确保server has 足够 IP地址.
- in produceenvironmentin, 应 for 每个网站configuration适当 security措施, such asSSLcertificate, 访问控制etc..
实践case: configuration many 个基于名称 虚拟主机
步骤1: 准备工作
fake设我们要 in 同一台server on 托管三个网站:
- example.com - 公司主网站
- blog.example.com - 公司博客
- shop.example.com - 公司网 on 商店
步骤2: creation网站Table of Contents
for 每个网站creationTable of Contentsstructure:
sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/blog.example.com/public_html
sudo mkdir -p /var/www/shop.example.com/public_html
步骤3: 设置Table of Contentspermission
设置适当 Table of Contentspermission:
sudo chown -R $USER:$USER /var/www/
sudo chmod -R 755 /var/www/
步骤4: creationtest页面
for 每个网站creationtest页面:
# creationexample.com test页面
echo '<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example.com - 公司主网站</title>
</head>
<body>
<h1>欢迎访问 Example.com!</h1>
<p>这 is 公司 主网站. </p>
</body>
</html>' > /var/www/example.com/public_html/index.html
# creationblog.example.com test页面
echo '<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog - Example.com</title>
</head>
<body>
<h1>欢迎访问公司博客!</h1>
<p>这 is 公司 博客网站. </p>
</body>
</html>' > /var/www/blog.example.com/public_html/index.html
# creationshop.example.com test页面
echo '<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shop - Example.com</title>
</head>
<body>
<h1>欢迎访问公司网 on 商店!</h1>
<p>这 is 公司 网 on 商店. </p>
</body>
</html>' > /var/www/shop.example.com/public_html/index.html
步骤5: creation虚拟主机configurationfile
for 每个网站creation虚拟主机configurationfile:
# creationexample.com configurationfile
sudo nano /etc/apache2/sites-available/example.com.conf
添加以 under configuration:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
同样, creationblog.example.com and shop.example.com configurationfile:
# creationblog.example.com configurationfile
sudo nano /etc/apache2/sites-available/blog.example.com.conf
添加以 under configuration:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName blog.example.com
DocumentRoot /var/www/blog.example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/blog.example.com_error.log
CustomLog ${APACHE_LOG_DIR}/blog.example.com_access.log combined
</VirtualHost>
# creationshop.example.com configurationfile
sudo nano /etc/apache2/sites-available/shop.example.com.conf
添加以 under configuration:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName shop.example.com
DocumentRoot /var/www/shop.example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/shop.example.com_error.log
CustomLog ${APACHE_LOG_DIR}/shop.example.com_access.log combined
</VirtualHost>
步骤6: 启用虚拟主机
启用所 has 虚拟主机:
sudo a2ensite example.com.conf
sudo a2ensite blog.example.com.conf
sudo a2ensite shop.example.com.conf
步骤7: testconfiguration并重启Apache
testconfigurationfile 语法:
sudo apache2ctl configtest
重启Apacheservice:
sudo systemctl restart apache2
步骤8: test网站
in 浏览器in分别访问三个网站, verification它们 is 否正常工作.
互动练习
练习1: configuration基于名称 虚拟主机
in 您 Apacheserver on configuration两个基于名称 虚拟主机:
- 域名1: yourname.com
- 域名2: test.yourname.com
for 每个虚拟主机creation simple test页面, 并verification它们 is 否可以through域名访问.
练习2: configuration基于IP 虚拟主机
such as果您 server has many 个IP地址, 尝试configuration两个基于IP 虚拟主机, 每个虚拟主机using不同 IP地址.
练习3: optimization虚拟主机configuration
for 您 虚拟主机configuration添加以 under functions:
- 单独 logfile
- Table of Contents访问控制
- 自定义error页面