Setting up a virtual host in the Apache web server is not exactly a PHP topic, but many PHP developers use the Apache web server to test web pages on their development machine.
There is a lot of information around on how to do this, but the first time I tried it, I found the existing information to be more confusing than helpful. Hopefully, this page will simplify the process a bit. Please note that this information pertains to setting up a virtual host in Apache on a Windows machine for use as a local testing server. Setting up a virtual host for an actual production server is beyond the scope of this article and you should refer to the official Apache documentation for that.
Configuring Apache
The first file we'll need to edit is the Apache httpd.conf
file. If you installed the Apache software using the download from the Apache web site, you should have a menu item that will open this file for editing. Click Start->Programs->Apache HTTP Server->Configure Apache Server->Edit the Apache httpd.conf Configuration File. If you don't have that start menu item, start your text editor and open the file. It will be in a sub-folder named conf of your Apache folder. For example, mine is here:
C:\Program Files\Apache Group\Apache\conf\httpd.conf
Notes for Apache Server Versions Since 2.2
Configuration
Note that Apache changed the preferred method for configuring the Apache server with the release of Apache 2.2. For versions beginning with 2.2, the peferred configuration is more modular. Setting up a virtual host as described here will still work with the newer versions, but to follow the modular approach, the editing of httpd.conf is only to uncomment (remove the # from the beginning of the following line:
#Include conf/extra/httpd-vhosts.conf
Everything else is entered in the file httpd-vhosts.conf
, which will be located in the extra
folder below the below the folder containing httpd.conf
. As mentioned, the method described here will still work.
Security
Version 2.2 also changed some of the default security configuration parameters. To set things up the way you'll need them, you'll need to add the following block to either your httpd.conf file, just above the virtual hosts, or to your httpd-vhosts.conf file:
<Directory "C:\ My Sites ">
Order Deny,Allow
Allow from all
</Directory>
The above assumes you're using the directory structure described below. Adjust that as necessary to reflect your actual directory.
Now, for this example, we'll assume that you have your web sites located in a folder on your C drive called My Sites. Each web site has a sub-folder of its own under that folder, like this:
C:\My Sites\Site1
C:\My Sites\Site2
Say also, for this example, that the domains for the two sites are site1.com and site2.com. We're going to set up virtual hosts for those two sites using the domain names site1.local and site2.local. That way, you'll be able to tell at a glance whether you're looking at the live site, or your testing site.
In reality, you can call the domains anything you want. You could just as easily name them microsoft.monkeybutt and ibm.greentambourine. I choose to use the convention of using the same domain name along with the .local TLD to simplify and minimize the typing needed to switch between the live site and the testing site. The only important point, and it's really important, is that you NEVER use an actual, real, live domain name. If you used, for example, site1.com for the local virtual host, you would never be able to actually reach the live site. All requests for the live site would be re-directed to your local virtual host.
Go to the very bottom of your httpd.conf file in your text editor. You should see an example of a virtual host there. Each line of that example will begin with an octothorpe (#). The octothorpe character marks the line as a comment, so the example is not executed. Add the following lines below that example:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
DocumentRoot "C:\My Sites\Site1"
ServerName site1.local
</VirtualHost>
<VirtualHost 127.0.0.1>
DocumentRoot "C:\My Sites\Site2"
ServerName site2.local
</VirtualHost>
That's all you need to do! Save and close the file. That will tell the Apache server everything it needs to know in order for it to serve the pages using the domain names site1.local and site2.local. One note is that, in the above example, we have a space in the path. Because of that, we put quotation marks around the document root directory. If the path does not have any spaces in it, do not quote the path. If the directory used for your sites were, for example MySites instead of My Sites, the document root line would look like this instead:
DocumentRoot C:\MySites\Site1
Resolving the DNS issue
Obviously, if you typed http://site1.local in your browser, it would not be found by your Internet provider's DNS server. We're next going to edit another file to work around that. The second file you need to edit is called hosts, with no file extension. It is a Windows system file and it will enable you to enter specific addresses for specific domains instead of using a DNS lookup. The normal location for this file is:
C:\WINNT\system32\drivers\etc\hosts
or
C:\Windows\system32\drivers\etc\hosts
If you don't find it there, do a search in your windows directory for the word hosts in the file name. The file you want is called hosts, with no file extension. The correct file will begin with the following lines:
# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
Once again, in this file, the octothorpe character is a comment marker. Lines beginning with it are comments. In all likelihood, there will be nothing there, except for comments. If there are any other non-commented entries, leave them alone. Just go to the bottom of the file, below all the comments and any existing entries and add the following two lines:
127.0.0.1 site1.local
127.0.0.1 site2.local
That's all you need to do there. Save and close the hosts file.
You're almost done! The only remaining thing you need to do is to re-start the Apache server. You need to do this because Apache only reads the configuration file when it first starts up. Click Start->Programs->Apache HTTP Server->Control Apache Server->Restart. If you don't have that menu item, open a command prompt and change to the Apache directory, and type the following command and press the Enter key:
apache -w -n "Apache" -k restart
You should see a message like this:
The Apache service is restarting.
The Apache service has restarted.
That's it! You're done! Close the command window and start your web browser. In the browser's address bar, type http://site1.local and hit the Enter key. You should now see your local copy of your site1.
Okay, now I'll mention one very small, but possibly important, caveat. When you create the virtual hosts like this, the default http://localhost will no longer work. In many cases, that is unimportant. However, if you're using something like phpMyAdmin, you'll still need it. The solution to that is to create one additional virtual host called "localhost" that points to the original Apache htdocs folder. It might look something like this:
<VirtualHost 127.0.0.1>
DocumentRoot C:\Apache\htdocs
ServerName localhost
</VirtualHost>
Don't forget to include that additional virtual host when you edit the Windows hosts file.
Note that there are other optional settings you can use to configure the virtual host. The above uses only two lines and that's all that's really necessary. You can read about other options in the Apache documentation. Note that this link is to the Apache web site and it will open a new browser window.
I hope you found this more helpful than confusing. Good luck!
分享到:
相关推荐
基于虚拟机的网络搭建视频,基于虚拟机的网络搭建视频,基于虚拟机的网络搭建视频基于虚拟机的网络搭建视频基于虚拟机的网络搭建视频基于虚拟机的网络搭建视频基于虚拟机的网络搭建视频基于虚拟机的网络搭建视频基于...
CIMCO Setting up a 4th axis machine.mkv
《设置LAMP:让Linux、Apache、MySQL和PHP协同工作》是Eric Rosebrock与Eric Filson合作编写的,由SYBEX Inc.在2004年出版的专业技术书籍。这本书深入探讨了如何在Linux操作系统上搭建并配置Apache网络服务器、MySQL...
SIMATIC NET Industrial Ethernet Security, Setting up security in STEP 7 Professional[手册]pdf,
参考提供的"Setting Up OpenGL in an MFC Control.doc"文档和"OPENGL_VCPP.pdf"资料,可以更深入地学习和理解这个过程。记住,实践是掌握OpenGL与MFC结合的关键,多做尝试,不断调试,你将能够创建出令人印象深刻的...
My goal in this tutorial is to set up, step-by-step, how to initialize an OpenGL rendering context inside of an MFC control such as a picture control, as well as basic functionality to draw based on a...
01 2D Animation Tutorial - Setting Up Flash.srt
Furthermore, the book walks you through setting up and using Maven with popular Java Integrated Development Environments (IDEs) so you can exploit Maven features to build standard Java applications....
Setting up ARIS WebMethods Integration 文档提供了详细的指导,帮助用户设置 ARIS WebMethods 集成,并解决可能遇到的问题。 知识点: * ARIS 是一个业务流程管理和分析工具 * WebMethods 是一个集成平台 * ...
August 26, 2009 Virtual Access Point (Virtual AP) – Share your Internet through Wi-Fi....This is also an ideal solution for setting up a temporary AP in a hotel room, meeting room, at home or the like.
Pro Apache Phoenix covers the nuances of setting up a distributed HBase cluster with Phoenix libraries, running performance benchmarks, configuring parameters for production scenarios, and viewing the...
apache-maven-3.6.0,其中setting.xml文件已修改,采用阿里云中央仓库,下载到本地路径“E:\maven\maven-repository”,修改时只要将上面的路径修改成自己的就行。
Setting Up a Certificate Authority Section 4.6. Performance Considerations Chapter 5. Denial of Service Attacks Section 5.1. Network Attacks Section 5.2. Self-Inflicted Attacks Section 5.3....
标题 "GPRS+Dial-Up+Setting+v1.1" 提及的是一个关于GPRS拨号设置的软件或教程,版本为v1.1。GPRS(General Packet Radio Service)是一种2.5代移动通信技术,它允许在GSM网络上提供分组交换的数据服务,使得手机和...
Chapter 3, Axis2 XML Model (AXIOM) - Learn about the heart of a web service framework and learning more about XML processing in Axis2. Chapter 4, Execution Chain - Learn how to extend the core ...
This book teaches you everything you need to know, right from setting up Kafka clusters to understanding basic blocks like producer, broker, and consumer blocks. Once you are all set up, you will ...