如何在Windows设置XAMPP虚拟主机? [英] How to setting Virtual Hosts for XAMPP in Windows?

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

问题描述

在哪里这个标签去了?

我在哪里可以找到虚拟主机文件?

我无法在文档中找到它。

 <虚拟主机的主机名:80>
   ...
   SETENV NS_ENV VARIABLE_VALUE
   ...
< /虚拟主机>


解决方案

全指引,在XAMPP / WAMP使VHOST(虚拟主机)


  

XAMPP和WAMP之间的唯一不同的是,你需要找到
  在WAMP相当于文件夹来代替。


简介:虚拟主机(虚拟主机)是一个很好的解决方案,如果你打算开发多个独立的项目,PHP,让他们从孤立对方,如:

项目1是php具有本地URL地址 project1.localhost

项目2是一个Laravel项目,网址 project2.localhost

项目3是一个codeigniter与网址 project3.locathost

您预期已经安装了最新版本的XAMPP / WAMP的。顺便说一句,在我XAMPP只选择Apache和MySQL的安装。


  

*注:默认情况下,大多数Web服务器将使用 80端口作为默认的端口,在某些情况下,如果你有另一台Web服务器一样(IIS)的
  使用80端口或在其他情况下 SKYPE也确实有些时候使用端口
  80喜欢它是一些在Windows 10的情况下,在这种情况下,你可以做
  两招的



  • 更改您的XAMPP / WAMP端口 8080端口 81 或其他一些自由港(8080会有些时间也给了一些问题)

  • 或更改他人软件/ IIS端口,并保持XAMPP默认 80端口 Skype中的情况下,只是退出Skype并启动它完成安装后,它会自动获得新的端口。

我选择我的XAMPP / WAMP更改为 8080端口


  

注:对于下一节,我们需要使用文本编辑器如记事本或普通IDE。我用崇高的文本编辑器。


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

第1步:修改httpd.conf文件

点击配置 - >阿帕奇(httpd.conf文件)
或者你在这里找到文件 C:\\ XAMPP \\ apache的\\的conf \\ httpd.conf文件 C:\\ XAMPP \\ Apache2的\\的conf \\ httpd.conf文件 C:\\ Bitnami \\ wampstack \\ apache的\\的conf \\ httpd.conf文件 C:\\ Bitnami \\ wampstack \\ Apache2的\\的conf \\ httpd.conf文件或类似的位置。

更改与行

 收听80

要8080或其他自由港。

 收听8080

虽然我们有的httpd.conf 文件打开,我们需要确保的httpd-vhosts.conf 包括:

虚拟主机

包含的conf /额外/的httpd-vhosts.conf


  

在默认情况下服务器文件定位在 C:\\ XAMPP \\ htdocs中
   C:\\ Bitnami \\ wampstack \\ Apache2的\\ htdocs中这很好,如果你只有一个
  项目或许多小测试文件。


然而,如果你需要开发很多项目则建议的虚拟主机将它们分开。

第2步:设置虚拟主机

之前信息化笔记启动:


  • 如果端口仍然默认为80,然后URL地址将本地主机

  • 如果端口保持默认80则在虚拟主机标签应改为<虚拟主机*:80>

  • 如果端口更改为8080,URL地址将本地主机:8080

  • 而虚拟主机URL地址可能看起来像这样 project1.localhost:8080

  • 您可以添加无限的项目和虚拟主机像这样。

1为您的项目文件夹;我创建一个在 C:\\虚拟主机可以称之为项目等。

