NameVirtualHost *: 80
Note 1: NameVirtualHost specifies the IP address or domain name used by the VM, but preferably the IP address. When using a domain name-based VM, NameVirtualHost is a required command. Multiple NameVirtualHost can be defined.
Note 2: All requests that comply with the NameVirtualHost or <VirtualHost> label will be processed as virtual hosts, and the master server will ignore them. If the <VirtualHost> label does not define a request, the server cannot find the corresponding virtual host and cannot process it. Therefore, each parameter defined by NameVirtualHost must have at least one <VirtualHost> matched.
Note 3: If NameVirtualHost or <VirtualHost> is set to *: 80, all requests for port 80 will be processed by the VM, and the requests will be directed to a VM based on the domain name. If there is a request from Port 80 and the requested domain name is not configured as a VM, it will point to the first VM. In this way, the master server will not be able to receive any requests from port 80. Therefore, you must configure a virtual host for the master server.
For example, we configure two virtual hosts with IP192.168.0.3.
First, edit the configuration file/etc/httpd/conf/httpd. conf.
Vi/etc/httpd/conf/httpd. conf
Then add the following code to the bottom of the file:
NameVirtualHost 192.168.0.3
<Virtualhost 192.168.0.3>
ServerAdmin admin@example.com
DocumentRoot/var/www/html/website2
ServerName www.website2.com
ErrorLog/var/www/html/website2/logs/error_log
TransferLog/var/www/html/website2/logs/access_log
</Virtualhost>
<Virtualhost 192.168.0.3>
ServerAdmin admin@website3.com
DocumentRoot/var/www/html/website3
ServerName www.website3.com
ErrorLog/var/www/html/website3/logs/error_log
TransferLog/var/www/html/website3/logs/access_log
</Virtualhost>
If you have multiple IP addresses, add the IP addresses following the code above. You only need to change 192.168.0.3 to your IP address.
To enable the server to support multiple websites, you only need to modify the Apache configuration file/etc/httpd/conf/httpd. conf for apache.
There is an example file in it. You only need to configure it.
# <VirtualHost *: 80>
# ServerAdmin webmaster@dummy-host.example.com
# DocumentRoot/www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
# </VirtualHos>
You can add at the bottom of httpd. conf by referring to my configuration:
NameVirtualHost *: 80
<VirtualHost *: 80>
ServerAdmin admin@example.com
DocumentRoot/var/www/html
ServerName www.111cn.net
# ErrorLog/var/log/httpd/phpddt/error_log
# Note: Here I set the daily production log
ErrorLog "| rotatelogs/var/log/httpd/phpddt/error_log % Y _ % m _ % d. log 86400 480"
# CustomLog/var/log/httpd/phpddt/access_log common
CustomLog "| rotatelogs/var/log/httpd/phpddt/access_log % Y _ % m _ % d. log 86400 480" common
</VirtualHost>
<VirtualHost *: 80>
ServerAdmin admin@example.com
DocumentRoot/var/www/html/blog
ServerName m.111cn.net
ErrorLog/var/log/httpd/blog/error_log
CustomLog/var/log/httpd/blog/access_log common
</VirtualHost>
As shown above, I have configured two virtual hosts: www.111cn.net m.111cn.net.
Note: service httpd restart takes effect only after apache is restarted)