在本地运行多个网站 Apache PHP Ubuntu

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1329303/
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-09-13 18:06:01  来源:igfitidea点击:

Running multiple websites locally Apache PHP Ubuntu

apacheubuntuvirtualhost

提问by Pascal MARTIN

I am attempting to run more than one virtualhost(?), for example: http:/localhost will point to one project, http:/newsite to another and http:/myfavourite again to a different project a different document root.

我正在尝试运行多个虚拟主机(?),例如:http:/localhost 将指向一个项目,http:/newsite 将指向另一个项目,而 http:/myfavourite 再次指向另一个项目的不同文档根目录。

(each http:// is http:/ here because of hyperlink posting restrictions)

(由于超链接发布限制,这里的每个 http:// 都是 http:/)

I have had no success looking where to edit the apache files in /etc/apache2. Am I looking for a vhosts file?

我在 /etc/apache2.conf 中寻找编辑 apache 文件的位置没有成功。我在寻找 vhosts 文件吗?

Any advice would be awesome, thanks.

任何建议都会很棒,谢谢。

回答by Pascal MARTIN

Here is a chapter of an e-book that explains how to create Virtual Hosts to do precisely what you want -- and the examples are using Ubuntu : Creating A Local Domain Using Apache Virtual Hosts

这是一本电子书的一章,解释了如何创建虚拟主机来精确地执行您想要的操作——示例使用的是 Ubuntu:Creating A Local Domain Using Apache Virtual Hosts

In a few words :

简而言之 :

  • You first need to create the VirtualHost
  • Then, you have to edit your hosts file (under Linux, it's /etc/hosts) so the new "pseudo domain name" points to your machine.
  • 您首先需要创建 VirtualHost
  • 然后,您必须编辑您的主机文件(在 Linux 下,它是/etc/hosts),以便新的“伪域名”指向您的机器。

For the VirtualHost, with Ubuntu, you'd create a new file in /etc/apache2/sites-available/; for instance named your-site.com; it would contain something like this :

对于 VirtualHost,使用 Ubuntu,您需要在/etc/apache2/sites-available/; 例如命名your-site.com; 它将包含这样的内容:

<VirtualHost *:80>
    ServerName your-site.com
    DocumentRoot /.../www/...

    <Directory /.../www/...>
        Options Indexes FollowSymLinks Includes
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

And you register this file so it's loaded by Apache, with this command :

然后您注册此文件,以便 Apache 加载它,使用以下命令:

sudo a2ensite your-site.com

And, then, reload Apache :

然后,重新加载 Apache :

sudo /etc/init.d/apache2 reload


You then have to edit /etc/hosts to add a line like this :


然后,您必须编辑 /etc/hosts 以添加如下一行:

127.0.0.1       your-site.com

So "your-site.com" actually points to your own computer.

所以“your-site.com”实际上指向您自己的计算机。


What is important is that the name used to access your website in a browser is the one that is declared in the hosts file ; it also must be the same than the one used by the ServerName directivr in Apache's configuration.


重要的是,用于在浏览器中访问您网站的名称是在 hosts 文件中声明的名称;它也必须与 Apache 配置中的 ServerName 指令使用的相同。


When you have done that for one VirtualHost... It's just the same for every other one : only the name of the site, and it's DocumentRoot, change.


当您为一个 VirtualHost 完成此操作后...对于其他每个虚拟主机都一样:仅更改站点名称和 DocumentRoot。


Hope this helps!


希望这可以帮助!

回答by Adam Benzan

You could edit your /etc/hosts and add multiple names pointing to 127.0.0.1, then add VirtualHost entries for each of those names. Depending on your server, the config could be in /etc/apache2/conf/httpd.conf or in /etc/apache2/sites-available. If it's the latter, then hereis the first google hit that I got for the config.

您可以编辑 /etc/hosts 并添加指向 127.0.0.1 的多个名称,然后为每个名称添加 VirtualHost 条目。根据您的服务器,配置可能在 /etc/apache2/conf/httpd.conf 或 /etc/apache2/sites-available 中。如果是后者,那么是我为配置获得的第一个谷歌命中。

回答by knittl

localhost has nothing to do with apache, but is an alias to your machine (ip 127.x.x.x).

localhost 与 apache 无关,而是您机器的别名(ip 127.xxx)。

you'll have to edit /etc/hoststo accomplish what you want.

你必须编辑/etc/hosts才能完成你想要的。

why do you want to do that? isn't http://localhost/newsiteenough?

你为什么要这样做?是不是HTTP://本地主机/ newsite就够了吗?