`

Creating virtual hosts on Apache httpd 2.2

 
阅读更多

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.)

  1. Create a subfolder inside C:\vhosts for each virtual host that you want to add to your Apache server.

  2. 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

  3. 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

  4. 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.

  5. 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

  6. Remove the # from the beginning of the second line so the section now looks like this:

    #Virtual hosts Include conf/extra/httpd-vhosts.conf

  7. Save httpd.conf and close it.

  8. 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:

    Contents of httpd-vhosts.conf

    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

  9. 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.

  10. 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>

  11. 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/.

  12. 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

分享到:
评论

相关推荐

    apache httpd-2.2 win64 x64

    本资源为"apache httpd-2.2 win64 x64",是专为64位Windows系统设计的Apache服务器版本,特别适用于Windows 7 64位操作系统。在官方下载页面可能找不到这个特定的64位Windows构建,因此这个分享显得尤为珍贵。 ...

    配合apache(2.2.x)+openssl(0.9.6->1.0.1升级)过程博文环境搭建的资源

    这些资源是配合apache(2.2.x)+openssl(0.9.6-&gt;1.0.1升级)过程博文环境搭建中所需要的资源,不是太好找,所以都传上来,主要有: 1.升级opensslV1.0.1b单独用到的三个文件:libeay32.dll,openssl.exe,ssleay32.dll ...

    apache-2.2 安装包

    这个"apache-2.2 安装包"包含了安装Apache 2.2版本所需的所有文件,特别是对于Windows平台的用户,其中包含的文件`httpd-2.2.21-win32-x86-openssl-0.9.8r.msi`是一个微软安装管理器(MSI)文件,用于在Windows 32位...

    Httpd2.2与Tomcat8.5的整合

    通过mod_jk,实现Httpd2.2与Tomcat8.5的整合.运行时,静态文件由httpd服务器处理,遇到了脚本文件,则由mod_jk模块提交给tomcat处理。

    Apache httpd-2.2.34-win64.zip

    这个名为"Apache httpd-2.2.34-win64.zip"的压缩包包含的是Apache服务器在Windows 64位平台上的版本,版本号为2.2.34。这个版本使用Visual C++ 10(VC10)编译器构建,确保与Windows系统兼容。 Apache服务器的核心...

    apache2.2安装包

    4. 虚拟主机:Apache支持多个虚拟主机,通过配置文件中的`NameVirtualHost`和`&lt;VirtualHost&gt;`指令,可以实现不同域名指向不同目录。 三、Apache 2.2与PHP集成 1. 安装PHP:首先需要安装PHP,确保版本与Apache兼容。...

    Apache v2.2最新中文手册(09年版)

    1. **配置文件结构**:Apache服务器的配置主要通过`httpd.conf`文件进行,但v2.2引入了更灵活的配置布局,允许通过`&lt;Directory&gt;`、`&lt;VirtualHost&gt;`等指令来针对特定目录或虚拟主机设置配置。 2. **模块管理**:...

    httpd-2.2.22.tar.gz httpd-2.2.22下载

    httpd-2.2.22下载,用于搭建Apache,使用Apache可以运行网页,可以结合PHP、Tomcat、Resin等(文件全称:httpd-2.2.22.tar.gz)

    Apache 2.2官方安装包

    该文件版本为Apache 2.2.31,包含两个安装包,httpd-2.2.31-x64-r6(64位系统可用),httpd-2.2.31-x86-r6(32位系统可用),解压缩后可用,将解压缩后的文件夹,放到你想要存放的位置,运行cmd,定位到该文件夹的...

    apache 2.2.29-x64

    1. **配置文件**:Apache服务器的配置主要通过httpd.conf文件进行,这个文件包含了关于服务器运行的所有设置,如端口号、虚拟主机、文档根目录、访问控制等。在Apache22目录下,你可以找到这个文件以及其他的配置...

    Apache 2.2中文手册

    1. **安装与配置**:手册会详细解释如何在不同的操作系统(如Windows、Linux、Mac OS X等)上安装Apache 2.2,并介绍配置文件httpd.conf的结构和各项设置,如监听端口、虚拟主机、日志记录等。 2. **模块管理**:...

    apache2.2.zip

    在配置方面,Apache 2.2的主配置文件通常为`httpd.conf`,位于安装目录的conf子目录中。在这个文件中,你可以定义服务器的基本设置,如监听端口、虚拟主机、日志文件位置等。此外,`.htaccess`文件允许在特定目录...

    Apache2.2+Tomcat5.5整合配置教程

    安装完成后,Apache的配置文件通常位于`Apache2.2\conf\httpd.conf`。在这个文件中,我们需要将下载的mod_jk模块拷贝到Apache的`modules`目录,并在httpd.conf文件末尾添加模块加载语句以及相关的虚拟主机配置。 ...

    Apache2.2版本中文使用手册.zip

    这个中文使用手册提供了关于如何安装、配置、管理和优化Apache2.2的详细指南,对于开发者和系统管理员来说是宝贵的资源。以下是手册中可能包含的一些关键知识点: 1. **安装Apache2.2**: - Windows、Linux、Mac ...

    apache2.2 windows安装版 httpd-win32-src.zip

    在Windows上安装Apache2.2通常包括以下步骤: 1. **下载与解压**:`httpd-win32-src.zip`是一个源代码版本,而非预编译的二进制安装包。要进行安装,你需要先解压缩文件,通常这会包含Apache的源代码和其他配置文件...

    apache 2.2

    "httpd-apache"可能是Apache 2.2的安装包或者配置文件,具体要看压缩包内容。 总之,Apache 2.2和PHP的结合是Web开发中常见的组合,它们共同为动态网站提供了强大的后端支持。通过韩顺平老师的课程,学习者可以掌握...

    apache httpd.conf中文 apache优化

    apache httpd.conf中文 apache优化

    php5.3.5 + apache2.2搭建环境

    httpd.exe -k install -n apache2.2 ``` - 启动服务: ```sh net start apache2.2 ``` 2. 若要停止服务,可以运行: ```sh net stop apache2.2 ``` #### 四、PHP5.3.5的安装与配置 **步骤1:下载与解压**...

    httpd-2.2与tomcat7连接模块mod_jk.so

    httpd-2.2与tomcat7连接模块mod_jk.so,已编译,可用。

    Apache v2.2最新中文文档(HTML)

    2. **配置指南**:详述了Apache服务器的配置文件结构,如httpd.conf和.htaccess。这部分内容涵盖虚拟主机设置、模块管理、重写规则以及权限控制等。 3. **模块参考**:Apache v2.2包含了许多模块,用于扩展其功能。...

Global site tag (gtag.js) - Google Analytics