如何在 XAMPP (Windows) 上设置 Apache 虚拟主机 [英] How To Set Up Apache Virtual Hosts on XAMPP (Windows)

查看:35
本文介绍了如何在 XAMPP (Windows) 上设置 Apache 虚拟主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 XAMPP (Windows) 上设置 Apache 虚拟主机

解决方案

##Full guideline to make VHOST (Virtual Host) on XAMPP/WAMP

<块引用>

注意:首先,本指南的唯一区别在于 XAMPP 和 WAMP 或其他适用于 Windows 的 Apache 软件包之间的区别是这些软件包安装在您的计算机上的位置的问题.

简介: vhost(虚拟主机)是一个很好的解决方案,如果您打算在 php 中开发许多独立项目并使它们彼此隔离,例如:

  • 项目 1 基于 php 并具有本地 url 地址 php.localhost
  • 项目 2 是一个 Laravel 项目,网址为 laravel.localhost
  • 项目 3 是一个带有 url codeigniter.localhost
  • 的 Codeigniter
  • 项目 4 是一个带有 url wordpress.localhost
  • 的 WordPress

您应该安装了最新版本的 XAMPP/WAMP.顺便说一句,就我而言,我只安装了 Apache 和 MySQL.

<块引用>

*注意:默认情况下,大多数 Web 服务器将使用端口 80 作为默认端口,在某些情况下,如果您安装了另一个 Web 服务器(如(Microsoft IIS)),它也会使用端口 80 或在其他情况下,SKYPE 有时也会使用端口 80,就像某些用户在 Windows 8/10 中的情况一样,在这种情况下,您可以做两件事

  • 将您的 Apache 端口更改为端口 80808081 等或其他一些免费端口(注意:8080 有时也会出现一些问题)
  • 或更改您的其他软件/IIS 端口并在 Skype 的情况下将 Apache 保持为默认端口 80,只需在端口 80 上安装 Apache 后杀死 Skype 应用程序并重新启动它,Skype 将自动获取新端口.

我已选择将我的 Apache 更改为端口 8080.

<块引用>

注意:

  • 对于下一部分,我们需要使用记事本或常规 IDE 等文本编辑器.我使用了 sublime 文本编辑器.
  • 如果您将 Apache 保留为默认端口 80,请跳过此部分并转到虚拟主机

更改 XAMPP/WAMP 端口(仅在必要时)如果保留默认值,则跳转到设置虚拟主机

步骤 1. 编辑 httpd.conf

点击配置 ->Apache (httpd.conf)或者你在这里找到文件 C:\XAMPP\apache\conf\httpd.conf, C:\XAMPP\apache2\conf\httpd.conf, C:\Bitnami\wampstack\apache\conf\httpd.confC:\Bitnami\wampstack\apache2\conf\httpd.conf 或类似位置.

换行

听80

到 8080 或其他免费端口.

听8080

当我们打开 httpd.conf 文件时,我们需要确保包含 httpd-vhosts.conf:包括conf/extra/httpd-vhosts.conf

虚拟主机

<块引用>

注意:默认服务器文档位于 C:\XAMPP\htdocsC:\Bitnami\wampstack\apache2\htdocs如果您只有一个项目或许多小测试文件,那很好.但是,如果您需要开发多个项目,则建议通过虚拟主机将它们分开,如前所述.

步骤 2. 设置虚拟主机

  1. 为您的项目创建一个文件夹;我在 c:\vhost 中创建了一个,你可以称之为项目等.
  2. c:\vhost 文件夹中,我们创建一个子文件夹 domain1 或 project1 或任何其他,由您决定 (c:\vhost\project1)
  3. 打开 httpd-vhosts.conf 文件 C:\XAMPP\apache\conf\extra\httpd-vhosts.conf 根据 vhost 的数量添加以下代码您要创建:

<目录 C:/vhost>允许覆盖所有要求所有授予</目录>#这是XAMPP的默认地址<虚拟主机 *:8080>DocumentRoot "C:/XAMPP/htdocs/";服务器名称本地主机</虚拟主机>#这是XAMPP中的第一个vhost地址<虚拟主机 *:8080>DocumentRoot "C:/vhost/project1/";服务器名称 php.localhostSetEnv NS_ENV variable_value</虚拟主机>#这是 Laravel 等项目在 XAMPP 中的第二个虚拟主机地址<虚拟主机 *:8080>DocumentRoot "C:/vhost/Laravel-Blog/public";服务器名称 laravel.localhost</虚拟主机>等等

<块引用>

