Apache 虚拟主机 VirtualHost 配置

虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同的域名.

Apache 是世界上使用最广的 Web 服务器, 从 1.1 版开始支持虚拟主机. 本文将讲解在不同服务器 (Redhat Enterprise Linux, Ubuntu Linux, Windows) 上使用 Apache 搭建虚拟主机来搭建多个网站.

Apache 虚拟主机 VirtualHost 配置

主旨

本文旨在让读者知道如何在同一台机器上搭建多个网站, 并附带一些使用技巧. 以操作为主, 不会过多谈及原理.

目标

本文是写给拥有一定的服务器配置和管理技能, 工作中需要同时维护多个网站的网站主, 网站开发者和网络管理员. 如果你是互联网公司的配管工程师, 对计算机服务器原理和操作十分熟悉, 请忽视本文, 你不会在上面找到太多有价值的东西.

以下是各操作系统的配置方法.

Redhat Enterprise Linux

Redhat Enterprise Linux (包括 CentOS Linux), 是使用最广的 Linux 服务器, 大量的网站应用都部署在其上.

1. 打开文件 /etc/httpd/conf/httpd.conf, 搜索 VirtualHost example, 找到代码如下:

#
# 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@dummy-host.example.com
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>

2. 仿照例子, 添加一段代码来指定某一域名的网站.

#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /var/www/httpdocs/demo_neoease_com
    ServerName demo.neoease.com
    ErrorLog logs/demo.neoease.com-error.log
    CustomLog logs/demo.neoease.com-access.log common
</VirtualHost>

3. 重启 httpd 服务, 执行以下语句.

service httpd restart

Ubuntu Linux

Ubuntu 在 Linux 各发行版中, 个人用户数量最多的. 很多人在本机和虚拟机中使用. 但 Ubuntu 和 Redhat 的 VirtualHost 设置方法不相同.

1. 打开目录 /etc/apache2/sites-available/, 发现 default 和 default-ssl 两个文件, 其中 default 是 http 虚拟主机服务的配置文件, default-ssl 是配置 https 服务使用的. 可以复制一份 default 文件. 并修改配置文件名, 文件名必须与域名一致 (如: demo.neoease.com)

2. 打开新建的配置文件, 修改 DocumentRoot, ServerName 和对应的配置目录. 例子如下:

#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /var/www/httpdocs/demo_neoease_com
    ServerName demo.neoease.com
    ErrorLog ${APACHE_LOG_DIR}/demo.neoease.com-error.log
    CustomLog ${APACHE_LOG_DIR}/demo.neoease.com-access.log combined
</VirtualHost>

3. 通过 a2ensite 激活虚拟主机配置

sudo a2ensite demo.neoease.com

4. 打开目录 /etc/apache2/sites-enabled/, 你会发现所有激活的虚拟主机, 可以通过 a2dissite 进行注销

sudo a2dissite demo.neoease.com

5. 重启 Apache 服务, 激活虚拟主机

sudo /etc/init.d/apache2 restart

Windows

Windows 是市场占有率最高的 PC 操作系统, 也是很多人的开发环境. 其 VirtualHost 配置方法与 Linux 上有些差异, 以下方式适合原生 Apache, XAMPP 和 WAMP 套件.

1. 打开目录 {Apache2 安装目录}\conf\extra\, 找到 httpd-vhosts.conf 文件.

2. 仿照例子, 添加一段代码来指定某一域名的网站.

#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "D:/workspace/php/demo_neoease_com"
    ServerName demo.neoease.com
    ErrorLog "logs/demo.neoease.com-error.log"
    CustomLog "logs/demo.neoease.com-access.log" common
</VirtualHost>

3. 打开 httpd.conf 文件, 添加如下语句.

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

4. 重启 Apache 服务.

Mac OS

近年苹果的雄起, 让 Mac 日催普及, 也成为很多开发人员的选择. 因为与 Linux 同源, 配置方法也相似.

1. 打开文件 /private/etc/apache2/extra/httpd-vhosts.conf.

