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. LINQ使用

    基于扩展方法和lamda表达式 1. 查询序列中满足一定条件 Where扩展方法 public interface ISlotPortBinding { byte SlotNumber { get; ...

  2. Sublime text 3 SVN插件及使用方法

    前提是你电脑里已装有SVN,这时只是设置快捷调用SVN. 1.通过快捷键 ctrl+` 或者 View > Show Console 菜单打开控制台 2.粘贴对应版本的代码后回车安装 适用于 S ...

  3. CString + UINT Error:有多个运算符"+="与这些操作数匹配

    在OnChar中,参数UINT nChar 有一个CString str,现在执行 str += nChar报错:Error:有多个运算符"+="与这些操作数匹配 解决办法:把UI ...

  4. Spring boot中使用springfox来生成Swagger Specification小结

    Rest接口对应Swagger Specification路径获取办法: 根据location的值获取api   json描述文件 也许有同学会问,为什么搞的这么麻烦,api json描述文件不就是h ...

  5. Basic认证

    Basic 概述 Basic 认证是HTTP 中非常简单的认证方式,因为简单,所以不是很安全,不过仍然非常常用. 当一个客户端向一个需要认证的HTTP服务器进行数据请求时,如果之前没有认证过,HTTP ...

  6. 使用NSURLSession请求需要AD认证的HTTPS服务器

    关键代码:使用后台下载PDF文件 - (void)startDownloadPDF{ NSURLSession *session = [self session]; NSString *downloa ...

  7. mysql慢查日志分析工具 percona-toolkit

    备忘自: http://blog.csdn.net/seteor/article/details/24017913 1. 工具简介 pt-query-digest是用于分析mysql慢查询的一个工具, ...

  8. System.Threading.Timer使用心得

    System.Threading.Timer 是一个使用回调方法的计时器,而且由线程池线程服务,简单且对资源要求不高. "只要在使用 Timer,就必须保留对它的引用."对于任何托 ...

  9. linux: centos设置ip以及连接外网

    注明:我使用的的使centos 7,所有文件名是ifcfg-enp0s3, 一. 设置虚拟机中linux的ip,使本地能连通虚拟机的linux系统 1>.进入本地windows的cmd,输入ip ...

  10. javax.xml.ws.webserviceexception class do not have a property of the name

    我是用wsimport生成webservice 的客户端,放到工程里,调用,出现这个异常, 后来发现,是没有把package-info.java这个文件一起放到包里的缘故 解决: 连同package- ...