php 使用 XAMPP 托管多个本地站点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3660066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:36:27  来源:igfitidea点击:

Hosting multiple local sites with XAMPP

phplocalhostxampp

提问by Greg

I'm new to using XAMPP so this may be simple to some people.

我是使用 XAMPP 的新手,所以这对某些人来说可能很简单。

I have a few php projects that I would like to be able to debug locally and view in the browser (not concurrently, but without having to change config files/copy project folders each time I want to work on a different project).

我有一些 php 项目,我希望能够在本地调试并在浏览器中查看(不是同时进行,但每次我想处理不同的项目时都不必更改配置文件/复制项目文件夹)。

On IIS, you could set up multiple sites to serve from your machine, and I'm looking for something similar in XAMPP. When using IIS, I added multiple records to the Windows hosts file so I could access the locally hosted sites by typing friendly web-style addresses (like http://myproject1.dev)

在 IIS 上,您可以设置多个站点以从您的机器上提供服务,我正在 XAMPP 中寻找类似的东西。使用 IIS 时,我向 Windows 主机文件添加了多条记录,以便我可以通过键入友好的 Web 样式地址(如http://myproject1.dev)来访问本地托管的站点

Thanks.

谢谢。

回答by jbnunn

Greg, you're almost there--you need (like Moses said) to setup virtual hosts.

格雷格,你就快到了——你需要(就像摩西说的)设置虚拟主机。

So if your Windows hosts file has

所以如果你的 Windows 主机文件有

127.0.0.1    localhost
127.0.0.1    mysite-dev.com
127.0.0.1    anothersite-dev.com

Your virtual hosts file (httpd-vhosts.conf) might look like:

您的虚拟主机文件 (httpd-vhosts.conf) 可能如下所示:

<VirtualHost *:80>
  DocumentRoot C:/xampp/htdocs/
  ServerName localhost
</VirtualHost>

<VirtualHost *:80>

    ServerName mysite-dev.com

    DocumentRoot "C:/sites/mysite-dev"

    <Directory "C:/sites/mysite-dev">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

<VirtualHost *:80>

    ServerName anothersite-dev.com

    DocumentRoot "C:/sites/anothersite-dev"

    <Directory "C:/sites/anothersite-dev">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

Don't forget to restart the web server after you make any changes.

进行任何更改后,不要忘记重新启动 Web 服务器。

回答by Yildirim Erdemli

I would like to make an additional in terms of up to date information.

我想补充一下最新信息。

XAMMP uses port 80 by default and we are able to publish 1 website. I also use IIS for .Net projects. In this respect, I set the port to XAMMP except the 80 port. So I avoid a conflict.

XAMMP 默认使用 80 端口,我们可以发布 1 个网站。我也将 IIS 用于 .Net 项目。在这方面,我将端口设置为 XAMMP,除了 80 端口。所以我避免了冲突。

When we want to publish more than one website, we should do the following operations to httpd.conf (this is the current name).

当我们要发布多个网站时,我们应该对httpd.conf(这是当前名称)做如下操作。

1. Setting the ports

1. 设置端口

Find the #Listen expression in the httpd.conf file. Change Listen 80 to Listen 8000 (or whatever else you want)

在 httpd.conf 文件中找到 #Listen 表达式。将 Listen 80 更改为 Listen 8000(或您想要的任何其他内容)

Listen 8000

If you need 3 different websites, type the others, including 1 definition on each line, as follows.

如果您需要 3 个不同的网站,请键入其他网站,每行包括 1 个定义,如下所示。

Listen 8001
Listen 8002
Listen 8003

2. Define the file paths of sites accessed through ports

2.定义通过端口访问的站点的文件路径

Again, find in the httpd.conf file. Identify the folders of each website as follows. As you would see, I've created 3 directories called 8000, 8001, 8002 and 8003 under the htdocs directory within the XAMMP directory.

再次,在 httpd.conf 文件中找到。确定每个网站的文件夹如下。如您所见,我在 XAMMP 目录中的 htdocs 目录下创建了 3 个名为 8000、8001、8002 和 8003 的目录。

<VirtualHost *:8000>
?DocumentRoot "C:\XAMPP\htdocs00"
?ServerName localhost:8000
<\ VirtualHost>
<VirtualHost *:8001>
?DocumentRoot "C:\XAMPP\htdocs01"
?ServerName localhost:8001
<\ VirtualHost>
<VirtualHost *:8002>
?DocumentRoot "C:\XAMPP\htdocs02"
?ServerName localhost:8002
<\ VirtualHost>
<VirtualHost *:8003>
?DocumentRoot "C:\XAMPP\htdocs03"
?ServerName localhost:8003
<\ VirtualHost>

Restart your Apahche server on XAMMP. You can now view your 3rd site, such as http://localhost:8003or http://192.168.1.1:8003/.

在 XAMMP 上重新启动您的 Apache 服务器。您现在可以查看您的第三个站点,例如http://localhost:8003http://192.168.1.1:8003/

Hope to be useful.

希望有用。

回答by Bob Diego

This question was asked almost ten years ago, and the answers above are a bit dated. Note that XAMPP has a "How-To" for virtual hosts avilable off the dashboard, when you install it.

这个问题差不多是十年前问的,上面的答案有点过时了。请注意,当您安装 XAMPP 时,它有一个适用于仪表板外可用的虚拟主机的“操作方法”。

From the "Welcome to XAMPP for Windows" page (localhost/dashboard, the default when you first load localhost) click on the "HOW-TO" Guides in the top menu bar. From there, look for the link "Configure Virtual Hosts" which will lead you to the localhost page "http://localhost/dashboard/docs/configure-vhosts.html"

在“Welcome to XAMPP for Windows”页面(本地主机/仪表板,第一次加载本地主机时的默认设置)单击顶部菜单栏中的“HOW-TO”指南。从那里,查找链接“配置虚拟主机”,它将引导您到本地主机页面“ http://localhost/dashboard/docs/configure-vhosts.html

In a nutshell, the process involves editing the "httpd-vhosts.conf" file (typically in C:\XAMPP\apache\conf\extra) and replacing the contents of that file with something like this:

简而言之,该过程包括编辑“httpd-vhosts.conf”文件(通常在 C:\XAMPP\apache\conf\extra 中)并用以下内容替换该文件的内容:

<VirtualHost *:80>
        DocumentRoot "C:/xampp/htdocs/"
        ServerName localhost
</VirtualHost>

# vhosts - note sample entry from XAMPP how-to throws an error, so try this:
<VirtualHost *:80>
        DocumentRoot "C:/Users/jdoe/Documents/dev.mysite.com/htdocs"
        ServerName mysite.local
        <Directory "C:/Users/jdoe/Documents/dev.mysite.com/htdocs">
            Require all granted
            Options Indexes FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
        </Directory>
</VirtualHost>

Additional vhosts (including SSL hosts) can be had by cloning the entry, and modifying DocumentRoot and ServerName directives and port numbers (e.g. 443 for TLS (SSL)). You can find tutorialson the web for creating and signing your own certificate, if you want to go that route.

可以通过克隆条目并修改 DocumentRoot 和 ServerName 指令和端口号(例如 TLS (SSL) 的 443)来获得其他虚拟主机(包括 SSL 主机)。如果您想走那条路,您可以在网络上找到有关创建和签署您自己的证书的教程

The final step is to get your Windows machine to point your browser to the Apache host for your virtual domain (e.g. above, http://mysite.local). Using a text editor (Notebook will do) as administratorappend the following entry onto your hosts file, which lives here:

最后一步是让您的 Windows 机器将您的浏览器指向虚拟域的 Apache 主机(例如,上面的http://mysite.local)。以管理员身份使用文本编辑器(Notebook 即可)将以下条目附加到您的主机文件中,该文件位于此处:

C:\Windows\System32\drivers\etc\hosts

Append this entry to the hosts file:

将此条目附加到主机文件:

127.0.0.1           mysite.local

IMPORTANT - you must restart your Windows machine or the new host will not respond. Some documentations will tell you just to restart the browser and Apache server, but I've found that's not sufficient.

重要 - 您必须重新启动 Windows 计算机,否则新主机将不会响应。一些文档会告诉您只需重新启动浏览器和 Apache 服务器,但我发现这还不够。

IME, the hosts system and Apache directives can be particular, so be patient. You may need to rebuild configs, restart Apache, and restart your machine more than once.

IME、主机系统和 Apache 指令可能很特殊,所以请耐心等待。您可能需要多次重建配置、重新启动 Apache 并重新启动您的机器。