一:apache虚拟主机简介
linux主机上安装了apache这个web服务器,它只服务bbs.zijian.com这个论坛,所以只有一个网站根目录,这时候就没必要用到虚拟主机,这个主机都时bbs.zijian.com的,它的配置直接在apache主配置文件里面修改
shell> vim /usr/local/apache/conf/httpd.conf
-----------------------------------------------
#DocumentRoot "/var/www/bbs" #网站根目录
<Directory "/var/www/bbs"> #针对根目录设置相应权限
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
-----------------------------------------------
另一种情况,apache搭建的web服务器可能同时服务多个网站(bbs.xxx.com,www.xxx.com,news.xxx.com),这时apache就得为每个网站分配一个网站根目录,并分别对每个网站做相应设置,每个网站就好像运行在单独的一台虚拟主机上,书写格式:
-------------------------------------------------
<VirtualHost 10.10.54.152:80> #基于IP的虚拟主机,监听10.10.54.152:80端口
#<VirtualHost *:80> #基于域名的虚拟主机,监听所有传入到本机上的80端口
ServerAdmin root@jian.com
DirectoryIndex index.php index.html
DocumentRoot "/var/www/news"
ServerName news.zijian.com
ErrorLog "logs/news-error_log"
CustomLog "logs/news-access_log" common
</VirtualHost>
--------------------------------------------------
二:具体配置步骤
//环境介绍
1.apache编译安装版本2.4.7
2.系统:centos6.4 IP:
eth0 10.10.54.157 www.zijian.com
eth0:0 10.10.54.151 bbs.zijian.com
eth0:1 10.10.54.152 news.zijian.com
3.实现
www和bbs网站需要用ssl加密
news网站不许要ssl加密
#在配置之前,需要先用系统提供的openssl工具制作网站证书,参考我的另一篇文章
http://my.oschina.net/zijian1315/blog/205839
4.php编译参数
./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-zlib --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --with-curl --with-curlwrappers --enable-fpm --enable-fastcgi --with-mcrypt --with-gd --with-openssl --with-mhash --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc -enable-zip --enable-soap --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache2/bin/apxs --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/usr/local/mysql/tmp/mysql.sock
//主配置文件修改
shell> vim /usr/local/apache2/conf/httpd.conf
------------------------------------------------
#为了支持ssl,确保下面两个模块开启
LoadModule ssl_module modules/mod_ssl.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
#news网站需要监听80端口
Listen 80
ServerName www.localhost.com:80
#下面两个文件一定要开启
#下面使用的是相对路径,相对于 ServerRoot "/usr/local/apache2"
Include conf/extra/httpd-vhosts.conf #完整的是/usr/local/apache2/conf/extra/httpd-vhosts.conf
Include conf/extra/httpd-ssl.conf
------------------------------------------------
//配置httpd-ssl.conf文件加密www和bbs网站
httpd-ssl.conf
---------------------------------------------------
Listen 443
#www网站https协议
<VirtualHost 10.10.54.157:443>
DocumentRoot "/var/www/html"
ServerName www.zijian.com
ServerAdmin you@example.com
ErrorLog "/usr/local/apache2/logs/www_error_log"
TransferLog "/usr/local/apache2/logs/www_access_log"
#Enable/Disable SSL for this virtual host.
SSLEngine on
SSLCertificateFile "/usr/local/nginx/conf/ssl/client.pem" #制作的网站证书路径
SSLCertificateKeyFile "/usr/local/nginx/conf/ssl/client.key" #网站key文件
</VirtualHost>
#bbs网站https协议
<VirtualHost 10.10.54.151:443>
DocumentRoot "/var/www/bbs"
ServerName bbs.zijian.com
ServerAdmin you@example.com
ErrorLog "/usr/local/apache2/logs/bbs_error_log"
TransferLog "/usr/local/apache2/logs/bbs_access_log"
SSLEngine on
SSLCertificateFile "/usr/local/nginx/conf/ssl/client.pem" #制作的网站证书路径
SSLCertificateKeyFile "/usr/local/nginx/conf/ssl/client.key" #网站key文件
</VirtualHost>
#news网站https协议
<VirtualHost 10.10.54.152:80> #https不能访问
DocumentRoot "/var/www/news"
ServerName news.zijian.com
ServerAdmin you@example.com
ErrorLog "/usr/local/apache2/logs/news_error_log"
TransferLog "/usr/local/apache2/logs/news_access_log"
</VirtualHost>
--------------------------------------------------
#这台主机在httpd.conf主配置文件中开启了80端口监听,在http-ssl.conf中开启了443端口监听,每个网站根据它可以接受的端口处理用户请求
来源:oschina
链接:https://my.oschina.net/u/1421986/blog/207625