设置默认的 apache 虚拟主机

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

setting a default apache virtual host

apache

提问by Sam Hamilton

Is there any better way of setting the default apache virtual host other than it just picking the first config it finds?

除了选择它找到的第一个配置之外,还有没有更好的方法来设置默认的 apache 虚拟主机?

I have a server with many domains, of which only some are configured with httpd but the default virtual host severed up is for example is aaa.com where as really I would like it to default to mmm.com instead?

我有一个有很多域的服务器,其中只有一些配置了 httpd,但是默认的虚拟主机被切断例如是 aaa.com 而我真的希望它默认为 mmm.com 吗?

Something like parking domains without going through the hassle of setting up a config for each one - then I can serve a "content this domain has not been created yet" page?

诸如停放域之类的东西,而无需为每个域设置配置的麻烦 - 然后我可以提供“该域尚未创建的内容”页面?

Cheers

干杯

回答by leepowers

You can create a default virtual host and name it something like 000-defaultso that it loads first and is used unless another vhost matching the requested domain is found. Here's the bare-bones 000-default:

您可以创建一个默认虚拟主机并将其命名为类似的名称,000-default以便它首先加载并使用,除非找到另一个与请求的域匹配的虚拟主机。这是最基本的000-default

<VirtualHost *:80>
    DocumentRoot /var/www
    <Directory /var/www >
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Then you can setup a PHP file under /var/wwwto create a domain parking page (this is a very simplified example):

然后你可以在下面设置一个 PHP 文件/var/www来创建一个域停放页面(这是一个非常简单的例子):

<?php

printf('The domain <b>%s</b> is being parked', 
    htmlentities($_SERVER['HTTP_HOST']));

?>

回答by cmac

The first sites-availableconf file is default (alphabetically). There should be a 000-default.conffile already, if not create it.

第一个站点可用的conf 文件是默认的(按字母顺序)。如果没有创建它,应该已经有一个000-default.conf文件。

Edit this one to your liking and then make sure it's enabled a2ensite 000-default.conf. And apache2 is reloaded sudo service apache2 reload.

根据您的喜好编辑这个,然后确保它已启用a2ensite 000-default.conf。并且 apache2 被重新加载sudo service apache2 reload

Then any request that isn't caught by your other vhosts will come here.

然后任何未被您的其他虚拟主机捕获的请求都会来到这里。

回答by kibitzer

Use ServerAlias in a name-based VirtualHost, you will only have to add one line per each new domain.

在基于名称的 VirtualHost 中使用 ServerAlias,您只需为每个新域添加一行。