在 Linux 上使用 Apache 设置子域 [英] Setting up a subdomain with Apache on Linux

查看:20
本文介绍了在 Linux 上使用 Apache 设置子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不敢相信我以前没有这样做过,但我想要一个明确的答案,所以我已经准备好继续前进了.

I can't believe that I haven't done this before, but I would like a definitive answer so I'm all set going forward.

我在 /etc/apache2/sites-available/mysite 有一个 apache 配置文件,如下所示:

I have an apache config file at /etc/apache2/sites-available/mysite which looks like this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /home/sam/public_html
    <Directory />
            Options FollowSymLinks
            AllowOverride All
    </Directory>
    <Directory /home/sam/public_html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

所以这可以从 ~/public_html 提供 html 和 php 文件.但是我在那里有多个项目,所以想开始使用子域.我想要做的是将 ~/public_html/myproject/ 中的文件作为 myproject.localhost 的根目录提供.

So this serves html and php files from ~/public_html all fine. But I have multiple projects there so would like to start using subdomains. What I want to do is serve files from ~/public_html/myproject/ as the root directory for myproject.localhost.

我尝试将以下内容添加到我的 apache 文件底部:

I have tried adding the following to the bottom of my apache file:

<VirtualHost myproject.localhost>
    DocumentRoot ~/public_html/myproject/
    ServerName myproject.localhost
    ServerAdmin admin@myproject.localhost
    <Directory ~/public_html/myproject>
            Options Indexes FollowSymLinks
            AllowOverride FileInfo
            Order allow,deny
            Allow from all
    </Directory>
</VirtualHost>

但 apache 抱怨:

but apache complains:

Restarting web server: apache2[Tue Aug 20 11:06:19 2013] [error] (EAI 2)Name or service not known: Could not resolve host name myproject.localhost -- ignoring!
 ... waiting [Tue Aug 20 11:06:20 2013] [error] (EAI 2)Name or service not known: Could not resolve host name myproject.localhost -- ignoring!

我知道我犯了一个基本错误,但我不确定它是什么.

I know I'm committing a fundamental error, but I'm not sure what it is.

这是我现在的完整文件:

This is my complete file now:

<VirtualHost *:80>
    DocumentRoot /home/sam/public_html/ryua1226-magento/
    ServerName mydomain.localhost
    ServerAdmin admin@mydomain.localhost
    <Directory /home/sam/public_html/ryua1226-magento>
            Options Indexes FollowSymLinks
            AllowOverride FileInfo
            Order allow,deny
            Allow from all
    </Directory>
</VirtualHost>


<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /home/sam/public_html
    <Directory />
            Options FollowSymLinks
            AllowOverride All
    </Directory>
    <Directory /home/sam/public_html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost> 

推荐答案

你在 <VirtualHost> 标记,所以这里的 * 表示任何 IP,但在端口 80 上接受对该站点的请求.接下来您需要告诉 Apache 在哪里文档根是.~/ 表示您的默认主目录,因此如果您的 DocumentRoot 恰好是默认的 home 变量,那么它将与您现有的符号一起使用(取决于您以哪个用户身份运行服务器).然后您将声明服务器名称.

You are telling Apache what IP and port you want to answer it on inside of the <VirtualHost> tag so here * means any IP, but accept requests for this site on port 80. Next you need to tell Apache where the document root is. ~/ means your default home directory, so if your DocumentRoot just happens to be the default home variable then it would work with your existing notation (depending on which user you're running the server as). Then you would declare the server name.

除非您使用别名,否则您为其创建主机的每个域名都需要自己的虚拟主机指令.

<VirtualHost *:80>
    DocumentRoot /home/sam/public_html
    ServerName myproject.localhost

    # Other directives here

</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /home/sam/public_html/myproject
    ServerName myotherproject.localhost

    # Other directives here

</VirtualHost>

关于主机除此之外,您为主机创建的任何特殊名称都需要进入主机文件或 DNS 服务器.这样,任何正在寻找您的服务器的 Web 浏览器都可以找到它,而无需输入 IP.由于如果您尝试仅使用 IP 访问服务器,则您的设置可能会在同一 IP 上有多个主机,因此您只会得到第一个响应 IP 的主机(通常是 vhosts 列表中的顶部).

About Hosts In addition to this, any special name that you create for a host needs to go into a hosts file or in the DNS server as well. This way any web browser that is looking for your server can find it without having to type in the IP. Since you'll likely have multiple hosts on the same IP with your setup if you were to try and access the server with the IP only, you would only get the first host to respond on the IP (usually the top in the vhosts list).

这篇关于在 Linux 上使用 Apache 设置子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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