在linux上使用apache设置子域

Dar*_*ech 18 linux apache subdomain

我不敢相信我之前没有这样做过,但我希望得到一个明确的答案,所以我已经准备好了.

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

<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>
Run Code Online (Sandbox Code Playgroud)

因此,这提供了~/public_html所有罚款的HTML和PHP文件.但我有多个项目,所以想开始使用子域名.我想要做的是从~/public_html/myproject/根目录提供文件myproject.localhost.

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

<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>
Run Code Online (Sandbox Code Playgroud)

但阿帕奇抱怨道:

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!
Run Code Online (Sandbox Code Playgroud)

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

任何帮助非常感谢.

编辑:

这是我现在的完整文件:

<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> 
Run Code Online (Sandbox Code Playgroud)

Abs*_*ERØ 27

你告诉Apache你要在<VirtualHost>标签内部回答什么IP和端口,所以这里*表示任何IP,但接受端口80上对该站点的请求.接下来你需要告诉Apache文件根目录在哪里.~/表示您的默认主目录,因此如果您DocumentRoot恰好是默认home变量,那么它将适用于您现有的表示法(取决于您运行服务器的用户).然后你会声明服务器名称.

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

<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>
Run Code Online (Sandbox Code Playgroud)

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