Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of virtual web host configuration of apache

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Today, the editor will bring you an article about apache's virtual web host configuration. The editor thinks it's pretty good, so I'll share it for you as a reference. Let's follow the editor and have a look.

@ [toc]

Common function of apache, virtual host

One: virtual Web hosts run multiple Web sites in the same server, each of which does not independently occupy a real computer 1.1 virtual host types supported by httpd (three) domain name-based virtual hosts IP address-based virtual hosts port-based virtual hosts

For example:

Www.kgc.om

Www.accp.com

Same IP, same port

Different IP, same port

The IP is the same, the port is not working

Two: build a virtual host based on domain name experiment 2.1.1 install the package [root@localhost ~] # yum install bind httpd- yPackage 32:bind-9.11.4-9.P2.el7.x86_64 already installed and latest versionPackage httpd-2.4.6-90.el7.centos.x86_64 already installed and latest versionNothing to do2.1.2 turn off the firewall enhancement service [root@localhost ~] # setenforce 0 [root@localhost ~] # systemctl stop firewalld.service [root@localhost ~] # 2.1.3 configure dns configuration dns 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;}; configure dns zone profile / 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;}; modify dns's region data file [root@localhost ~] # cd / var/named/ [root@localhost named] # lsdata 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.1www IN A 192.168.247.150 ~ [root@localhost named] # cp-p kgc.com.zone accp.com.zone is modified, start the dns service [root@localhost named] # systemctl start named and configure the dns for the client to test whether the service is effective.

Nslookup parsed successfully

2.1.4 create a virtual host configuration file located in / etc/httpd/conf/extra/,. For simplicity, the file name is set to vhost.conf [root@localhost httpd] # lsconf conf.d conf.modules.d logs modules run [root@localhost httpd] # ls-ltotal 0drwxr-xr-x. 2 root root 37 Dec 12 14:45 confdrwxr-xr-x. 2 root root 82 Dec 12 14:45 conf.ddrwxr-xr-x. 2 root root 146 Dec 12 14:45 conf.modules.dlrwxrwxrwx. 1 root root 19 Dec 12 14:45 logs->.. /.. / var/log/httpdlrwxrwxrwx. 1 root root 29 Dec 12 14:45 modules->.. /.. / usr/lib64/httpd/moduleslrwxrwxrwx. 1 root root 10 Dec 12 14:45 run-> / run/httpd [root@localhost httpd] # [root@localhost httpd] # cd conf [root@localhost conf] # lshttpd.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

It means that all ip addresses can be accessed over port 80.

DocumentRoot is the web site directory

ServerName "site Service Domain name"

Errorlog "specify error log path"

Customlog "specify access log path" followed by common extension tool

Specify the directory name of the detailed configuration, which can be found to be the parent directory of the web site directory

Allow all access to all user hosts / / this extends more configuration properties [root@localhost extra] # vim vhost.conf1 2 DocumentRoot "/ var/www/html/kgc" 3 ServerName www.kgc.com4 Errorlog "logs/www.kgc.com.error_log" 5 Customlog "logs/www.kgc.comaccess_log" common6 7 Require all granted8 9 10 11 12 DocumentRoot "/ var/www/html/accp" 13 ServerName www.accp.com14 Errorlog "logs/www.accp.com.error_log" 15 Customlog "logs/www.accp.comaccess_log" common16 17 Require all granted18 19 # 2.1.5 create the home page of two web sites The homepage file index.html is in the site directory under / var/www/html ```bash [root @ localhost extra] # cd / var/ [root@localhost var] # lsaccount cache db games kerberos local log named opt run target wwwadm crash empty gopher lib lock mail nis preserve spool tmp yp [root@localhost var] # cd www [root@localhost www] # lscgi-bin html [root@localhost www] # cd html [root@localhost html] # ls [root @ localhost html] # mkdir kgcaccp [root@localhost html] # lsaccp 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 kgcaccp └── index.htmlkgc └── index.html0 directories 2 files [root@localhost html] # 2.1.7 important: the path of extra needs to be added to the main configuration file You can only start the service by identifying [root@localhost html] # vim / etc/httpd/conf/httpd.conf 354 Include conf/extra/vhost.conf2.1.8 at startup, and check the service port [root@localhost html] # systemctl start httpd [root@localhost html] # netstat-natp | grep httpdtcp6 0: 80: * LISTEN 79262/httpd 2.1.9 in client verification

