How To Set Up Apache Virtual Hosts on CentOS 6
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的更多相关文章
- Configure Apache Virtual Hosts - CentOS 7
Difficulty: 2Time: 15 minutes Want to host websites on your server? Using Apache? Great. This articl ...
- [转]Windows 下 Apache Virtual hosts 简单配置
From : http://blog.csdn.net/wuerping/article/details/4164362 /* Author : Andrew.Wu [ Created on : 20 ...
- #Virtual hosts #Include conf/extra/httpd-vhosts.conf 开启就不能启动apache
#Virtual hosts#Include conf/extra/httpd-vhosts.conf我只要把其中任何一个开启就是吧#去掉就启动不了apache.怎么回事error.log是这样的ht ...
- 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 ...
- Apache配置基于端口号的虚拟主机 Apache virtual host configuration is based on the port
有可能只有一个ip出口,但却有多个项目,那么就需要基于端口号架设虚拟主机. Step 1: 检查是否开启 httpd-vhosts.conf apache/conf/httpd.conf文件 # Vi ...
- [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation
我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. With exchanges, bindings, and queues under your belt, you mig ...
- 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 ...
- Virtual Box中 CentOS双网卡设置
Virtual Box中 CentOS双网卡设置: 在Virtual Box中安装CentOS x86-64 6.4(final),配置了双网卡,eth0 为桥接模式 , eth1为内网模式 ...
- 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 ...
随机推荐
- APICloud上openFrameGroup把菜单挡住了,怎么处理?
问:openFrameGroup把菜单挡住了,怎么处理? 试了sendFrameToBack没反应,又不能页面自己openFrameGroup,不知道该怎么办.而且用另外的页面先openFrameGr ...
- storm 随机发送字符串
Storm的程序叫做Topology,类似MapReduce job 一个Topolog应该有Spout,代表数据源,和若干个bolt 首先写一个Spout public class RandomSp ...
- UIButton属性
1.UIButton状态: UIControlStateNormal // 正常状态 UIControlStateHighlighted // 高亮状态 UICo ...
- play项目部署
首先对于现场的数据库,一定要谨慎谨慎再谨慎,特别是保存有重要数据的. 使用expdp命令导入数据库: 事前准备: 1.确保linux服务器上已经正确安装oracle (10g以上版本) 2.有Xshe ...
- IntelliJ IDEA 常用设置讲解2
IntelliJ IDEA 有很多人性化的设置我们必须单独拿出来讲解,也因为这些人性化的设置让我们这些 IntelliJ IDEA 死忠粉更加死心塌地使用它和分享它. 常用设置 如上图 Gif 所示, ...
- 如果类的属性是copy的NSString类型 用set方法拦截时
@property (nonatomic, copy) NSString *badgeValue; - (void)setBadgeValue:(NSString *)badgeValue { _ba ...
- Ubuntu Firefox installs Flashplayer
Adobe flash 下载(https://get.adobe.com/flashplayer/) tar.gz版本(注:adobe 提供了yum,rpm,tar.gz和APT四种版本,yum和t ...
- Linux: .vimrc
set nuset autoindentset cindent"set tabstop=2"set shiftwidth=2set cursorlineset hlsearch&q ...
- 数据可视化工具zeppelin安装
介绍 zeppelin主要有以下功能 数据提取 数据发现 数据分析 数据可视化 目前版本(0.5-0.6)之前支持的数据搜索引擎有如下 安装 环境 centOS 6.6 编译准备工作 sudo yum ...
- 夺命雷公狗---TP商城----TP之配置环境---1
下载到tp3.2.3版本后架设到自己的wamp环境下,然后配置虚拟主机,完事后直接开工 环境下创建一个文件夹,然后里面存放这这两个文件即可开始新的旅途了 这里完了,下一步就开始配置index.php文 ...