注意:如果你在 Laravel 项目上工作,你可以创建无限的 Laravel 项目以及其他框架,如 codeigniter、Yii 等,重点是在 c:\vhost\laravel1 上拥有你的 Laravel 项目,c:\vhost\laravel2 等并将 c:\vhost\laravel1\public 设为DocumentRoot 等,如前所示.每个 Laravel 项目都有自己的虚拟主机网址.

保存并关闭文件

一些附加信息和注意事项:

  • 如果端口保持默认 80,则 URL 地址将为 localhost
  • 如果端口保持默认 80,则 VirtualHost 标签应更改为
  • 如果端口改为8080,则URL地址为localhost:8080
  • vhost URL 地址可能看起来像这样 project1.localhost:8080
  • 您可以像这样添加无限的项目和虚拟主机.

我们还没有准备好,阅读更多.

第 3 步:编辑 Windows 主机文件

  1. 从 XAMPP/WAMP 停止 Apache 和 MySQL 服务.
  2. C:\windows\system32\drivers\etc中打开hosts文件您需要管理员权限才能编辑文件.我建议直接使用 Sublime 文本编辑器编辑文件.
  3. 在文件末尾添加127.0.0.1 project1.localhost,保存并关闭文件.

127.0.0.1 本地主机127.0.0.1 php.localhost127.0.0.1 laravel.localhost127.0.0.1 codeigniter.localhost127.0.0.1 wordpress.localhost127.0.0.1 laravel2.localhost等等,这些只是例子

保存文件

最后一步.再次启动/重新启动您的 Apache 和 MySQL.


**添加(建议)**<块引用>

注意:在您的开发过程中,您可能会遇到打开缓存的问题,因为您可能会冒险更新 Laravel 中的某些内容,而这些内容不会出现在您的浏览器中.因此,您可能需要在本地环境中的开发过程中禁用 php 缓存.

打开 Apache(XAMPP 或 WAMP)文件夹中 php 文件夹下的文件 php.ini 和将 opcache.enable 更改为 0 使其看起来像这样:

[opcache]zend_extension=php_opcache.dll;确定 Zend OPCache 是否启用opcache.enable=0


添加(替代解决方案)

<块引用>

重要提示:以下解决方案不适合测试Restful Api、canvas、繁重的解决方案等.它可能会给您带来一些麻烦,我建议使用默认80端口的虚拟主机解决方案.

无需配置 XAMPP/WAMP 虚拟主机,即可部署临时虚拟服务器,启动 CMD 控制台并运行以下 php 命令:

php -S localhost:8001 -t c:\vhost\Laravel-Project\public

  • 端口 8001 可以更改为任何可用的端口号,并确保不与其他软件端口冲突.
  • c:\vhost\Laravel... 路径应该更改为您的项目路径.
  • 可以启动多个服务器,但应该有不同的端口号.

在您的浏览器中,您只需要编写

http://localhost:8001/

关于 403 访问禁止错误的说明

如果您在浏览网站时收到 403 Access forbidden 错误,您可能需要将其添加到 httpd.conf 文件中:

允许覆盖无要求所有授予</目录>

How To Set Up Apache Virtual Hosts on XAMPP (Windows)

解决方案

##Full guideline to make VHOST (Virtual Host) on XAMPP/WAMP

Note: To start with, the only difference for this guideline, between XAMPP and WAMP or other Apache packages for Windows, is question where those packages are installed on your computer.

Introduction: vhost (virtual host) is a great solution if you intend to develop many independent project in php and keep them isolated from each others, like:

  • Project 1 is based on php and has local url address php.localhost
  • Project 2 is a Laravel project with url laravel.localhost
  • Project 3 is a Codeigniter with url codeigniter.localhost
  • Project 4 is a WordPress with url wordpress.localhost

You are expected to have installed the latest version of XAMPP/WAMP. Btw, in my case, I have only installed Apache and MySQL.

*Note: By default, most web servers will uses port 80 as default port, in some situation if you have another web server installed like (Microsoft IIS), it uses port 80 as well OR in other cases SKYPE does also some times use port 80 like it is the case for some in Windows 8/10 for some users, in that case you can do two thing’s

  • Change your Apache port to port 8080, 8081 etc. or some other free ports (note: 8080 can some times also give some problems)
  • OR Change your others software/IIS port and keep Apache to default port 80 in case of Skype, just kill the Skype app and restart it after installing Apache on port 80, Skype will automatically get new port.

I have chosen to change my Apache to port 8080.

Note:

  • For next section, we need to use text editor like notepad or regular IDE. I used sublime text editor.
  • If you keep Apache to default port 80, skip this part and move on to Virtual hosts

Change XAMPP/WAMP port (only if necessary) if left default, then jump to Setting virtual host

Step 1. Edit httpd.conf

