Apache基於域名、端口、IP的虛擬主機配置(Centos 6.5)

虛擬主機:部署多個站點,每一個站點,但願用不一樣的域名和站點目錄,或者是不一樣的端口,不一樣的ip,須要虛擬主機功能。一句話,一個http服務要配置多個站點,就須要虛擬主機。html

虛擬主機分類:基於域名、基於端口、基於ip;所謂的基於**,就是靠**來區分不一樣的站點,支持各類混合,N多個虛擬主機。apache

基於域名的虛擬主機配置以下:vim

建立環境:windows

站點目錄 域名
/var/html/blog blog.bqh123.com
/var/html/bbs bbs.bqh123.com
[root@bqh-119 extra]# mkdir /var/html/{blog,bbs} -p
[root@bqh-119 extra]# touch /var/html/{blog,bbs}/index.html
[root@bqh-119 extra]# tree /var/html/
/var/html/
├── bbs
│   └── index.html
└── blog
    └── index.html

2 directories, 2 files
[root@bqh-119 extra]# for name in blog bbs;do echo "http://$name.bqh123.com" >/var/html/$name/index.html;done
[root@bqh-119 extra]# for name in blog bbs;do  cat /var/html/$name/index.html;done
http://blog.bqh123.com
http://bbs.bqh123.com

配置虛擬主機配置文件:httpd-vhosts.conf瀏覽器

[root@bqh-119 extra]# vim httpd-vhosts.conf

  1 #
  2 # Virtual Hosts
  3 #
  4 # If you want to maintain multiple domains/hostnames on your
  5 # machine you can setup VirtualHost containers for them. Most configurations
  6 # use only name-based virtual hosts so the server doesn't need to worry about
  7 # IP addresses. This is indicated by the asterisks in the directives below.
  8 #
  9 # Please see the documentation at 
 10 # <URL:http://httpd.apache.org/docs/2.2/vhosts/>
 11 # for further details before you try to setup virtual hosts.
 12 #
 13 # You may use the command line option '-S' to verify your virtual host
 14 # configuration.
 15 
 16 #
 17 # Use name-based virtual hosting.
 18 #
 19 NameVirtualHost *:80
 20 
 21 #
 22 # VirtualHost example:
 23 # Almost any Apache directive may go into a VirtualHost container.
 24 # The first VirtualHost section is used for all requests that do not
 25 # match a ServerName or ServerAlias in any <VirtualHost> block.
 26 #
 27 <VirtualHost *:80>
 28     ServerAdmin 1147076062@qq.com
 29     DocumentRoot "/var/html/blog"
 30     ServerName blog.bqh123.com
 31     ServerAlias bg.bqh123.com
 32     ErrorLog "logs/blog-error_log"
 33     CustomLog "logs/blog-access_log" common
 34 </VirtualHost>
 35 
 36 <VirtualHost *:80>
 37     ServerAdmin 1147076062@qq.com
 38     DocumentRoot "/var/html/bbs"
 39     ServerName bbs.bqh123.com
 40     ServerAlias bs.bqh123.com
 41     ErrorLog "logs/bbs-error_log"
 42     CustomLog "logs/bbs-access_log" common
 43 /VirtualHost>

 在主配置文件(httpd.conf)裏激活生效:dom

  • Include conf/extra/httpd-vhosts.conf
  • Include conf/extra/httpd-mpm.conf

檢測配置文件語法錯誤並刷新配置:tcp

[root@bqh-119 extra]# ../../bin/apachectl -t
httpd: apr_sockaddr_info_get() failed for bqh-119
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
Syntax OK
[root@bqh-119 extra]# ../../bin/apachectl graceful
httpd: apr_sockaddr_info_get() failed for bqh-119
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

配置一下hosts解析:ide

[root@bqh-119 extra]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.0.119 blog.bqh123.com bbs.bqh123.com

windows系統,在「C:\Windows\System32\drivers\etc」下的hosts中配置一下域名解析:測試

----------------------------------------------------------------------------------spa

用cur或客戶端瀏覽器測試一下:

解決方法:

在主配置文件(httpd.conf)追加一下內容:

 17 <Directory "/var/html">
 18     Options  FollowSymLinks
 19     AllowOverride None
 20     Order allow,deny
 21     Allow from all
 22 </Directory>

檢測配置文件語法錯誤,刷新配置,重新啓動:

[root@bqh-119 conf]# vim httpd.conf
[root@bqh-119 conf]# ../bin/apachectl -t
httpd: apr_sockaddr_info_get() failed for bqh-119
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
Syntax OK
[root@bqh-119 conf]# ../bin/apachectl graceful
httpd: apr_sockaddr_info_get() failed for bqh-119
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

用cur或客戶端瀏覽器測試一下:

---------------------------------------------------------------------------------

ok,Apache基於域名的虛擬主機配置及測試完成。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

基於端口的虛擬主機配置以下:

①在主配置文件httpd.conf裏配置監聽新增端口:

②在虛擬機配置文件httpd-vhosts.conf修改以下:

③檢測配置文件語法錯誤,刷新配置,重新啓動:

[root@bqh-119 conf]# ../bin/apachectl -t
httpd: apr_sockaddr_info_get() failed for bqh-119
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
Syntax OK
[root@bqh-119 conf]# ../bin/apachectl graceful
httpd: apr_sockaddr_info_get() failed for bqh-119
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[root@bqh-119 conf]# netstat -lntup|egrep "80|90"
tcp        0      0 :::80                       :::*                        LISTEN      1343/httpd          
tcp        0      0 :::90                       :::*                        LISTEN      1343/httpd 

④用cur或客戶端瀏覽器測試一下:

注:若是不加端口訪問,默認以ip的形式解析訪問。

ok,Apache基於端口的虛擬主機配置及測試完成。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

基於IP的虛擬主機配置以下:

①添加別名IP

②在虛擬機配置文件httpd-vhosts.conf修改以下:

③檢測配置文件語法錯誤,刷新配置,重新啓動:

④用cur或客戶端瀏覽器測試一下: 

ok,Apache基於IP的虛擬主機配置及測試完成

相關文章
相關標籤/搜索