Analysis of Apache's common virtual host configuration methods

Analysis of Apache's common virtual host configuration methods

1. Apache server installation and configuration

yum install httpd -y

systemctl start httpd &&systemctl enable httpd

systemctl stop firewalld

setenforce 0 //Set the selinux security level to premise and the reboot will fail.

Use the local Windows browser to test the virtual machine's IP address (be sure to turn off the firewall). If you see the following interface, it means that HTTP is successfully started.

2. Apache2 configuration file

After httpd is installed, the /etc/httpd directory will be automatically generated

Main configuration file: conf/httpd.conf

3. Based on IP address (the server requires multiple public IP addresses)

www.lyn1.com----192.168.100.135
www.lyn2.com----192.168.100.136

(1) Bind another IP address to the server

(2) Add a site file lyn1.conf in the /etc/httpd/conf.d directory

You can also configure it directly in /etc/httpd/conf/httpd.conf. The httpd.conf file will automatically import the files in /etc/httpd/conf.d. For convenience, we write it directly to the /etc/httpd/conf.d folder.

mkdir /mnt/lyn1

cd /etc/httpd/conf.d

vi lyn1.conf

<VirtualHost 192.168.100.135> //Local IP addressDocumentRoot /mnt/lyn1/ //Network data directoryServerName www.lyn1.com //Domain name of the website server<Directory /mnt/lyn1/ > //Website data directory permissionsAllowOverride None //Do not allow overwritingRequire all granted //Allow all access requests</Directory>
</VirtualHost>

(3) Add the webpage file index.html to the publishing directory of the corresponding website of shiyan1.com

vi /mnt/lyn1/index.html

<html>
<head>
<title>lyn1</title>
</head>
<body>
<h1>lyn1</h1>
this is the www.lyn1.com website
</body>
</html>

(4) Add a site file lyn2.conf in the /etc/httpd/conf.d directory

mkdir /mnt/lyn1

cd /etc/httpd/conf.d

vi lyn1.conf

<VirtualHost 192.168.100.136> //Another IP address of this machineDocumentRoot /mnt/lyn2/ //Network data directoryServerName www.lyn2.com //Domain name of the website server<Directory /mnt/lyn2/ > //Permissions for the website data directoryAllowOverride None //Do not allow overwritingRequire all granted //Allow all access requests</Directory>
</VirtualHost>

vi /mnt/lyn2/index.html

<html>
<head>
<title>lyn2</title>
</head>
<body>
<h1>lyn2</h1>
this is the www.lyn2.com website
</body>
</html>

(6) Restart the Apache server and verify using a browser

systemctl restart httpd

4. Configure virtual hosts based on port numbers

www.lyn1.com----192.168.100.135:80
www.lyn2.com----192.168.100.135:81

(1) Add listening port 81 to the main configuration file /etc/httpd/conf/httpd.conf

#vi /etc/httpd/conf/httpd.conf
Listen 80
Listen 81

(2) Modify the /etc/httpd/conf.d/lyn1.conf file:

<VirtualHost 192.168.100.135:80>
ServerName www.lyn1.com
DocumentRoot /var/www/html/lyn1/
<Directory /var/www/html/lyn1/ > 
AllowOverride None   
Require all granted  
</Directory>
</VirtualHost>

(3) Modify the /etc/httpd/conf.d/shiyan2.conf file:

<VirtualHost 192.168.100.135:81>
ServerName www.lyn2.com
DocumentRoot /var/www/html/lyn2/
<Directory /var/www/html/lyn2/ > 
AllowOverride None  
Require all granted 
</Directory>
</VirtualHost>

(4) Restart the Apache server and verify using a browser

systemctl restart httpd

5. Configure a hostname-based virtual machine

www.lyn1.com----192.168.100.135:80
www.lyn2.com----192.168.100.135:80

(1) Register DNS (configure DNS server and implement normal resolution). For temporary testing, you can use the /etc/hosts modification method. Here, the hosts modification method is used.

#vi /etc/hosts

192.168.100.135 www.lyn1.com

192.168.100.135 www.lyn2.com

(2) In the main configuration file /etc/httpd/conf.d/lyn1.conf

<VirtualHost *:80>
ServerName www.lyn1.com
DocumentRoot /var/www/html/lyn1/
<Directory /var/www/html/lyn1/ >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

(3) In the main configuration file /etc/httpd/conf.d/lyn2.conf

<VirtualHost *:81>
ServerName www.lyn2.com
DocumentRoot /var/www/html/lyn2/
<Directory /var/www/html/lyn2/ >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

(4) Restart the apache2 server and verify

systemctl restart httpd

[root@lyn html]# curl www.lyn1.com

[root@lyn html]# curl www.lyn2.com

To access a website under Windows, add the following two lines to the C:\Windows\System32\drivers\etc\hosts file

192.168.100.135 www.lyn1.com

192.168.100.135 www.lyn2.com

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed steps for configuring Apache virtual hosts in Ubuntu 16.04
  • Solution to localhost not being available after win10 apache configures virtual host
  • Detailed explanation of Apache configuration virtual host and reverse proxy under Ubuntu system
  • How to configure Apache virtual directories and multiple host headers
  • Virtual Host configuration and rewrite parameter description in Apache
  • Apache virtual host configuration guide
  • Apache configuration multi-port multi-virtual host LAN access example
  • How to configure Apache virtual host example summary
  • Detailed explanation of local Apache configuration of domain name-based virtual hosts

<<:  JavaScript to achieve skin effect (change background)

>>:  Vue-router example code for dynamically generating navigation menus based on backend permissions

Recommend

An article to understand operators in ECMAScript

Table of contents Unary Operators Boolean Operato...

Nexus private server construction principle and tutorial analysis

one. Why build a Nexus private server? All develo...

Tutorial on installing MySQL 8.0.11 under Linux

1. Go to the official website to download the ins...

Detailed usage of Linux text search command find

The find command is mainly used to find directori...

A brief analysis of CSS :is() and :where() coming to browsers soon

Preview versions of Safari (Technology Preview 10...

Docker /var/lib/docker/aufs/mnt directory cleaning method

The company's service uses docker, and the di...

TCP performance tuning implementation principle and process analysis

Three-way handshake phase Number of retries for c...

Alibaba Cloud Server Ubuntu Configuration Tutorial

Since Alibaba Cloud's import of custom Ubuntu...

Centos8 bridge static IP configuration method in VMware virtual machine

1. Make sure the network connection method is bri...

html+css+js to realize the function of photo preview and upload picture

Preface: When we are making web pages, we often n...

Example of implementing GitHub's third-party authorization method in Vue

Table of contents Creating OAuth Apps Get the cod...