Click on Config -> Apache (httpd.conf) Or you find the file here C:\XAMPP\apache\conf\httpd.conf, C:\XAMPP\apache2\conf\httpd.conf, C:\Bitnami\wampstack\apache\conf\httpd.conf or C:\Bitnami\wampstack\apache2\conf\httpd.conf or similar location.

Change the line with

Listen 80

To 8080 or other, free ports.

Listen 8080

While we have httpd.conf file open we need to ensure that httpd-vhosts.conf is included: Include conf/extra/httpd-vhosts.conf

Virtual hosts

Note: By default server documents are locate on C:\XAMPP\htdocs or C:\Bitnami\wampstack\apache2\htdocs that’s fine if you have only one project or many small test files. However, if you need to develop many projects then it suggested separating them by virtual host, as mentioned earlier.

Step 2. Setting Virtual host

  1. Create a folder for your projects; I have create one in c:\vhost you can call it projects etc.
  2. In c:\vhost folder we create a sub-folder domain1 or project1 or any other, it is up to you (c:\vhost\project1)
  3. Open httpd-vhosts.conf file C:\XAMPP\apache\conf\extra\httpd-vhosts.conf Add following code in line depending on how many vhost you want to create:

<Directory C:/vhost>
    AllowOverride All
    Require all granted
</Directory>

#this is the default address of XAMPP    
<VirtualHost *:8080>
    DocumentRoot "C:/XAMPP/htdocs/"
    ServerName localhost
</VirtualHost>

#this is the first vhost address in XAMPP
<VirtualHost *:8080>
    DocumentRoot "C:/vhost/project1/"
    ServerName php.localhost
    SetEnv NS_ENV variable_value
</VirtualHost>

#this is the second vhost address in XAMPP for project like Laravel
<VirtualHost *:8080>
    DocumentRoot "C:/vhost/Laravel-Blog/public"
    ServerName laravel.localhost
</VirtualHost>

etc

Note: If you work on Laravel projects you can create unlimited Laravel projects as well as other frameworks like codeigniter, Yii, etc., the point is to have your Laravel project/s on c:\vhost\laravel1, c:\vhost\laravel2 etc and make c:\vhost\laravel1\public as DocumentRoot etc as showed before. Each Laravel project will have own VirtualHost URL.

Save and close the file

Some additional information and notes:

  • If port is remain default 80 then the URL address will be localhost
  • If port is remain default 80 then the the VirtualHost tag should be changed to <VirtualHost *:80>
  • If port is changed to 8080, the URL address will be localhost:8080
  • And the vhost URL address could look like this project1.localhost:8080 etc
  • You can add unlimited projects and virtual host like this way.

We are not ready yet, read more.

Step 3. Edit Windows Host file

  1. Stop Apache and MySQL services from XAMPP/WAMP.
  2. Open hosts file in C:\windows\system32\drivers\etc you need Administrator privilege to edit the file. I suggest to edit the file directly with Sublime text editor.
  3. Add 127.0.0.1 project1.localhost at the end of the file, Save and close the file.

127.0.0.1       localhost
127.0.0.1       php.localhost
127.0.0.1       laravel.localhost
127.0.0.1       codeigniter.localhost
127.0.0.1       wordpress.localhost
127.0.0.1       laravel2.localhost
etc. those are just examples

save the file

Final Step. Start/Re-start your Apache and MySQL again.


**Addition (Suggestion)**

Note: Under you development process you might face problem having cache on, because you can risk updating some thing in Laravel and it won't appear in your browser. Therefore you might need to disable php cache under development process in your local environment.

Open file php.ini under php folder in your Apache (XAMPP or WAMP) folder and change opcache.enable to 0 so it looks like this:

[opcache]
zend_extension=php_opcache.dll
; Determines if Zend OPCache is enabled
opcache.enable=0


Addition (Alternative solution)

Important: The following solution is not suitable to test Restful Api, canvas, heavy solutions, etc.it might give you some headaches, I would suggest virtual host solution with port 80 as default.

It is possible to deploy temporary Virtual Server with out necessarily configuring XAMPP/WAMP Virtual Host, start CMD console and run following php command:

php -S localhost:8001 -t c:\vhost\Laravel-Project\public

  • Port 8001 can be change to any available port number and be sure not conflicting with other software ports.
  • c:\vhost\Laravel... path should be changed to whatever your project path.
  • It is possible to start multiple servers but should have different port numbers.

In your browser you need only to write

http://localhost:8001/

Note on 403 Access forbidden error

If you get a 403 Access forbidden error when you browse to your site, you may need to add this to your httpd.conf file:

<Directory path_to_dir>
    AllowOverride none
    Require all granted
</Directory>

这篇关于如何在 XAMPP (Windows) 上设置 Apache 虚拟主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