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

Apache service configuration virtual host (based on domain name, port, IP address) and simple access management

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Virtual Web host

Multiple Web sites are running on the same physical server, each of which does not occupy a real computer independently.

Virtual host types supported by httpd domain name-based virtual hosts IP address-based virtual hosts build virtual hosts based on port virtual hosts-based domain names

(1) install bind and httpd services.

(2) enter the main configuration file of the named service, and change the following two locations to "any".

[root@localhost ~] # vim / etc/named.conf

(3) enter the region configuration file of the named service and add the region information of the two domain names.

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

(4) enter the "/ var/named/" directory, reserve the right to copy a "named.localhost" area data configuration file, name it "aaa.com.zone", and then modify it.

[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 aaa.com.zone [root@localhost named] # [root@localhost named] # vim aaa.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.52.133

(5) reserve the right to copy a "aaa.com.zone" file, named "bbb.com.zone", without modification. Then turn on the named service and turn off the firewall and enhanced security features.

[root@localhost named] # cp-p aaa.com.zone bbb.com.zone [root@localhost named] # systemctl start named [root@localhost named] # [root@localhost named] # systemctl stop firewalld.service [root@localhost named] # setenforce 0 [root@localhost named] #

(6) Open another win10 virtual machine and set the IP address of its DNS server to the IP address of the Linux system just now.

(7) use the win10 host to test whether the DNS service can be parsed successfully.

(8) enter the "/ etc/httpd/conf" directory, create a "extra/" directory, then enter the "extra/" directory, use the vim editor, create a new configuration file "vhost.conf", and enter the following in the configuration file.

[root@localhost named] # cd / etc/httpd/conf [root@localhost conf] # lshttpd.conf magic [root@localhost conf] # mkdir extra [root@localhost conf] # lsextra httpd.conf magic [root@localhost conf] # cd extra/ [root@localhost extra] # vim vhost.conf DocumentRoot "/ var/www/html/aaa/" ServerName www.aaa.com ErrorLog "logs/www.aaa.com.error_log" CustomLog "logs/www.aaa.com.access_log" common Require all Granted DocumentRoot "/ var/www/html/bbb/" ServerName www.bbb.com ErrorLog "logs/www.bbb.com.error_log" CustomLog "logs/www.bbb.com.access_log" common Require all granted

(9) enter "/ var/www/html/" to create two directories "aaa/" and "bbb/".

[root@localhost extra] # [root@localhost extra] # cd / var/www/html/ [root@localhost html] # ls [root@localhost html] # mkdir aaa bbb [root@localhost html] # lsaaa bbb [root@localhost html] #

(10) enter the "aaa/" directory and create a new site home file with the following contents:

[root@localhost html] # cd aaa [root@localhost aaa] # ls [root@localhost aaa] # vim index.htmlthis is aaa web

(11) enter the "bbb/" directory and create a new site home file with the following contents:

[root@localhost aaa] # cd.. / bbb [root@localhost bbb] # ls [root@localhost bbb] # vim index.htmlthis is bbb web

(12) go to the main configuration file of the httpd service, write our new configuration file into the main configuration file at the last line, and then start the httpd service.

[root@localhost bbb] # vim / etc/httpd/conf/httpd.conf Include conf/extra/vhost.conf [root@localhost bbb] # systemctl start httpd [root@localhost bbb] #

(13) if you use the win10 host to access the two domain names, both can be accessed successfully.

Build a virtual host-port-based

(1) on the basis of the previous experiment, enter the configuration file "vhost.conf" and add port 8080 of the "www.aaa.com" domain name.

[root@localhost bbb] # vim / etc/httpd/conf/extra/vhost.conf DocumentRoot "/ var/www/html/aaa/" ServerName www.aaa.com ErrorLog "logs/www.aaa.com.error_log" CustomLog "logs/www.aaa.com.access_log" common Require all granted DocumentRoot "var/www/html/bbb/" ServerName www.bbb.com ErrorLog "logs/www.bbb.com.error_log" CustomLog "logs/www.bbb.com. Access_log "common Require all granted DocumentRoot" / var/www/html/aaa02/ "ServerName www.aaa.com ErrorLog" logs/www.aaa02.com.error_log "CustomLog" logs/www.aaa02.com.access_log "common Require all granted

(2) enter the "/ var/www/html" directory, create a new "aaa02" directory, enter the "aaa02" directory, and create a new site homepage file, as follows:

[root@localhost bbb] # cd.. / [root@localhost html] # mkdir aaa02 [root@localhost html] # cd aaa02/ [root@localhost aaa02] # vim index.htmlthis is aaa02 web

(3) enter the httpd service master configuration file, add the listening port, and log off the listening of the IPv6 port. Restart the httpd service.

[root@localhost aaa02] # vim / etc/httpd/conf/httpd.confListen 192.168.52.133:80Listen 192.168.52.133:8080#Listen 80 [root@localhost aaa02] # systemctl restart httpd [root@localhost aaa02] #

(4) use the win10 host to access two domain names with different ports, and the access is successful.

Building a virtual host-based on IP

(1) add a network card to the Linux host and check the IP address.

(2) enter the configuration file "vhost.conf" and enter the following:

[root@localhost aaa02] # vim / etc/httpd/conf/extra/vhost.conf DocumentRoot "/ var/www/html/aaa/" ErrorLog "logs/www.aaa.com.error_log" CustomLog "logs/www.aaa.com.access_log" common Require all granted DocumentRoot "/ var/www/html/aaa02/" ErrorLog "logs/www.aaa02.com.error_log" CustomLog "logs/www.aaa02.com.access_log" common Require all granted

(3) modify the home page files of "aaa" site and "aaa02" site as follows:

[root@localhost aaa02] # cd.. / aaa [root@localhost aaa] # vim index.html this is 133 aaa web

[root@localhost aaa] # cd.. / aaa02 [root@localhost aaa02] # vim index.html this is 139 aaa02 web

(4) enter the httpd main configuration file to add and comment the port. Then restart the httpd service.

[root@localhost aaa02] # vim / etc/httpd/conf/httpd.conf Listen 192.168.52.133:80Listen 192.168.52.139:80#Listen 192.168.52.133:8080#Listen 80 [root@localhost aaa02] # systemctl restart httpd [root@localhost aaa02] #

(5) use the win10 host to visit two sites with different IP addresses, and the visit is successful. However, it can only be accessed by IP address. Generally, the domain name is used to access the website. Next, we configure the domain name to access different IP address sites.

(6) first add the domain name "ServerName" in the configuration file "vhost.conf".

[root@localhost aaa02] # vim / etc/httpd/conf/extra/vhost.conf DocumentRoot "/ var/www/html/aaa/" ServerName www.aaa.com ErrorLog "logs/www.aaa.com.error_log" CustomLog "logs/www.aaa.com.access_log" common Require all granted DocumentRoot "/ var/www/html/aaa02/" ServerName www.aaa02.com ErrorLog "logs/www.aaa02.com.error_log" CustomLog "logs/www.aaa02.com.access_log" common Require all granted

(7) enter the zone configuration file of the named service and add a region information of "aaa02".

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

(8) enter the "/ var/named/" directory and reserve the right to copy a "aaa.com.zone" file named "aaa02.com.zone", and modify it as follows:

[root@localhost aaa02] # cd / var/named/ [root@localhost named] # lsaaa.com.zone data named.ca named.localhost slavesbbb.com.zone dynamic named.empty named.loopback [root@localhost named] # cp-p aaa.com.zone aaa02.com.zone [root@localhost named] # vim aaa02.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.52.139

(9) use the win10 host again to visit two sites with different IP addresses through the domain name, and the visit is successful.

Access control

(1) check the IP address of the win10 host.

(2) add the deny win10 host access, "www.aaa.com" domain name rule in the configuration file "vhost.conf". Then restart the service

[root@localhost ~] # vim / etc/httpd/conf/extra/vhost.conf DocumentRoot "/ var/www/html/aaa/" ServerName www.aaa.com ErrorLog "logs/www.aaa.com.error_log" CustomLog "logs/www.aaa.com.access_log" common Require not ip 192.168.52.129 Require all granted DocumentRoot "/ var/www/html/aaa02/" ServerName www.aaa02.com ErrorLog "logs/www.aaa02.com .error _ log "CustomLog" logs/www.aaa02.com.access_log "common Require all granted [root@localhost ~] # systemctl restart httpd

(3) clear the history of the win10 host browser, and then access the two domain names. At this time, the "www.aaa.com" domain name can no longer access the home page, and you can only see the default page of Apache.

(4) use the "htpasswd" command to create a user authentication database. The "- c" option is used to create the file "passwd". If the file already exists, you do not need to add it.

[root@localhost ~] # cd / etc/httpd/conf [root@localhost conf] # lsextra httpd.conf magic [root@localhost conf] # [root@localhost conf] # htpasswd-c / etc/httpd/conf/passwd test01New password: Re-type new password: Adding password for user test01 [root@localhost conf] # htpasswd / etc/httpd/conf/passwd test02New password: Re-type new password: Adding password for user test02 [root@localhost conf] # cat passwd test01:$apr1 $72w08g5z$26fEl6Yqym/nPi08lhrYj/test02:$apr1 $1sZRVmZ/$Qs2BrdK/SJoZwRe1sIXUQ/ [root@localhost conf] #

(5) in the configuration file "vhost.conf", add authentication access rules to the "www.aaa02.com" domain name, and then restart the service.

[root@localhost conf] # vim extra/vhost.conf DocumentRoot "/ var/www/html/aaa/" ServerName www.aaa.com ErrorLog "logs/www.aaa.com.error_log" CustomLog "logs/www.aaa.com.access_log" common Require not ip 192.168.52.129 Require all granted DocumentRoot "/ var/www/html/aaa02/" ServerName www.aaa02.com ErrorLog "logs/www.aaa02.com.error_log" CustomLog "logs/www.aaa02.com.access_log" common AuthName "DocumentRoot" AuthType Basic AuthUserFile / etc/httpd/conf/passwd Require valid-user [root@localhost conf] # systemctl restart httpd [root@localhost conf] #

(6) clear the browser cache of the win10 host, and then access the "www.aaa02.com" domain name again, resulting in pop-up authentication. The access was successful after entering the user and password.

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