Theory + practical operation: apache virtual web host configuration

@ [toc]
the Apache common features, web hosting

A: Virtual Web Host

  • Run multiple Web sites on the same server, which is not independent of each site occupied by a real computer

    1.1 httpd virtual host type (three kinds) support

  • Based on the type of domain name
  • IP address-based virtual hosts
  • Based on the virtual host ports
    such as:
    www.kgc.om
    www.accp.com
    IP the same, the same port

IP different, the same port

The same IP, port barrier

Here Insert Picture Description

Two: establish virtual host based on experimental domain name

2.1.1 installation package

[root@localhost ~]# yum install bind httpd -y
Package 32:bind-9.11.4-9.P2.el7.x86_64 already installed and latest version
Package httpd-2.4.6-90.el7.centos.x86_64 already installed and latest version
Nothing to do

2.1.2 Turn off the firewall service enhancements

[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# 

2.1.3 Configuration dns

Dns configuration global configuration file /etc/named.conf

[root@localhost ~]# vim /etc/named.conf 
options {
        listen-on port 53 { any; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };

Configuring dns zone configuration file /etc/named.rfc1912.zones

[root@localhost ~]# vim /etc/named.rfc1912.zones 
zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};

zone "accp.com" IN {
        type master;
        file "accp.com.zone";
        allow-update { none; };
};      

Dns modify the zone data file

[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@localhost named]# cp -p named.localhost kgc.com.zone
[root@localhost named]# vim kgc.com.zone 
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www     IN      A       192.168.247.150
~                                                   
[root@localhost named]# cp -p kgc.com.zone accp.com.zone

Modification, start the dns service

[root@localhost named]# systemctl start named

The client configured dns, to test whether the service is to take effect

Here Insert Picture Description

nslookup successfully resolved

Here Insert Picture Description

2.1.4 create a virtual host configuration file, the location in / etc / httpd / conf / extra /, meaning the sake of simplicity, the file name is set vhost.conf

[root@localhost httpd]# ls
conf  conf.d  conf.modules.d  logs  modules  run
[root@localhost httpd]# ls -l
total 0
drwxr-xr-x. 2 root root  37 Dec 12 14:45 conf
drwxr-xr-x. 2 root root  82 Dec 12 14:45 conf.d
drwxr-xr-x. 2 root root 146 Dec 12 14:45 conf.modules.d
lrwxrwxrwx. 1 root root  19 Dec 12 14:45 logs -> ../../var/log/httpd
lrwxrwxrwx. 1 root root  29 Dec 12 14:45 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx. 1 root root  10 Dec 12 14:45 run -> /run/httpd
[root@localhost httpd]# 
[root@localhost httpd]# cd conf
[root@localhost conf]# ls
httpd.conf  magic
[root@localhost conf]# mkdir extra
[root@localhost conf]# cd extra/
[root@localhost extra]# ls
[root@localhost extra]# 

/etc/httpd/conf/extra/vhost.conf file

  • Refers to all the ip addresses with over 80 ports are accessible
    DocumentRoot "is a web site directory"
    ServerName "site services domain name"
    Errorlog "specify the error log path"
    CustomLog "specify the access log path" followed common extensible tool
    to specify detailed configuration directory name, can be found in the parent directory of the web site directory
    allows all users access to all hosts behind // thus will expand more configuration properties
    
    [root@localhost extra]# vim vhost.conf
    1 <VirtualHost *:80>
    2   DocumentRoot "/var/www/html/kgc"
    3   ServerName www.kgc.com
    4   Errorlog "logs/www.kgc.com.error_log"
    5   Customlog "logs/www.kgc.comaccess_log" common
    6   <Directory "/var/www/html">
    7    Require all granted
    8   </Directory>
    9 </VirtualHost>
    10 
    11 <VirtualHost *:80>
    12   DocumentRoot "/var/www/html/accp"
    13   ServerName www.accp.com
    14   Errorlog "logs/www.accp.com.error_log"
    15   Customlog "logs/www.accp.comaccess_log" common
    16   <Directory "/var/www/html">
    17    Require all granted
    18   </Directory>
    19 </VirtualHost>
### 2.1.5 创建两个web站点的首页,首页文件index.html在/var/www/html下面的站点目录中
```bash
[root@localhost extra]# cd /var/
[root@localhost var]# ls
account  cache  db     games   kerberos  local  log   named  opt       run    target  www
adm      crash  empty  gopher  lib       lock   mail  nis    preserve  spool  tmp     yp
[root@localhost var]# cd www
[root@localhost www]# ls
cgi-bin  html
[root@localhost www]# cd html
[root@localhost html]# ls
[root@localhost html]# mkdir kgc accp
[root@localhost html]# ls
accp  kgc
[root@localhost html]# echo "this is accp web" > accp/index.html
[root@localhost html]# echo "this is kgc web" > kgc/index.html
[root@localhost html]# tree accp kgc
accp
└── index.html
kgc
└── index.html

0 directories, 2 files
[root@localhost html]# 

2.1.7 focus on: the need to route extra added to the master configuration file, when you can start to identify

[root@localhost html]# vim /etc/httpd/conf/httpd.conf 
354 Include conf/extra/vhost.conf

2.1.8 open the service to view the service port

[root@localhost html]# systemctl start httpd
[root@localhost html]# netstat -natp | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      79262/httpd         

2.1.9 verify the client

Here Insert Picture Description
Here Insert Picture Description

Three: Construction of Web Hosting ---- based on experimental port

More experimental basis, the port does not make sense virtual host configuration

3.1.1 modify virtual web host's profile, increase the parameter a 8080 port, in order to distinguish the site, you need to modify the file name for the site to distinguish, not covered

[root@localhost html]# vim /etc/httpd/conf/extra/vhost.conf 
//复制修改增加
 11 <VirtualHost *:8080>
 12   DocumentRoot "/var/www/html/kgc02"
 13   ServerName www.kgc02.com
 14   Errorlog "logs/www.kgc02.com.error_log"
 15   Customlog "logs/www.kgc02.comaccess_log" common
 16   <Directory "/var/www/html">
 17    Require all granted
 18   </Directory>
 19 </VirtualHost>
[root@localhost html]# ls
accp  kgc
[root@localhost html]# cp -p kgc kgc02
cp: omitting directory ‘kgc’
[root@localhost html]# mkdir kgc02
[root@localhost html]# echo "this is web kgc02" >kgc02/index.html
[root@localhost html]# 

3.1.2 adds port, you also increase the listen address, listen address changes in the main configuration file / etc / httpd / conf / httpd / conf in

[root@localhost html]# vim /etc/httpd/conf/httpd.conf
 41 Listen 192.168.247.150:80
 42 Listen 192.168.247.150:8080
 43 #Listen 80

3.1.3 restart the service, verify that the interface is open

[root@localhost html]# systemctl restart httpd
[root@localhost html]# netstat -napt | grep httpd
tcp        0      0 192.168.247.150:8080    0.0.0.0:*               LISTEN      91814/httpd         
tcp        0      0 192.168.247.150:80      0.0.0.0:*               LISTEN      91814/httpd   

3.1.4 Check verification, the same IP address, the port is not the same

Here Insert Picture Description

Four: build IP-based virtual hosting ---- different experiments

4.1.1 Adding card, create several different IP addresses of virtual web hosts need to increase the number of card

[root@localhost html]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.247.150  netmask 255.255.255.0  broadcast 192.168.247.255

ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.247.158  netmask 255.255.255.0  broadcast 192.168.247.255

Verify that the network Networking

Here Insert Picture Description

4.1.2 modify virtual web host's profile

Note: When the virtual web host servers using different IP, IP address / etc / httpd / conf / extra / vhost.conf file required concrete to a fixed IP address, then the * wildcard can not avoid confusion
[root@localhost html]# vim /etc/httpd/conf/extra/vhost.conf 
<VirtualHost 192.168.247.158:80>
  DocumentRoot "/var/www/html/accpaccp"
  ServerName www.accpaccp.com
  Errorlog "logs/www.accpaccp.com.error_log"
  Customlog "logs/www.accpaccp.comaccess_log" common
  <Directory "/var/www/html">
   Require all granted
  </Directory>
</VirtualHost>

4.1.3 Create a new virtual web site directory

[root@localhost html]# ls
accp  accp02  kgc  kgc02
[root@localhost html]# mkdir accpaccp
[root@localhost html]# echo "this is 192.168.247.158" > accpaccp/index.html

4.1.4 modify the main configuration file, increasing the listen address

[root@localhost html]# vim /etc/httpd/conf/httpd.conf 
Listen 192.168.247.158:80

Service httpd restart 4.1.5

[root@localhost html]# systemctl restart httpd
[root@localhost html]# netstat -natp | grep httpd
tcp        0      0 192.168.247.150:8080    0.0.0.0:*               LISTEN      123662/httpd        
tcp        0      0 192.168.247.158:80      0.0.0.0:*               LISTEN      123662/httpd        
tcp        0      0 192.168.247.150:80      0.0.0.0:*               LISTEN      123662/httpd   

4.1.6 Client Authentication

##### Because there is no configuration dns resolution, you need to enter the IP address to enter the site, then increase the ip address of the domain name resolution
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

4.1.7 modify configuration files dns

/etc/named.rfc1912.zones file
[root@localhost html]# vim /etc/named.rfc1912.zones
zone "accpaccp.com" IN {
        type master;
        file "accpaccp.com.zone";
        allow-update { none; };
};
/ Var / named / directory, creating a region corresponding to a data file
[root@localhost html]# cd /var/named
[root@localhost named]# ls
accp.com.zone  data  dynamic  kgc.com.zone  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@localhost named]# cp -p accp.com.zone accpaccp.com.zone
[root@localhost named]# vim accpaccp.com.zone 
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www     IN      A       192.168.247.158

4.1.8 restart the DNS service

[root@localhost named]# systemctl restart named

Here Insert Picture Description

Five: Create a hyperlink within the site

5.1 Modify Home

[root@localhost named]# vim /var/www/html/accpaccp/index.html 
<html>
<head>
 <title>hello world</title>
</head>
<body>
   <h1><a href="http://www.accp.com/index.html">hello world</a></h1>
</body>
</html>

5.2 client test, of course, also need to restart the HTTPD Service

[root@localhost named]# systemctl restart httpd

Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.51cto.com/14558445/2458217