2. 仿照例子, 添加一段代码来指定某一域名的网站.

#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/usr/docs/httpdocs/demo_neoease_com"
    ServerName demo.neoease.com
    ErrorLog "/private/var/log/apache2/demo.neoease.com-error_log"
    CustomLog "/private/var/log/apache2/demo.neoease.com-access_log" common
</VirtualHost>

3. 打开文件 /private/etc/apache2/httpd.conf, 搜索 Virtual hosts, 找到代码如下:

# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf

去掉前面的注释符号 #, 保存文件.

4. 重启 apache 服务, 执行以下语句.

sudo apachectl restart

 Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 无论是静态服务器还是小网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高.

我在《Apache 虚拟主机 VirtualHost 配置》介绍了在不同操作系统上使用 Apahce 虚拟主机的方法, 还有那么些朋友想知道 Nginx 虚拟主机配置方法, 本文作为补充也介绍如何 Nginx 上添加虚拟主机.

Nginx 虚拟主机配置

绝大多数的 Nginx 运行在 Linux 机器上, 虽然有 Windows 移植版, 但我也没搭建过. 所以本文将以 Linux 为例讲解, 而 Mac OS 或其他 Unix like 机器上的操作应该是一样的.

增加 Nginx 虚拟主机

这里假设大家的 Nginx 服务器已经安装好, 不懂的请阅读各 Linux 发行版的官方文档或者 LNMP 的安装说明. 配置 Virtual host 步骤如下:

1. 进入 /usr/local/nginx/conf/vhost 目录, 创建虚拟主机配置文件 demo.neoease.com.conf ({域名}.conf).

2. 打开配置文件, 添加服务如下:

server {
	listen       80;
	server_name demo.neoease.com;
	index index.html index.htm index.php;
	root  /var/www/demo_neoease_com;
 
	log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request'
	'$status $body_bytes_sent $http_referer '
	'$http_user_agent $http_x_forwarded_for';
	access_log  /var/log/demo.neoease.com.log demo.neoease.com;
}

3. 打开 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf, 在 http 范围引入虚拟主机配置文件如下:

