Configure Apache virtual host

Keywords: Linux Apache vim CentOS Windows

Experimental environment

  1. A CentOS 7.3 virtual machine with minimal installation

Configure the base environment

1. Install apache

yum install -y httpd

2. Establish the root directory of the virtual host

mkdir /var/wwwroot
mkdir /var/wwwroot/site1
mkdir /var/wwwroot/site2
echo -e "site1" >> /var/wwwroot/site1/index.html
echo -e "site2" >> /var/wwwroot/site2/index.html

3. Turn off the firewall of CentOS

setenforce 0
systemctl stop firewalld
systemctl disable firewalld

Configure a port based virtual host

1. Edit apache configuration file

vim /etc/httpd/conf.d/vhost.conf

2. Add the following

Listen 8081
<VirtualHost *:8081>
    DocumentRoot "/var/wwwroot/site1"
    ErrorLog "logs/site1.error.log"
    CustomLog "logs/site1.access.log" common
    <Directory "/var/wwwroot/site1">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

Listen 8082
<VirtualHost *:8082>
    DocumentRoot "/var/wwwroot/site2"
    ErrorLog "logs/site2.error.log"
    CustomLog "logs/site2.access.log" common
    <Directory "/var/wwwroot/site2">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

3. Start apache service

systemctl start httpd

4. Visit two sites on the host computer

http://192.168.204.133:8081
http://192.168.204.133:8082


Configure domain name based virtual host

1. Edit apache configuration file

vim /etc/httpd/conf.d/vhost.conf

2. Amend to read

<VirtualHost *:80>
    DocumentRoot "/var/wwwroot/site1"
    ServerName site1.test.com
    ErrorLog "logs/site1.error.log"
    CustomLog "logs/site1.access.log" common
    <Directory "/var/wwwroot/site1">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/wwwroot/site2"
    ServerName site2.test.com
    ErrorLog "logs/site2.error.log"
    CustomLog "logs/site2.access.log" common
    <Directory "/var/wwwroot/site2">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

3. Restart apache service

systemctl restart httpd

4. Modify the hosts file on Windows

Edit the C:\Windows\System32\drivers\etc\hosts file,
Add the following (modify according to the actual situation)

192.168.204.135   site1.test.com  
192.168.204.135   site2.test.com

5. Visit two sites on the host

http://site1.test.com/
http://site2.test.com/

Configure IP based virtual host

1. Add two IP addresses to the virtual machine

ifconfig ens33:1 192.168.204.135
ifconfig ens33:2 192.168.204.136

2. Re edit the apache configuration file

vim /etc/httpd/conf.d/vhost.conf

3. Modify the configuration file as follows

<VirtualHost 192.168.204.135:80>
    DocumentRoot "/var/wwwroot/site1"
    ErrorLog "logs/site1.error.log"
    CustomLog "logs/site1.access.log" common
    <Directory "/var/wwwroot/site1">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost 192.168.204.136:80>
    DocumentRoot "/var/wwwroot/site2"
    ErrorLog "logs/site2.error.log"
    CustomLog "logs/site2.access.log" common
    <Directory "/var/wwwroot/site2">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

4. Restart apache service

systemctl restart httpd

5. Visit two sites on the host

http://192.168.204.135/
http://192.168.204.136/

Copyright notice:

Copyright: from Blog Garden author varlemon If you need to reprint the original works, please indicate the source
Link to this article: https://www.cnblogs.com/connect/p/apache-vhost.html

Posted by m3mn0n on Sun, 01 Dec 2019 05:24:04 -0800