2-在 C:我们创建一个子文件夹或域1 PROJECT1或任何其他\\虚拟主机文件夹,它是由你(Ç :\\虚拟主机\\ PROJECT1

3打开的httpd-vhosts.conf 文件 C:\\ XAMPP \\ apache的\\的conf \\额外\\的httpd-vhosts.conf 添加以下符合code取决于你想要多少虚拟主机创建:

 <目录C:/虚拟主机>
    所有的AllowOverride
    要求所有批准
< /目录>#这个是XAMPP的默认地址
<虚拟主机*:8080>
    的DocumentRootC:/ XAMPP / htdocs中/
    服务器名本地主机
< /虚拟主机>#这个是XAMPP的第一个虚拟主机地址
<虚拟主机*:8080>
    的DocumentRootC:/虚拟主机/ PROJECT1 /
    服务器名project1.localhost
    SETENV NS_ENV VARIABLE_VALUE
< /虚拟主机>#这个是XAMPP像Laravel项目第二个虚拟主机地址
<虚拟主机*:8080>
    的DocumentRootC:/虚拟主机/ Laravel-博客/公
    服务器名laravel.localhost
< /虚拟主机>


  

注意:如果您在Laravel工程项目可以创造无限的Laravel项目以及像其他的Yii框架,关键是要
  对您Laravel的项目/ s的C:\\虚拟主机\\ laravel1
   C:\\虚拟主机\\ laravel2 等,使 C:\\虚拟主机\\ laravel1 \\公共作为
  的DocumentRoot等像以前一样表现。每个Laravel项目都会有自己的
  虚拟主机URL。


保存并关闭文件

我们还没有准备好,多读一些。

第3步:编辑Windows主机文件

从XAMPP / WAMP 1停止Apache和MySQL服务。

2 - 打开主机在文件C:\\ WINDOWS \\ SYSTEM32 \\ drivers \\ etc下
你需要管理员权限才能编辑文件。
我建议直接与崇高的文本编辑器编辑该文件。

3添加 127.0.0.1 project1.localhost 在文件的最后,保存并关闭文件。

  127.0.0.1本地主机
127.0.0.1 project1.localhost
127.0.0.1 project2.localhost
127.0.0.1 laravel.localhost

保存文件

最后一步。
启动/重新重新启动Apache和MySQL。


  

请注意:您可能需要删除浏览器缓存并且也
  好正在开发过程中禁用PHP缓存。


  
  

打开文件的php.ini 在您的XAMPP或WAMP文件夹PHP文件夹下,并
  变化 opcache.enable 0

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



加(替代解决方案)

有可能部署了一定配置XAMPP / WAMP虚拟主机暂时的虚拟服务器,启动CMD控制台,并运行下面的PHP命令:

  PHP -S本地主机:8001 -t C:\\虚拟主机\\ Laravel项目\\公


  • 端口8001可以变化到任何可用的端口号,并确保冲突的,也不与其他软件的端口。

  • C:\\虚拟主机\\ Laravel ... 路径应改为什么都你的项目路径

  • 它可以启动多个服务器,但应该有不同的端口号。

在浏览器中你只需要右键

 的http://本地主机:8001 /

在403禁止访问错误注意

如果您在浏览到您的网站得到一个403禁止访问的错误,你可能需要添加到您的httpd.conf文件:

 <目录path_to_dir>
    没有的AllowOverride
    要求所有批准
< /目录>

Where does this tag go?

Where do I find the vhosts file?

I couldn't find it in the documentation.

<VirtualHost hostname:80>
   ...
   SetEnv NS_ENV variable_value
   ...
</VirtualHost>

解决方案

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

The only different between XAMPP and WAMP is you need to locate the equivalent folders in WAMP instead.

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 php has local url address project1.localhost
Project 2 is a Laravel project with url project2.localhost
Project 3 is a Codeigniter with url project3.locathost

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

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

  • Change your XAMPP/WAMP port to port 8080, 81 or some other free ports (8080 can some times also give some problems)
  • OR Change your others software/IIS port and keep XAMPP to default port 80 in case of Skype just quit Skype and start it after you finish the installation it will automatically get new port.

I have chosen to change my XAMPP/WAMP to port 8080.

Note: For next section, we need to use text editor like notepad or regular IDE. I used sublime text editor.

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:

Virtual hosts

Include conf/extra/httpd-vhosts.conf

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.

Step 2. Setting Virtual host

Informative notes before you start:

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

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 project1.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 Yii, 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

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       project1.localhost
127.0.0.1       project2.localhost
127.0.0.1       laravel.localhost

save the file

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

Note: You might need to delete your cache in your browser and also it is good to disable php cache under development process.

Open file php.ini under php folder in your XAMPP or WAMP folder and change opcache.enable to 0

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


Addition (Alternative solution)

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 nor conflicting with other software ports.
  • c:\vhost\Laravel... path should be changed to what ever your project path.
  • It is possible to start multiple servers but should have different port numbers.

In your browser you need only to right

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>

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

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