apache virtual host configuration: secondary directory access jumps to other root folders

  • 2020-05-13 04:28:04
  • OfStack

Visit http: / / www. abc. com/course, actual access is: d: / www moodle/index PHP


<VirtualHost *:80> 
  ServerAdmin webmaster@abc.com 
  DocumentRoot "d:/www/abc" 
  ServerName www.abc.com 
  ##ServerAlias  
  Alias /course "d:/www/moodle" 
  <Directory "d:/www/moodle"> 
    Options FollowSymLinks 
    AllowOverride None  
    Order deny,allow 
    allow from all 
  </Directory> 
</VirtualHost> 

Learn 1 about the relationship between the root directory and the virtual directory:
When you open the httpd.conf file after installing apache, you will find that the default setting for DocumentRoot is the htdocs folder in the apache installation directory. Then you can enlarge the htdocs folder test, case you put in it a 1. PHP, can input http: / / 127.0.0.1/1. php test. The path following DocumentRoot is the root directory of apache. Sometimes we don't want to put their own websites in here, for example, I want to put F: / MyPHP to run, you can modify a DocumentRoot F: / MyPHP, then will the 1. php into F: / MyPHP folder, use the same http: / / 127.0.0.1/1. php can normal visit.
What is a virtual directory? As the name implies, it is only a virtual directory, and the real directory is different. Let's look at using real directory access, we are just F: / MyPHP Test 1 folder has been established, and then into the inside 2. php, this time can pass http: / / 127.0.0.1 Test / 2. php access. But sometimes you might create a folder that has a mapping to the access address you want to enter, rather than just typing the Test folder name. There are a number of reasons to do this, one of which is security, because then others will know your root folder. Specific points, you placed 2. In the Test folder php, but want to http: / / 127.0.0.1 cmj / 2. php access 2. php rather than through http: / / 127.0.0.1 Test / 2. php to access? At this point we need a virtual directory, which obviously does not have the cmj folder, but can be accessed as if it were a formal directory 1, requiring a mapping relationship. How do you do that? In httpd.conf add:

Alias /cmj "F:/MyPHP/Test/"
< Directory "F:/MyPHP/Test" >
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
< /Directory >
Alias /cmj "F:/MyPHP/Test/", which means I can access something in F:/MyPHP/Test/ by adding /cmj after the ip I used in the directory access. The latter is mainly about setting permissions, which is no longer redundant.

Simply put, open httpd.conf adds the following at the end:


<VirtualHost 127.0.0.2:80>
  DocumentRoot d:/AppServ/www2
  ServerName 127.0.0.2:80
< /VirtualHost>

<Directory "d:/AppServ/www2">
  Options Indexes FollowSymLinks Multiviews
  AllowOverride All
  Order Allow,Deny
  Allow from all
</Directory>

"d:/AppServ/www2" store directory for your site: after restarting apache2, your virtual host is configured and you can access different sites through 127.0.0.2 and 127.0.0.3.
The following is a detailed analysis:
When we installed APACHE, the default configuration of apache like 1 was only 1 website, so it was very inconvenient to switch. This problem is easily solved by configuring the native apache as a virtual server. However, most of the tutorials on the web teach you how to configure a domain-based web host using apache, and when debugging your web site with native ip (127.0.0.1 or localhost), you usually use local ip (127.0.0.1 or localhost) instead of a domain name. So you have to configure apache to be a virtual host based on the ip address.
First of all, we all know that all ip addresses starting with 127 should point to the device, not just 127.0.0.1, which you can try.
This means that the machine has enough ip addresses for you to host.
Cut the crap and get to the formal configuration work. The following is the configuration section of apache, httpd.conf (httpd.conf is located at Apache2.2 \conf) :
1. The port of Listen must be specified directly, and the ip address should not be specified. The configuration should be written as:
Listen 80
2. Don't write "NameVirtualHost" like a domain-based web host.
3. Virtual host configuration section: add httpd.conf at the end


<VirtualHost 127.0.0.2:80>
  DocumentRoot d:/AppServ/www2
  ServerName 127.0.0.2:80
< /VirtualHost>
<VirtualHost 127.0.0.3:80>
  DocumentRoot d:/AppServ/www3
  ServerName 127.0.0.3:80
</V irtualHost>...

4. Then configure each directory property accordingly. The following is a typical configuration of a directory property:


<Directory "d:/AppServ/www2">
  Options Indexes FollowSymLinks Multiviews
  AllowOverride All
  Order Allow,Deny
  Allow from all
</Directory>
<Directory "d:/AppServ/www3">
  Options Indexes FollowSymLinks Multiviews
  AllowOverride All
  Order Allow,Deny
  Allow from all
</Directory>

After restarting apache2, your virtual host will be configured and you will be able to access different sites through 127.0.0.1, 127.0.0.2, and 127.0.0.3


Related articles: