设为首页 收藏本站
查看: 454|回复: 0

[经验分享] Setting Up A Virtual Host in Apache

[复制链接]

尚未签到

发表于 2017-1-4 11:01:48 | 显示全部楼层 |阅读模式
  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!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-323774-1-1.html 上篇帖子: apache 虚拟主机中的重定向 下篇帖子: RedHat9.0 上配置SVN+APACHE
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表