Three: build virtual host-based on the port-based experiment, configure the virtual host with blocked port 3.1.1 modify the configuration file of the virtual web host, add a parameter of port 8080, in order to distinguish the site, you need to modify the site file name to distinguish Will not overwrite [root@localhost html] # vim / etc/httpd/conf/extra/vhost.conf / / add 11 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 17 Require all granted 18 19 [root@localhost html] # lsaccp kgc [root@localhost html] # cp-p kgc kgc02cp: 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 added port The listening address is added, and the listener address is modified in the main configuration file / etc/httpd/conf/httpd/conf [root@localhost html] # vim / etc/httpd/conf/httpd.conf 41 Listen 192.168.247.150 etc/httpd/conf/httpd.conf 80 42 Listen 192.168.247.150 etc/httpd/conf/httpd/conf 8080 43 # Listen 803.1.3 restart the service Verify whether the API is enabled [root@localhost html] # systemctl restart httpd [root@localhost html] # netstat-napt | grep httpdtcp 0 0192.168.247.150 netstat 8080 0.0.0.0 root@localhost html * LISTEN 91814/httpd tcp 0 192.168.247.150 80 0.0.0.0 root@localhost html 3.1.4 check and verify that the IP address is the same The port is different.

Four: build a virtual host-add a network card based on different IP experiments 4.1.1 To create a virtual web host with several different IP addresses, you need to add several network cards [root@localhost html] # ifconfigens33: flags=4163 mtu 1500 inet 192.168.247.150 netmask 255.255.255.0 broadcast 192.168.247.255ens36: flags=4163 mtu 1500 inet 192.168.247.158 netmask 255.255.255.0 broadcast 192.168.247.255 verify whether the network card is connected to the network

4.1.2 modify the configuration file of a virtual web host Note: when a virtual web host uses a different IP in the server, the IP address in the / etc/httpd/conf/extra/vhost.conf file needs to be represented to a fixed IP address, and the * wildcard symbol can no longer be used. To avoid confusion [root@localhost html] # vim / etc/httpd/conf/extra/vhost.conf DocumentRoot "/ var/www/html/accpaccp" ServerName www.accpaccp.com Errorlog "logs/www.accpaccp.com.error_log" Customlog "logs/www.accpaccp.comaccess_log" common Require all granted 4.1.3 create a new virtual web site directory [root@localhost html] # lsaccp accp02 kgc kgc02 [root@localhost html] # mkdir accpaccp [root@localhost html] # Echo "this is 192.168.247.158" > accpaccp/index.html4.1.4 modifies the main configuration file Add the listening address [root@localhost html] # vim / etc/httpd/conf/httpd.conf Listen 192.168.247.158 etc/httpd/conf/httpd.conf Listen 804.1.5 restart the httpd service [root@localhost html] # systemctl restart httpd [root@localhost html] # netstat-natp | grep httpdtcp 0 0192.168.247.150 etc/httpd/conf/httpd.conf Listen 8080 0.0.0.0 LISTEN 123662/httpd tcp 0 192.168. 247.158 80 0.0.0.0 LISTEN 123662/httpd tcp 0 192.168.247.150 80 0.0.0.0 LISTEN 123662/httpd 4.1.6 client authentication

# because dns resolution is not configured, you need to enter an IP address to enter the website, and then add the domain name resolution of this ip address

4.1.7 modify dns configuration file / etc/named.rfc1912.zones file [root@localhost html] # vim / etc/named.rfc1912.zoneszone "accpaccp.com" IN {type master; file "accpaccp.com.zone"; allow-update {none;};} / var/named/ directory, create the corresponding zone data file [root@localhost html] # cd / var/named [root@localhost named] # lsaccp.com.zone data dynamic kgc.com.zone named.ca named.empty named.localhost named.loopback slaves [root@localhost named] # cp-paccp.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.1www IN A 192.168.247.1584.1.8 restart the domain name resolution service [root@localhost named] # systemctl restart named

Create a hyperlink within the site 5.1 modify the home page [root@localhost named] # vim / var/www/html/accpaccp/index.html hello world hello world5.2 client test, of course, you also need to restart the HTTPD service [root@localhost named] # systemctl restart httpd

The above are the details of apache's virtual web host configuration, and the details of use still need to be used in hands-on experiments to be understood. If you want to know more about it, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report