How can I use Apache to configure multiple virtual hosts locally? and using different "domain names" to access different local sites?
In general, we use localhost to access the server on this computer, and in our c:/windows/system32/drivers/etc/hosts file, we have one of the default:
When we type in the browser address bar:
, you will see many folders in the root directory of the server (if your root directory is not a single site), like this:
Index of /
* BAK/
* B_Discuz/
* B_Supesite/
* B_UCenter/
* B_Uchome/
* B_Uchome_Bak/
* B_XSpace/
* DiscuZ_X1/
* Javascript/
* NewUch/
* PHP/
* UchUtf8/
* Zend Framework_1.10.8/
* Zendphpweb20/
* mycms/
* phpMyAdmin/
* userAdminManager/
|
Now I want to access sites in my root directory through a custom "domain name," such as:
1, the root directory has a phpmyadmin/folder (the penultimate), it is a web-version of the database management system (certainly not unfamiliar with it!). ), I would like to access a simple:
To enter the system (without entering localhost, then clicking phpmyadmin/);
2, the root directory has a zendphpweb20/folder (the third last), it is just a local application site, I want to access by:
To enter this local site;
What do you want to do now? Come with me! See below:
1> Modify the system's Hosts file:
Open the previously mentioned system's hosts file, to add two "fake" DNS entries to our computer, adding code:
127.0.0.2 dbmanager 127.0.0.3 phpweb20 |
In this way, our browser resolves the dbmanager hostname to 127.0.0.2 and resolves the phpweb20 hostname to 127.0.0.3.
2> Modifying the Apache configuration file:
In my native Apache, the master profile is in:
D:/usr/local/apache2/conf/httpd.conf |
Said to modify the Apache configuration file, actually just added some content, because do not want to modify the Apache master configuration file, so we'd better write their own virtual host configuration sub-files at each site, and then use the include directive to include it in the Apache master configuration file.
In fact, at the bottom of the Apache main configuration file, there is a virtual host configuration example, but all are commented out:
# 基于名称的虚拟主机示例: #<
VirtualHost
127.0.0.2:80>
# ServerAdmin [email protected]
# DocumentRoot D:\usr\webroot\webhosting1
# ServerName 127.0.0.2
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</
VirtualHost
>
#<
VirtualHost
127.0.0.3:80>
# ServerAdmin [email protected]
# DocumentRoot D:\usr\webroot\webhosting2
# ServerName 127.0.0.3
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</
VirtualHost
>
#<
VirtualHost
127.0.0.4:80>
# ServerAdmin [email protected]
# DocumentRoot D:\usr\webroot\webhosting3
# ServerName 127.0.0.4
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</
VirtualHost
>
|
Add code at the bottom of the configuration file:
# 包含 dbmanager 虚拟主机配置文件 Include D:/usr/webroot/phpMyAdmin/httpd.conf # 包含 phpweb20 虚拟主机配置文件 Include D:/usr/webroot/Zendphpweb20/httpd.conf |
(The root directory of my localhost is d:/usr/webroot/)
3> Create sub-profiles for each virtual host:
Create a Dbmanager profile: d:/usr/webroot/phpmyadmin/httpd.conf
#dbmanager 虚拟主机设置
<
VirtualHost
127.0.0.2:80>
ServerName dbmanager
DocumentRoot D:/usr/webroot/phpMyAdmin
<
Directory
D:/usr/webroot/phpMyAdmin>
AllowOverride All
Options All
</
Directory
>
</
VirtualHost
>
|
Create a PHPWEB20 profile: d:/usr/webroot/zendphpweb20/httpd.conf
#phpweb20 虚拟主机设置
<
VirtualHost
127.0.0.3:80>
ServerName phpweb20
DocumentRoot D:/usr/webroot/Zendphpweb20/htdocs
<
Directory
D:/usr/webroot/Zendphpweb20/htdocs>
AllowOverride All
Options All
</
Directory
>
#修改 PHP 配置(可选)
php_value include_path .;D:/usr/webroot/Zendphpweb20/include;D:/usr/local/php5/PEAR
php_value magic_quotes_gpc off
php_value register_globals off
</
VirtualHost
>
|
The configuration of each virtual host depends on the application site needs, documentroot for its root directory (Php_value is to modify the PHP operating parameters of this site, according to their own application needs to write, so you do not have to modify the PHP master configuration file).
4> Restart Apache Service:
That's not much to say.
Complete
Configure multiple virtual machines locally based on Apache