About Virtual Hosts

虚拟主机,用于在一个单一IP地址上,运行多个域。这对那些想在一个VPS上,运行多个网站的人,尤其有用。基于用户访问的不同网站,给访问者显示不同的信息。没有限制能VPS中,添加的虚拟主机的个数。

Set Up

教程中的这些步骤,需要你有root权限。你可以查看Initial Server Setup ,来学习如何设置初始化服务器。这里使用www.作为root的名字,你可以使用你想用的。

另外,你需要在你的VPS上安装并运行apache。

如果没有安装,你可以通过以下命令安装

sudo yum install httpd

Step One-Create a New Directory

第一步,是创建一个保存新虚拟主机的路径

该路径,会添加到你Apache virtual configuration的Document Root文件中。通过在命令里加一个 –p ,命令会自动新路径的所有不存在的父目录。

sudo mkdir -p /var/www/example.com/public_html

你会需要设计一个实际的DNS,或一个IP地址,来访问呢domain,用以测试该虚拟主机是否工作。在该教程中,我们使用exemple.com作为你的域名的占位符。

然而,如果你要使用一个不能抵达的域名,用来测试,你需要查看第六步,让域名如何在你本地计算机上工作。

Step Two-Grant Permissions

我们需要将路径的所有者,grant为该用户,而不是在root系统中运行。

sudo chown -R www:www /var/www/example.com/public_html

另外,很重要的一点是,要确保任何人都能read我们的新文件。

sudo chmod 755 /var/www

现在,你已经设置好了所有权限。

Step Three-Create the Page

你需要在我们配置好的路径里,创建一个叫做index.html的新文件。

sudo vi /var/www/example.com/public_html/index.html

我们要添加一些文本到该文件,这些文本会在IP重定向到虚拟主机时看到。

<html>
<head>
<title>www.example.com</title>
</head>
<body>
<h1>Success: You Have Set Up a Virtual Host</h1>
</body>
</html>

保存,并退出。

Step Four-Turn on Virtual Hosts

下一步是进入apache configuration文件中。

sudo vi /etc/httpd/conf/httpd.conf

这里有要查找一些行,确保你看到的信息和下面的信息匹配。

#Listen 12.34.56.78:80
Listen 80

滚动到文档的底部,找到叫做Virtual Hosts的章节。

NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
# #
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot /var/www/example.com/public_html
ServerName www.example.com
ServerAlias example.com
ErrorLog /var/www/example.com/error.log
CustomLog /var/www/example.com/requests.log
</VirtualHost>

这里非常重要的行,是NameVirtualHost,Virtual Host,Document Root,Server Name。

