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

[经验分享] Creating virtual hosts on Apache httpd 2.2

[复制链接]

尚未签到

发表于 2017-1-9 12:11:26 | 显示全部楼层 |阅读模式
  This tutorial is intended for use only in a local testing environment on Windows. For a production server, please refer to the official documentation on the Apache site. These instructions have been updated for Apache 2.4 and later.
  A common problem with setting up virtual hosts in a local testing environment is getting the following message when accessing a virtual host:

  • Forbidden
    You don't have permission to access /index.php on this server.
  Because of the permissions issue, I recommend creating a top-level folder to hold all virtual hosts. The following instructions assume that all virtual hosts are located in a folder called C:\vhosts.
  NOTE: Security restrictions on Windows Vista and Windows 7 (and presumably Windows 8) prevent you from saving edits to the files referred to in these instructions, even if you are logged in as an administrator. To get around this restriction, open Notepad or your script editor from the Start menu by right-clicking the program name and selecting "Run as Administrator" from the context menu. Then open the relevant files by using File > Open inside the program you have just launched. By default, Notepad shows only files with a .txt file name extension, so you need to select the option to view All Files (*.*) in the Open dialog box. You can then save the files normally after editing them. (If you're using XAMPP, this restriction applies only to steps 2–4.)

  •   Create a subfolder inside C:\vhosts for each virtual host that you want to add to your Apache server.
  •   Open C:\WINDOWS\system32\drivers\etc\hosts in Notepad or a script editor. Look for the following line at the bottom:
    127.0.0.1   localhost
  •   On a separate line, enter 127.0.0.1, followed by some space and the name of the virtual host you want to register. For instance, to set up a virtual host called phpdw, enter the following:
    127.0.0.1   phpdw
  •   Add any further virtual hosts, each one on a separate line and pointing to the same IP address (127.0.0.1). Save the hosts file, and close it.
  •   Open the main Apache configuration file, httpd.conf, in a text editor. It's in the Apache conf folder. If you're using XAMPP, the file is located at C:\xampp\apache\conf\httpd.conf. Scroll down to the Supplemental configuration section at the end, and locate the following section (around line 500):
    #Virtual hosts #Include conf/extra/httpd-vhosts.conf
  •   Remove the # from the beginning of the second line so the section now looks like this:
    #Virtual hosts Include conf/extra/httpd-vhosts.conf
  •   Save httpd.conf and close it.
  •   Open extra\httpd-vhosts.conf in Notepad or a text editor. If you're using XAMPP, the location is C:\xampp\apache\conf\extra\httpd-vhosts.conf. The main section looks like this:
    DSC0000.gif

      Note: In XAMPP, all lines are commented out. You must remove the hash mark from the beginning of the line that contains the following directive:
    NameVirtualHost *.80
  •   Position your cursor in the blank space shown on line 15 in the preceding screenshot, and insert the following three lines of code:
    <Directory C:/vhosts>   Require all granted </Directory>
      This sets the correct permissions for the folder that contains the sites you want to treat as virtual hosts. (Note: Older versions of Apache used Allow from all. This has changed in Apache 2.4.)
      If you chose a location other than C:\vhosts as the top-level folder, replace the pathname in the first line. The pathname must use forward slashes in place of the Windows convention of backward slashes. Also surround the pathname in quotes if it contains any spaces.
      As long as all your virtual hosts are in subfolders of this top-level folder, this directive sets the correct permissions for all of them. However, if they are in different top-level folders, create a separate <Directory> directive for each one.
  •   The code shown on lines 27 through 42 in the preceding screenshot shows examples of how to define virtual hosts (in XAMPP, they're commented out). It shows all the commands that can be used, but only DocumentRoot and ServerName are required.
      When you enable virtual hosting, Apache disables the main server root, so the first definition needs to reproduce the original server root. You then add each new virtual host within a pair of <VirtualHost> tags, using the location of the site’s web files as the value for DocumentRoot, and the name of the virtual host for ServerName. Again, use forward slashes, and if the path contains any spaces, enclose the whole path in quotes. If your server root is located, like mine, at C:\htdocs, and you are adding phpdw as a virtual host in C:\vhosts, change the code shown on lines 27 through 42 so they look like this (in XAMPP, just add these new directives at the bottom of the file, and set the DocumentRoot for localhost to C:/xampp/htdocs):
    <VirtualHost *:80>   DocumentRoot c:/htdocs   ServerName localhost </VirtualHost> <VirtualHost *:80>   DocumentRoot c:/vhosts/phpdw   ServerName phpdw </VirtualHost>
  •   Save httpd-vhosts.conf, and restart Apache. All sites in the server root will continue to be accessible through http://localhost/sitename/. Anything in a virtual host will be accessible through a direct address, such as http://phpdw/.
  •   If you still have difficulty accessing your virtual hosts, make sure that you have added index.php to the DirectoryIndex directive in httpd.conf.
  http://foundationphp.com/tutorials/apache_vhosts.php

运维网声明 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-326036-1-1.html 上篇帖子: 本地机apache配置基于域名的虚拟主机详解 下篇帖子: 修改apache配置文件去除thinkphp url中的index.php
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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