Access web server based on IP address, port and domain name in RedHat8

1, Build environment

  1. Install the httpd package.
systemctl stop firewalld  //Stop firewall
setenforce 0    //Close selinux
mount /dev/sr0 /mnt    //Mount mnt to local CD
dnf repolist    //Detection after mounting the CD
vim /etc/yum.repo.d/base.repo    //Import download source from local
[Base]
name=Base
baseurl=file:///mnt/BaseOS
enable=1
gpgcheck=0
[AppStream]         //Extension library added by redhat8
name=AppStream
baseurl=file:///mnt/AppStream
gpgcheck=0
enable=1            //It is on by default and does not need to be written
  1. Install the Apache main program, provided by httpd.
dnf install httpd -y          //Red hat 7 uses yum install
systemctl start httpd         //Start service
  1. View the default http access interface.
systemctl status httpd   //First, check the httpd status


From the second line of the figure

httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message

We can use localhost Localdomain, the fully qualified domain name of the server cannot be reliably determined. The 'ServerName' directive should be set in the global to suppress this message

vim /etc/httpd/conf/httpd.conf
 find ServerName
ServerName 0.0.0.0:80
systemctl restart httpd

Client test:

curl http://(own host IP)

2, Access Web server based on IP address
1. Configure the network
Usage code:

(Find your own network card and network segment in the host settings. The following configuration is only an example)
[root@localhost ~]# nmcli connection modify eno16777736 ipv4.addresses 192.168.10.100/24 ipv4.gateway 192.168.10.1 ipv4.dns 114.114.114.114 ipv4.method  manual connection.autoconnect yes  //
[root@localhost ~]# nmcli connection modify eno16777736 +ipv4.addresses 192.168.10.200/24 ipv4.gateway 192.168.10.1 ipv4.dns 114.114.114.114 ipv4.method  manual connection.autoconnect yes
[root@localhost ~]# nmcli connection up eno16777736 / / open the network card

2. Edit the configuration file (create virtual host)

[root@localhost ~]# mkdir /www/{100,200} -pv 
mkdir: Directory created "/www/100"
mkdir: Directory created "/www/200"
[root@localhost ~]# echo this is 192.168.10.100 > /www/100/index. HTML / / give the content of the page to index.html in the main directory of the created page HTML (default)
[root@localhost ~]# echo this is 192.168.10.200 > /www/200/index.html
vim /etc/httpd/conf.d/vhosts.conf   //Edit under sub profile
 <Direotory /www>
	AllowOverride none     //overlays allowed
	Require all granted        //Set access directory permissions
 </Directory>
 <VirtualHost 192.168.10.100:80>
	DocumentRoot /www/100       //Specify the root directory of the website accessed by the current host
	ServerName 192.168.10.100    //Specify the name of the site to visit
	ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"    //Error log, not for experiment
	CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common   //Note that the log must be marked with common after access, and it is not necessary to write for experiments

 </VirtualHost>
<VirtualHost 192.168.10.200:80>
        DocumentRoot /www/200
        ServerName 192.168.10.200
        ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
        CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>
  1. Restart service
[root@localhost ~]# systemctl restart httpd

3, Port based access to different websites
Set up a website, the documents are stored at / www/80, and the content is displayed as the port is 80;
Set up a website, the document is stored at / www/10000, and the content is displayed as the port is 10000;

vim /etc/httpd/conf.d/vhosts.conf
</VirtualHost>
<VirtualHost *:80>
        DocumentRoot /www/200
        ServerName 192.168.10.200
        ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
        CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>

LISTEN 10000    //Add listening port 10000
<VirtualHost *:10000>
        DocumentRoot /www/200
        ServerName 192.168.10.200
        ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
        CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>

4, Port based access to Web server
Domain name resolution is required. Domain name ---- > address
1. Solve how the browser queries the IP corresponding to the URL (the address of the corresponding server) through the domain name

1. Browser cache: the browser will cache DNS records at a certain frequency.
2. Operating system cache: if the required DNS record cannot be found in the browser cache, go to the hosts file in the operating system. Hosts is a system file without an extension. Its function is to establish an associated "database" between some common web address domain names and their corresponding IP addresses. When the user enters a web address to log in in in the browser, the system will first automatically find the corresponding IP address from the hosts file. Once it is found, the system will immediately open the corresponding web page. If it is not found, Then the system will submit the web address to the DNS domain name resolution server for IP address resolution.
hosts file path under windows:
C:\Windows\System32\drivers\etc\hostsLinux hosts file path: / etc/hosts
hosts file path under Linux:
/etc/hosts

3. Route cache: the router also has DNS cache.
4. DNS server of ISP: ISP is the Internet Service provider
ISP has a special DNS server to deal with DNS query requests.
5. Root server: if the DNS server of the ISP cannot be found, it will send a request to the root server for recursive query (the DNS server first asks the IP address of the root domain name server. com domain name server, then asks the. com domain name server, and so on).
The second is used here

  1. requirement
    1. Create a new website with the domain name of www.haha COM, and through www1 haha. COM, the documents are stored in the directory of / www/haha, and the content of the web page is this is www.haha com.
    2. Create a new website with the domain name of www.xixi COM, the documents are stored at / www/xixi

The configuration method is as follows:

[root@localhost ~]# cat /etc/httpd/conf.d/vhosts.conf 
//Here are all configurations of IP resolution, port resolution and domain name resolution
<Directory /www>
        AllowOverride none 
        Require all granted 
</Directory>
<VirtualHost 192.168.10.100:10000>
        DocumentRoot /www/100
        ServerName 192.168.10.100
        ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
        CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>
LISTEN 10000
<VirtualHost 192.168.10.200:10000>
        DocumentRoot /www/200
        ServerName 192.168.10.200
        ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
        CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>
<VirtualHost 192.168.10.200:80>
        DocumentRoot /www/haha
        ServerName www.haha.com
        ServerAlias www1.haha.com
        ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
        CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>
<VirtualHost 192.168.10.100:80>
        DocumentRoot /www/xixi
        ServerName www.xixi.com
        ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
        CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>

Vim /etc/hosts    //Domain name corresponding to custom IP
192.168.10.100  www.xixi.com
192.168.10.200 www.haha.com

Tags: Linux

Posted by silentmajority3 on Mon, 09 May 2022 11:56:12 +0300