Apache-Virtualhost

如何設置虛擬主機指向兩個不同的目錄

  • January 21, 2017

我有一個域和一個子域,它們應該指向兩個不同的文件夾,我已經嘗試過這個幫助,但我仍然有問題。(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 info@wasamar.com.ng
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 info@wasamar.com.ng
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 文件的pastebin http://pastebin.com/dnDfB21y

我如何實現這一目標?

據我所知,存在一些問題,而且可能更多。對於初學者,雖然您已經告訴 Apache 在哪裡可以找到每台伺服器的根目錄,但您還沒有授予 Apache 從這些目錄“提供”文件的權限。您也沒有告訴 Apache 這些是虛擬主機。您還需要對虛擬主機 conf 文件執行其他操作,但至少您需要授予 Apache 權限以伺服器目錄中的文件,並且您需要告訴 Apache 這是一個虛擬主機定義。僅針對此最小值進行了修改,這兩個文件如下所示:

這是的虛擬主機文件(/etc/apache2/sites-available/wasamar.com.ng.conf):

<VirtualHost *:80>

ServerName wasamar.com.ng

ServerAlias www.wasamar.com.ng

ServerAdmin info@wasamar.com.ng
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 info@wasamar.com.ng
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 中。可能看起來很多,但時間花得很好。

引用自:https://unix.stackexchange.com/questions/339041