Article directory
a definition
Virtual hosting refers to the provision of web services for multiple separate domain names on one WWW server, resulting in different web interfaces. Each domain name has specific directories and configurations, which is equivalent to dividing one host into multiple hosts. It satisfies the need for insufficient number of hosts and wants to provide independent web services for different users.
Second system environment
parameter | value |
---|---|
host IP | 10.0.0.100 |
installed apache version | Apache/2.2.15 (Unix) |
operating system version | CentOS release 6.9 (Final) |
operating system kernel | 2.6.32-696.el6.x86_64 |
Three based on the domain name
Create multiple websites on one host, and use multiple domain names to access, where the IP corresponding to the domain name is the same.
3.1 Install the software
yum install apache service
yum install -y httpd
After the installation is complete, two dependent packages will be installed together.
3.2 Modify the main configuration file
The main configuration file obtained through yum installation /etc/httpd/conf/httpd.conf
is the source file backup before modifying it.
[root@test ~]# cd /etc/httpd/conf
[root@test conf]# cp httpd.conf httpd.conf.bak
We httpd.conf
make , which is the main configuration file of apache that finally takes effect.
ServerName
Change the following IP to the IP address bound to the network card of the host. Remove the comment symbol in front
to make it effective.NameVirtualHost
(The step of checking 四
Include 五
is also checked in and , so I won’t go into details.)
Check that Include conf.d/*.conf
there is no comment symbol, it needs to take effect.
3.3 Write a virtual host configuration file
First enter /etc/httpd/conf.d/
the directory and create vhost.conf
cd /etc/httpd/conf.d/
touch vhost.conf
The configured domain names are www.test1.com
and www.test2.com
, and the site directories are respectively /var/www/html/test1
and /var/www/html/test2
.
[root@test conf.d]# cat vhost.conf
<VirtualHost *:80>
ServerName www.test1.com
DocumentRoot "/var/www/html/test1"
ErrorLog logs/test1.err
CustomLog logs/test1.access common
</VirtualHost>
<VirtualHost *:80>
ServerName www.test2.com
DocumentRoot "/var/www/html/test2"
ErrorLog logs/test2.err
CustomLog logs/test2.access common
</VirtualHost>
After modifying the configuration file, use httpd -t
to check whether the configuration file syntax of the httpd service is correct.
3.4 Create a site directory and test files
mkdir -p /var/www/html/test{
1,2}
echo "www.test1.com" > /var/www/html/test1/index.html
echo "www.test2.com" > /var/www/html/test2/index.html
3.5 Set firewall access policy
Allow services on port 80.
Add a line with the following content.
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
3.6 Write hosts files for Linux and Windows
In Linux, write /etc/hosts
a file to generate a mapping relationship between domain name and IP.
On Windows, write C:\Windows\System32\drivers\etc\hosts
the file .
3.7 Restart the service and test
First restart the firewall and httpd service.
service iptables restart
service httpd restart
Secondly, test it on Linux first, and the page content can be displayed normally.
Then test it in the Windows browser, and it can also be displayed normally.
It can be seen that the domain name-based virtual host configuration is successful.
3.8 Error description
(1) If it is found in the log file, the server will display the following error message:
configuration error: couldn't perform authentication. AuthType not set!: /
The 500 error here needs to be checked for existence Require all granted
.
If it appears, Require all granted
just .
(2) If the following error occurs, just delete the redundant NameVirtualHost *:80
parameters in all configuration files, and only keep the part of our configuration.
[warn] NameVirtualHost *:80 has no VirtualHosts
(3) If the following error occurs, remove the comment in front of ServerName
the parameter to make it take effect.
httpd: apr_sockaddr_info_get() failed for test
Four based on IP
Configure multiple IP addresses on a server, each IP address provides a different web interface, and each IP address has specific directories and configurations.
4.1 Add network card
Since this machine has only one network card, the IP is 10.0.0.100, so you need to add a network card, the IP is 10.0.0.101
Then write the configuration file for the eth1 network card/etc/sysconfig/network-scripts/ifcfg-eth1
Change the network card name eth0 to eth1, change the IP address to the new 10.0.0.101, and modify the mac address.
To get the mac address, enter the commandip a
Finally, restart the network card service.
4.2 Install the software
yum install apache service
yum install -y httpd
After the installation is complete, two dependent packages will be installed together.
4.3 Writing the main configuration file
Uncomment ServerName to make it effective.
Other content does not need to be changed.
4.4 Write a virtual host configuration file
[root@test conf.d]# cat vhost.conf
<VirtualHost 10.0.0.100:80>
ServerName 10.0.0.100
DocumentRoot "/var/www/html/test1"
ErrorLog logs/test1.err
CustomLog logs/test1.access common
</VirtualHost>
<VirtualHost 10.0.0.101:80>
ServerName 10.0.0.101
DocumentRoot "/var/www/html/test2"
ErrorLog logs/test2.err
CustomLog logs/test2.access common
</VirtualHost>
After writing the configuration file, use httpd -t
to check the syntax format.
4.5 Create a site directory and test files
mkdir -p /var/www/html/test{
1,2}
echo "10.0.0.100" > /var/www/html/test1/index.html
echo "10.0.0.101" > /var/www/html/test2/index.html
4.6 Set Firewall Access Policy
Allow services on port 80.
Add a line with the following content.
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
4.7 Restart the service and test
First restart the firewall and httpd service.
service iptables restart
service httpd restart
Secondly, test it on Linux first, and the page content can be displayed normally.
Then in the Windows browser parameters, the page can also be displayed normally.
To sum up, the configuration of IP-based virtual host is successful.
Five port-based
Provide HTTP servers with multiple ports for extranet users.
5.1 Install the software
yum install apache service
yum install -y httpd
After the installation is complete, two dependent packages will be installed together.
5.2 Writing the main configuration file
Uncomment ServerName to make it effective.
Increase the port numbers, 81 and 82.
Other content does not need to be changed.
5.3 Write a virtual host configuration file
[root@test conf.d]# cat vhost.conf
<VirtualHost *:81>
ServerName www.test1.com
DocumentRoot "/var/www/html/test1"
ErrorLog logs/test1.err
CustomLog logs/test1.access common
</VirtualHost>
<VirtualHost *:82>
ServerName www.test2.com
DocumentRoot "/var/www/html/test2"
ErrorLog logs/test2.err
CustomLog logs/test2.access common
</VirtualHost>
After writing, use httpd -t
to detect that the configuration file is well-formed.
5.4 Create a site directory and test files
mkdir -p /var/www/html/test{
1,2}
echo "10.0.0.100:81" > /var/www/html/test1/index.html
echo "10.0.0.100:82" > /var/www/html/test2/index.html
5.5 Set firewall access policy
Allow services on port 80.
Add a line with the following content.
-A INPUT -m state --state NEW -m tcp -p tcp --dport 81 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 82 -j ACCEPT
5.6 Restart the service and test
First restart the firewall and httpd service.
service iptables restart
service httpd restart
Secondly, test it on Linux first, and the page content can be displayed normally.
Then in the Windows browser parameters, the page can also be displayed normally.
In summary, the port-based virtual host configuration is successful.
Six based on IP and port
After configuring the second network card, on the basis of 五
the configuration , only modify the configuration file of the virtual host into the following format
You can configure virtual hosts based on IP and port.
The test results are as follows: