ubuntu 14.04设置Apache虚拟主机的方法
<p><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>方案 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>在这个教程中,我会使用Ubuntu 14.04 32位 LTS,并搭建2个测试网站分别命名为“unixmen1.local” 和 “unixmen2.local”.我的测试机分别为192.168.1.250/24和server.unixmen.local。你可以根据你的需要更改虚拟域名。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>安装Apache网站服务器 </strong><br><br><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>安装apache服务器之前,我们来更新一下我们的Ubuntu服务器: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>sudo apt-get update然后,用下面命令来安装apache网络服务器: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>sudo apt-get install apache2安装apache服务器之后,让我们通过这个URL http://你的服务器的IP地址/ 来测试网站服务器是否正常工作 </span></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
<font color="#428bca"><span><img style="max-width:100%!important;height:auto!important;"title="ubuntu 14.04设置Apache虚拟主机的方法" alt="ubuntu 14.04设置Apache虚拟主机的方法" src="https://zhuji.jb51.net/uploads/img/202305/a49f9a0094a9374622973b79a0c1aa16.jpg"></span></font></p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>如你所见,apache服务器已经工作了。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>设置虚拟主机 <br><br>
1.创建虚拟目录 </strong><br><br><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>现在,让我们继续安装虚拟主机。正如我先前所述,我要新建2台虚拟主机分别命名为“unixmen1.local”和“unixmen2.local”. </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>创建一个公用的文件夹来存放这两台虚拟主机的数据。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>首先,让我们为unixmen1.local这个站点创建一个目录: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>sudo mkdir -p /var/www/unixmen1.local/public_html接着,为for unixmen2.local站点创建一个目录: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>sudo mkdir -p /var/www/unixmen2.local/public_html </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>2. 设置所有者和权限 </strong><br><br><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>上面目录现在只有root拥有权限。我们需要修改这2个目录的拥有权给普通用户,而不仅仅是root用户。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>sudo chown -R $USER:$USER /var/www/unixmen1.local/public_html/ </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>sudo chown -R $USER:$USER /var/www/unixmen2.local/public_html/ </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>“$USER”变量指向了当前的登录用户。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>设置读写权限给apache网页根目录(/var/www)及其子目录,这样每个人都可以从目录中读取文件。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>sudo chmod -R 755 /var/www/这样,我们就创建好了一些文件夹来保存网络相关数据并分配必要的权限和所属用户。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>4. 为虚拟主机创建示例页 </strong><br><br><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>现在,我们给网站增加示例页。第一步,让我们给虚拟主机unixmen1.local创建一个示例页。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>给unixmen1.local虚拟主机创建一个示例页, </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>sudo vi /var/www/unixmen1.local/public_html/index.html添加以下内容:</span></p>
<div class="codeText" style='margin: 8px 0px 1.1em; padding: 0px 0px 5px; outline: none; line-height: normal; border: 1px solid rgb(165, 165, 248); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-family: "Courier New", monospace; overflow: hidden; width: 686px; word-break: break-all;'>
<p class="codeHead">
<span class="lantxt">XML/HTML Code</span><span class="copyCodeText">复制内容到剪贴板</span></p>
<div id="code_5924">
<ol class="dp-xml">
<li class="alt">
<p>
<span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></p>
</li>
<li>
<p>
<span> </span><span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span> </span></p>
</li>
<li class="alt">
<p>
<span> </span><span class="tag"><</span><span class="tag-name">title</span><span class="tag">></span><span>www.unixmen1.local</span><span class="tag"></</span><span class="tag-name">title</span><span class="tag">></span><span> </span></p>
</li>
<li>
<p>
<span> </span><span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span> </span></p>
</li>
<li class="alt">
<p>
<span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span> </span></p>
</li>
<li>
<p>
<span> </span><span class="tag"><</span><span class="tag-name">h1</span><span class="tag">></span><span>Welcome To Unixmen1.local website</span><span class="tag"></</span><span class="tag-name">h1</span><span class="tag">></span><span> </span></p>
</li>
<li class="alt">
<p>
<span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span> </span></p>
</li>
<li>
<p>
<span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span> </span></p>
</li>
</ol>
</div>
</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
保存并关闭文件。<br>
同样的,添加示例页到第二台虚拟主机。<br>
sudo vi /var/www/unixmen2.local/public_html/index.html添加以下内容:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p class="right">
<span><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p class="msgborder" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
<html> <br>
<head> <br>
<title>www.unixmen2.local</title> <br>
</head> <br>
<body> <br>
<h1>Welcome To Unixmen2.local website</h1> <br>
</body><br>
</html></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
保存并关闭文件。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
<strong>5. 创建虚拟主机配置文件</strong></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
默认情况下,apache有一个默认的虚拟主机文件叫000-default.conf。我们将会复制000-default.conf文件内容到我们新的虚拟主机配置文件中。<br>
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/unixmen1.local.conf<br>
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/unixmen2.local.conf确保虚拟主机配置文件末尾包含.conf扩展名。<br>
现在,修改unximen1.local.conf文件以符合需求。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
sudo vi /etc/apache2/sites-available/unixmen1.local.conf使相关的变化直接呈现在unixmen1站点中(译注:以“#”开头的注释行可以忽略。)。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p class="right">
<span><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p class="msgborder" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
<VirtualHost *:80> <br>
# The ServerName directive sets the request scheme, hostname and port that <br>
# the server uses to identify itself. This is used when creating <br>
# redirection URLs. In the context of virtual hosts, the ServerName <br>
# specifies what hostname must appear in the request's Host: header to <br>
# match this virtual host. For the default virtual host (this file) this <br>
# value is not decisive as it is used as a last resort host regardless. <br>
# However, you must set it for any further virtual host explicitly. <br>
#<br>
ServerName www.example.com <br>
ServerAdmin webmaster@unixmen1.local <br>
ServerName unixmen1.local <br>
ServerAlias www.unixmen1.local <br>
DocumentRoot /var/www/unixmen1.local/public_html <br>
# Available loglevels: trace8, …, trace1, debug, info, notice, warn, <br>
# error, crit, alert, emerg. <br>
# It is also possible to configure the loglevel for particular <br>
# modules, e.g. <br>
#<br>
#LogLevel info ssl:warn <br>
ErrorLog ${APACHE_LOG_DIR}/error.log <br>
CustomLog ${APACHE_LOG_DIR}/access.log comb<br>
# For most configuration files from conf-available/, which are <br>
# enabled or disabled at a global level, it is possible to <br>
# include a line for only one particular virtual host. For example the <br>
# following line enables the CGI configuration for this host only <br>
# after it has been globally disabled with "a2disconf". <br>
#<br>
#Include conf-available/serve-cgi-bin.conf<br>
</VirtualHost></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
修改虚拟主机文件后,禁用默认的虚拟主机配置(000.default.conf),然后启用新的虚拟主机配置,如下所示。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p class="right">
<span><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p class="msgborder" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
sudo a2dissite 000-default.conf<br>
sudo a2ensite unixmen1.local.conf<br>
sudo a2ensite unixmen2.local.conf</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
最后,重启apache服务器。<br>
sudo service apache2 restart</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
就是这样。现在,我们成功地配置了apach虚拟主机在我们的Ubuntu服务器上<br>
测试虚拟主机<br>
编辑/tc/hosts文件,<br>
sudo vi /etc/hosts在文件末尾添加如下所示的虚拟域名。<br>
192.168.1.250 unixmen1.local192.168.1.250 unixmen2.local保存并关闭文件。<br>
打开你的浏览器并访问http://unixmen1.local 或 http://unixmen2.local。你将会看到我们之前创建的示例页。<br><br>
Unixmen1.local 测试页:<br>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
<font color="#428bca"><span><img style="max-width:100%!important;height:auto!important;"title="ubuntu 14.04设置Apache虚拟主机的方法" alt="ubuntu 14.04设置Apache虚拟主机的方法" src="https://zhuji.jb51.net/uploads/img/202305/72fb2306c95cf5c3b72b25bbf18aa701.jpg"></span></font></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
Unixmen2.local 测试页</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
<font color="#428bca"><span><img style="max-width:100%!important;height:auto!important;"title="ubuntu 14.04设置Apache虚拟主机的方法" alt="ubuntu 14.04设置Apache虚拟主机的方法" src="https://zhuji.jb51.net/uploads/img/202305/3b733f24972ffc1618dda6256fccd881.jpg"></span></font></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
如果你想从你的远程系统访问这些站点,你需要在你的DNS服务器添加实际域名记录。不过,我没有真实的域名和DNS服务器,我只想通过我的本地系统测试,那么它刚好如我所愿地工作。</p>
頁:
[1]