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

What to do if you forget the initial password of MySQL on MAC

The solution to forgetting the initial password o...

How to enable Flash in Windows Server 2016

I recently deployed and tested VMware Horizon, an...

Implementation of LNMP for separate deployment of Docker containers

1. Environmental Preparation The IP address of ea...

Mysql database design three paradigm examples analysis

Three Paradigms 1NF: Fields are inseparable; 2NF:...

How to use localStorage in JavaScript

If you are a developer looking to get into the wo...

MySQL index for beginners

Preface Since the most important data structure i...

How to add rounded borders to div elements

As shown below: CSS CodeCopy content to clipboard...

Detailed explanation of the use of Linux time command

1. Command Introduction time is used to count the...

The most basic code for web pages

◆Add to favorites illustrate Click to add your we...

Detailed explanation of the process of installing msf on Linux system

Or write down the installation process yourself! ...

In-depth explanation of Vue multi-select list component

A Multi-Select is a UI element that lists all opt...

Detailed explanation of zabbix executing scripts or instructions on remote hosts

Scenario Requirements 1. We can use the script fu...

Tutorial on installing mysql5.7.23 on Ubuntu 18.04

This article shares with you the specific method ...