-取消对NameVirtualHost的备注(remove the number sign ‘#’),而不改变任何其他设置。星号意味着任何通过80端口的IP地址,都会成为一个虚拟主机。在你的系统中,可能只有一个IP地址,不需要关注。然而,如果你乐意,你可以使用你的IP地址替换星号。

-你可以保留<VirtualHost *:80>之前的备注。从<VirtualHost>开始,取消注释。

-保持<VirtualHost *:80>,它和NameVirtualHost章节的信息要匹配。如果你用你的IP替换了之前的星号,在这里,你也需要这么做。

-Document Root是关键!这里填入我们在第一步中创建的路径表达式。如果document root是错误的或不存在的,你就不能设置该虚拟主机。

-Server Name是另一个重点信息,包含虚拟主机的域名(例如,www.example.com)。确保你你输入完全的域名。我们会在下一行写入任何该域的可能。

-ServerAlias是配置文件中的新行,默认它不在这儿。添加它,将允许你列出一些域名的变体,如没有www的 example.com。

在本节中,剩余的行不需要设置给虚拟主机。然而,知道他们是做什么,依然会有帮助。

-Server admin 请求网站所有者的email。

-Error Logs和Custom Logs保持跟踪任何服务器的异常。error log包含服务器运行期间引起的一场,custom log跟踪服务器的请求。你可以设置一个自定义的位置,为这些处理。

-确保<VirtualHost>取消备注;然后保存并退出。

Step Five-Restart Appache

我们已经对配置做了一些修改。然而,不重启Apache,他们不会生效。

首先关闭所有apache 进程:

sudo apachectl -k stop

然后,再次启动apache

sudo /etc/init.d/httpd start

你可能会看到下面的错误:

Could not reliably determine the server's fully qualified domain name,
 using 127.0.0.1 for ServerName

该信息仅仅是一个warning,你依然能访问你的虚拟主机。

Optional Step Six-Setting Up the Local Hosts

如果你已经将你的域名制定你的VPS的IP地址,你可以跳过该步骤-你不需要设置本地主机。你的虚拟主机已经可以工作。然而,如果你的新虚拟主机没有连接到一个实际的域名,你需要设置在你的电脑端设置本地主机。

确保你在你的电脑端,而不是VPS上。

要进行该步,你需要知道你计算机的管理员密码。另外,你需要使用一个实际的域名来测试虚拟主机。

如果你在Mac或Linux,访问电脑的root user(su),打开你的hosts文件

nano /etc/hosts

如果你在Windows电脑,。。。。

添加下面的信息到该文件。定位到浏览器,example.com会给你相应IP的所有虚拟主机细节。

# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost #Virtual Hosts
12.34.56.789 www.example.com

然而,这可能不是一个好主意。

Step Seven-RESULTS: See Your Virtual Host in Action

一旦你完成了虚拟主机的设置,你可以在线看到它。输入你IP地址到浏览器。

它会显示上面咱们添加index.html的信息。

Adding More Virtual Hosts

要创建额外的虚拟主机,你可以重复上面的步骤,每次设置新document root的时候,都要小心,使用相应的新域名。然后,仅仅拷贝粘贴新虚拟主机的信息,到Apache Config文件,看起来像下面这样:

<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot /var/www/example.com/public_html
ServerName www.example.com
ServerAlias example.com
ErrorLog /etc/var/www/example.com/error.log
CustomLog /var/www/example.com/requests.log
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@example.org
DocumentRoot /var/www/example.org/public_html
ServerName www.example.org
ServerAlias example.org
ErrorLog /var/www/example.org/error.log
CustomLog /var/www/example.orgrequests.log
</VirtualHost>

See More

一旦你设置好虚拟主机,你可以执行Create a SSL Certificate for your site or Install an FTP server 。

How To Set Up Apache Virtual Hosts on CentOS 6的更多相关文章

  1. Configure Apache Virtual Hosts - CentOS 7

    Difficulty: 2Time: 15 minutes Want to host websites on your server? Using Apache? Great. This articl ...

  2. [转]Windows 下 Apache Virtual hosts 简单配置

    From : http://blog.csdn.net/wuerping/article/details/4164362 /* Author : Andrew.Wu [ Created on : 20 ...

  3. #Virtual hosts #Include conf/extra/httpd-vhosts.conf 开启就不能启动apache

    #Virtual hosts#Include conf/extra/httpd-vhosts.conf我只要把其中任何一个开启就是吧#去掉就启动不了apache.怎么回事error.log是这样的ht ...

  4. Apache配置基于IP的虚拟主机 Apache virtual host configuration is based on the IP

    Step 1: 检查是否开启 httpd-vhosts.conf apache/conf/httpd.conf文件 # Virtual hosts Include conf/extra/httpd-v ...

  5. Apache配置基于端口号的虚拟主机 Apache virtual host configuration is based on the port

    有可能只有一个ip出口,但却有多个项目,那么就需要基于端口号架设虚拟主机. Step 1: 检查是否开启 httpd-vhosts.conf apache/conf/httpd.conf文件 # Vi ...

  6. [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation

    我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. With exchanges, bindings, and queues under your belt, you mig ...

  7. How To Install Apache Tomcat 7 on CentOS 7 via Yum

    摘自:https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-7-on-centos-7-via-y ...

  8. Virtual Box中 CentOS双网卡设置

    Virtual Box中 CentOS双网卡设置:   在Virtual Box中安装CentOS x86-64 6.4(final),配置了双网卡,eth0 为桥接模式 , eth1为内网模式   ...

  9. Install LAMP Server (Apache, MariaDB, PHP) On CentOS/RHEL/Scientific Linux 7

    Install LAMP Server (Apache, MariaDB, PHP) On CentOS/RHEL/Scientific Linux 7 By SK  - August 12, 201 ...

随机推荐

  1. linux修改hostname

    1.如果只是修改hostname可以通过如下命令 hostname newHostname 注意:这种修改方式只有当前有效,等服务器重启后hostname就会失效,回到原来的hostname. 2.如 ...

  2. python_计算一段文本各个字符的出现个数

    >题目要求 任意给定一段文本,求出每个字符出现的个数,并且打印出来 >程序实现 import pprint str01 = "重庆市,简称巴和渝,别称山城.渝都.雾都.桥都,中华 ...

  3. linux查看公网地址

    curl cip.cc curl http://members.3322.org/dyndns/getip

  4. Unable to instantiate Action...............

    当时找了好久都没搞明白,出现这样的问题,可能不是代码错了.将新添加的action不要添加在末尾,这样可能导致这样的问题出现,所以新添加的action最好放置在struts配置文件中间.

  5. Java基础之访问文件与目录——测试文件或目录的路径(TryPath)

    控制台程序,测试文件或目录的路径. import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.FileSy ...

  6. 生成arff文件,csv转为arff

    一.什么是arff格式文件 1.arff是Attribute-Relation File Format缩写,从英文字面也能大概看出什么意思.它是weka数据挖掘开源程序使用的一种文件模式.由于weka ...

  7. iOS中scrollview是否要回弹

    1. @property(nonatomic) BOOL bounces //当滚动到内容边缘是否发生反弹,default is YES.2. @property(nonatomic) BOOL al ...

  8. PostgreSQL Type的创建与Type在函数中的使用

    postgres=# create type complex as(postgres(# r double precision,postgres(# i double precisionpostgre ...

  9. Python之urllib2

    urllib2 - extensible library for opening URLs Note The urllib2 module has been split across several ...

  10. 转:python webdriver API 之 验证码问题

    对于 web 应用来说,大部分的系统在用户登录时都要求用户输入验证码,验证码的类型的很多,有字母数字的,有汉字的,甚至还要用户输入一条算术题的答案的,对于系统来说使用验证码可以有效果的防止采用机器猜测 ...