如何设置虚拟主机指向两个不同的目录

如何设置虚拟主机指向两个不同的目录

我有一个域和一个子域,它们应该指向两个不同的文件夹,我已经尝试过此帮助,但仍然遇到问题。 (https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-16-04

www.wasamar.com.ng | wasamar.com.ng -> /var/www/html/wasamar/public

这是虚拟主机文件(/etc/apache2/sites-available/wasamar.com.ng.conf)

ServerName wasamar.com.ng

ServerAlias www.wasamar.com.ng

ServerAdmin [email protected]
DocumentRoot /var/www/html/wasamar/public

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf

www.ts.wasamar.com.ng | ts.wasamar.com.ng -> /var/www/html/wasamar_ts/public

这是虚拟主机文件(/etc/apache2/sites-available/ts.wasamar.com.ng.conf)

ServerName ts.wasamar.com.ng

ServerAlias www.ts.wasamar.com.ng

ServerAdmin [email protected]
DocumentRoot /var/www/html/wasamar_ts/public/

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf

apache.conf文件的pastebinhttp://pastebin.com/dnDfB21y

我该如何实现这一目标?

答案1

据我所知,存在一些问题,甚至可能更多。对于初学者来说,虽然您已经告诉 Apache 在哪里可以找到每个服务器的根目录,但您还没有授予 Apache 从这些目录“提供”文件的权限。您也没有告诉 Apache 这些是虚拟主机。您还需要对虚拟主机配置文件执行其他操作,但至少您需要授予 Apache 权限来服务器目录中的文件,并且您需要告诉 Apache 这是一个虚拟主机定义。仅针对此最小值进行修改,这两个文件如下所示:

这是新的虚拟主机文件(/etc/apache2/sites-available/wasamar.com.ng.conf):

<VirtualHost *:80>

ServerName wasamar.com.ng

ServerAlias www.wasamar.com.ng

ServerAdmin [email protected]
DocumentRoot /var/www/html/wasamar/public

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf

# At least you need to make the folder accessable for serving by the server.
# Versions of Apache, and modules installed can make a difference in what's inside
# the Directory directive. But as an example:
<Directory "/var/www/html/wasamar_ts/public">
        <IfModule mod_authz_core.c>
            Require all granted
        </IfModule>
        <IfModule !mod_authz_core.c>
            Order allow,deny
            Allow from all
        </IfModule>
</Directory>
</VirtualHost>

这是新的虚拟主机文件(/etc/apache2/sites-available/ts.wasamar.com.ng.conf):

<VirtualHost *:80>

ServerName ts.wasamar.com.ng

ServerAlias www.ts.wasamar.com.ng

ServerAdmin [email protected]
DocumentRoot /var/www/html/wasamar_ts/public/

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf

# At least you need to make the folder accessable for serving by the server.
# Versions of Apache, and modules installed can make a difference in what's inside
# the Directory directive. But as an example:
<Directory "/var/www/html/wasamar_ts/public">
        <IfModule mod_authz_core.c>
            Require all granted
        </IfModule>
        <IfModule !mod_authz_core.c>
            Order allow,deny
            Allow from all
        </IfModule>
</Directory>
</VirtualHost>

在这两种情况下,更改都是添加第一行和最后十行。在指令中,您可能需要其他东西,Options例如AllowOverride

为了使所有这些工作正常,您还需要在主conf文件中添加更多内容,可能httpd.conf在某个目录中,可能是/etc/apache2/.需要的是在配置中包含这些新文件的指令。最简单的是,根据给定的路径,它看起来像这样:

Include sites-available/wasamar.com.ng.conf
Include sites-available/ts.wasamar.com.ng.conf

如果您只在站点可用目录中包含虚拟主机,那么您可以使用

Include sites-available/*.conf

这将包括放置在那里的任何 .conf 文件,因此您可以根据需要创建其他主机,而不必每次都向 httpd.conf 添加一行。

您可以学到很多东西,花几天时间阅读并重新阅读 Apache 文档在线的,或者可能包含在本地安装的 Apache 中。看起来似乎很多,但时间花得值。

相关内容