apache virtual host resolves that different domain names, ports, and IP s can be used on one host to improve different services
1. Domain name-based access to virtual hosts
Use an unused domain name for each virtual host, but its corresponding IP address is the same.
Domain name is different IP same port.
Domain name-based hosts are the most common type of virtual web hosts.
1. Provide domain name resolution for virtual hosts
Method 1: Deployment DNS Domain Name Resolution Server to provide domain name resolution Method 2:/etc/hosts Temporarily configure domain name and IP Mapping relationship of addresses echo "192.168.100.22 www.benet.com" >> /etc/hosts echo "192.168.100.22 www.ddyyff.com" >> /etc/hosts
2. Prepare Web Page Documentation for Virtual Host
[root@localhost ~]# mkdir -p /var/www/html/benet [root@localhost ~]# mkdir -p /var/www/html/ddyyff [root@localhost ~]# echo "<h1>www.benet.com</h1>" > /var/www/html/benet/index.html [root@localhost ~]# echo "<h1>www.ddyyff.com</h1>" > /var/www/html/ddyyff/index.html
3. Add Virtual Host Configuration
Virtual Host Profile Path for Source Compilation Installation
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf Find in subprofile vhosts Virtual Host Profile
Template file: <VirtualHost *:80> #Set Virtual Site Zone ServerAdmin webmaster@dummy-host.example.com #Set Administrator Mailbox, this line can be commented out DocumentRoot "/usr/local/httpd/docs/dummy-host.example.com" #Set Site Root Directory ServerName dummy-host.example.com #Set up the full domain name of the Web site (hostname + domain name) ServerAlias www.dummy-host.example.com ErrorLog "logs/dummy-host.example.com-error_log" #Set the path to the error log file CustomLog "logs/dummy-host.example.com-access_log" common #Set path to access log file </VirtualHost> #End label PS: combined Represents a combined log common Represents a generic log
Options directive common options
1,None : Indicates that no service features are enabled 2,FollowSymLinks : The server allows symbolic connections (soft links) in this directory 3,Indexes: If the web address entered corresponds to a file directory on the server,Not in this directory Apache In the configuration file DirectoryIndex Instruction specifies the file(for example:DirectoryIndex index.html index.php) ,List all files in that directory 4,Multiviews: If the path requested by the client may correspond to multiple types of files,Then the server will automatically select according to the client's request A file that best matches the client's requirements. for example,At the server site file There is a name under the folder hello.jpg and hello.html Two files,User input at this time http://localhost/file/hello, if there is no Hello subdirectory under the file folder, then the server will try to find hello in the file directory. The file then returns the Hello that best matches the user's request. Jpg or hello.html 5,All: Representation Division Multiviews All features except. This is also Options Default settings for instructions
The Allowoverride directive explains:
.htaccessy (Distributed Implicit Configuration File): Provides a way to change the configuration for each directory by placing a file containing specific instructions in a specific directory that act on this directory and all its subdirectories. When Allooverride is set to None, the corresponding configuration directory is located. The htaccess file is unreadable and will not take effect. When Allooverride is set to All, it is read every time a request is made to access a file in the appropriate directory. The configuration of the htaccess file means that the original Apache directive will be executed. Instruction override in htaccess file.
Avoid using it whenever possible for performance and security reasons. Htaccess file, anything you want to put in. Configurations in the htaccess file can be placed in the main configuration file (httpd.conf) section and are efficient. Therefore, the Allowoverride property is generally configured as None
Virtual Host Profile Path for RPM or YUM Installation
#vim /etc/httpd/conf.d/vhosts.conf
4. Loading a virtual configuration file
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf
5. Restart the httpd service
Result
2. Accessing virtual hosts based on IP addresses
1. Add Virtual Network Card
ifconfig ens33:0 192.168.100.23 netmask 255.255.255.0
2. Modify the configuration of the httpd virtual host
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
3. Modify the main profile
vim /usr/local/httpd/conf/httpd.conf
Result
3. Accessing virtual hosts based on port numbers
1. Modify the virtual host configuration information in the subconfiguration file
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
2. Modify the main profile
vim /etc/httpd/conf/httpd.conf
3. Restart httpd
Result
summary
When creating a virtual host, you need to modify your domain name translation, httpd, and so on, based on your domain name. Conf, http-vhosts. Con accessing virtual hosts over IP and port only requires modification of the configuration file: httpd.conf and http-vhosts.conf. httpd.conf only needs to modify the listening address and port. httpd-vhosts.conf needs to modify the mapping of IP and domain name.
Note: Introduce a virtual host configuration file in the main configuration file for httpd identification