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

XHTML 2.0 New Features Preview

<br />Before browsers can handle the next ge...

react+antd.3x implements ip input box

This article shares the specific code of react+an...

Detailed explanation of the correct way to install opencv on ubuntu

This article describes how to install opencv with...

The difference and execution method of select count() and select count(1)

Count(*) or Count(1) or Count([column]) are perha...

Detailed explanation of the command mode in Javascript practice

Table of contents definition structure Examples C...

XHTML Getting Started Tutorial: Simple Web Page Creation

Create your first web page in one minute: Let'...

Use javascript to create dynamic QQ registration page

Table of contents 1. Introduction 1. Basic layout...

Implementation of CSS3 button border animation

First look at the effect: html <a href="#...

Zen coding for editplus example code description

For example, he enters: XML/HTML Code div#page>...

Mysql cannot select non-aggregate columns

1. Introduction I recently upgraded my blog and a...

How to reset the initial value of the auto-increment column in the MySQL table

How to reset the initial value of the auto-increm...

Using JS to implement a simple calculator

Use JS to complete a simple calculator for your r...

v-html rendering component problem

Since I have parsed HTML before, I want to use Vu...