include vhost/*.conf;

4. 重启 Nginx 服务, 执行以下语句.

service nginx restart

让 Nginx 虚拟主机支持 PHP

在前面第 2 步的虚拟主机服务对应的目录加入对 PHP 的支持, 这里使用的是 FastCGI, 修改如下.

server {
	listen       80;
	server_name demo.neoease.com;
	index index.html index.htm index.php;
	root  /var/www/demo_neoease_com;
 
	location ~ .*\.(php|php5)?$ {
		fastcgi_pass unix:/tmp/php-cgi.sock;
		fastcgi_index index.php;
		include fcgi.conf;
	}
 
	log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request'
	'$status $body_bytes_sent $http_referer '
	'$http_user_agent $http_x_forwarded_for';
	access_log  /var/log/demo.neoease.com.log demo.neoease.com;
}

图片防盗链

图片作为重要的耗流量大的静态资源, 可能网站主并不希望其他网站直接引用, Nginx 可以通过 referer 来防止外站盗链图片.

server {
	listen       80;
	server_name demo.neoease.com;
	index index.html index.htm index.php;
	root  /var/www/demo_neoease_com;
 
	# 这里为图片添加为期 1 年的过期时间, 并且禁止 Google, 百度和本站之外的网站引用图片
	location ~ .*\.(ico|jpg|jpeg|png|gif)$ {
		expires 1y;
		valid_referers none blocked demo.neoease.com *.google.com *.baidu.com;
		if ($invalid_referer) {
			return 404;
		}
	}
 
	log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request'
	'$status $body_bytes_sent $http_referer '
	'$http_user_agent $http_x_forwarded_for';
	access_log  /var/log/demo.neoease.com.log demo.neoease.com;
}

WordPress 伪静态配置

如果将 WordPress 的链接结构设定为 /%postname%/, /%postname%.html 等格式时, 需要 rewrite URL, WordPress 提供 Apache 的 .htaccess 修改建议, 但没告知 Nginx 该如何修改. 我们可以将 WordPress 的虚拟主机配置修改如下:

server {
	listen       80;
	server_name demo.neoease.com;
	index index.html index.htm index.php;
	root  /var/www/demo_neoease_com;
 
	location / {
		if (-f $request_filename/index.html){
			rewrite (.*) $1/index.html break;
		}
		if (-f $request_filename/index.php){
			rewrite (.*) $1/index.php;
		}
		if (!-f $request_filename){
			rewrite (.*) /index.php;
		}
	}
	rewrite /wp-admin$ $scheme://$host$uri/ permanent;
 
	location ~ .*\.(php|php5)?$ {
		fastcgi_pass unix:/tmp/php-cgi.sock;
		fastcgi_index index.php;
		include fcgi.conf;
	}
 
	log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request'
	'$status $body_bytes_sent $http_referer '
	'$http_user_agent $http_x_forwarded_for';
	access_log  /var/log/demo.neoease.com.log demo.neoease.com;
}

LNMP 套件在提供了 WordPress 为静态配置文件 /usr/local/nginx/conf/wordpress.conf, 在虚拟主机配置的 server 范围引用如下即可.

include wordpress.conf;

如果你使用 LNMP 套件, 进入 WordPress 后台发现会出现 404 页面, wp-admin 后面缺少了斜杆 /, 请在 wordpress.conf 最后添加以下语句:

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

后话

一直以来, 我主要在用 Apache, 自从去年从 MT 搬家到 Linode VPS 之后, 发现服务器压力很大, 每隔几天就要宕机一次, 在胡戈戈的协助下转成了 Nginx, 大半年了一直很稳定.

相对 Apache, Nignx 有更加强大的并发能力, 而因为他对进程管理耗用资源也比较少. 而 Apache 比 Nginx 有更多更成熟的可用模块, bug 也比较少. 卖主机的 IDC 选择 Nignx, 因为高并发允许他们创建更多虚拟主机空间更来钱; 淘宝也因此改造 Nignx (Tengine) 作为 CDN 服务器, 可承受更大压力. 


总结

最近我在不同的几台服务器上搭建了一些网站服务, 这篇文章也算是我的个人笔记, 望日后可自用, 也希望对读者有用. 文中介绍了几个主流开发和部署环境上配置虚拟主机的方法, 其他 OS 大同小异. 


原文链接:

http://www.neoease.com/apache-virtual-host/

http://www.neoease.com/nginx-virtual-host/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对你的问题:APACHE虚拟主机配置,以下是回答: 在Apache配置虚拟主机需要在httpd.conf或者其他的配置文件中添加如下代码: ``` <VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/usr/local/apache2/htdocs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "/usr/local/apache2/logs/dummy-host.example.com-error_log" CustomLog "/usr/local/apache2/logs/dummy-host.example.com-access_log" common </VirtualHost> ``` 其中`*`表示监听所有的网络接口,`:80`表示监听80端口,`ServerAdmin`设置管理员邮箱,`DocumentRoot`设置虚拟主机的目录,`ServerName`设置虚拟主机名字,`ServerAlias`设置虚拟主机别名,`ErrorLog`和`CustomLog` 分别设置虚拟主机的错误日志和访问日志。 如果需要配置HTTPS,则需要在虚拟主机配置中添加以下代码: ``` <VirtualHost *:443> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/usr/local/apache2/htdocs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "/usr/local/apache2/logs/dummy-host.example.com-error_log" CustomLog "/usr/local/apache2/logs/dummy-host.example.com-access_log" common SSLEngine on SSLCertificateFile "/path/to/www.example.com.cert" SSLCertificateKeyFile "/path/to/www.example.com.key" SSLCACertificateFile "/path/to/www.example.com.chain" </VirtualHost> ``` 在HTTPS虚拟主机配置中,需要添加以下代码: `SSLEngine on`表示开启SSL加密引擎,`SSLCertificateFile`指定证书位置,`SSLCertificateKeyFile`指定私钥位置,`SSLCACertificateFile`指定证书链位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值