如何 在 Apache 上 从 特定 路径 为 多 个 网站 提供 服务 ?

11dmarpk  于 2022-11-16  发布在  Apache
关注(0)|答案(1)|浏览(148)

我有两个项目- site_a和site_B -我希望将它们托管在运行Apache的Ubuntu服务器上。我对为它们设置单独的域不感兴趣。
除了每个项目的public/目录之外,每个项目都包含我不想公开访问的文件,因此,我将项目存储在/home而不是/var/www中。
我试过在两个项目的.conf文件中指定ServerName,在apache2.conf文件中指定<Directory>,但不确定如何实现。目前,每当我在浏览器中访问{domain}/时,都会显示site_a/public的内容。
以下是我目前尝试的方法:
site_a.配置文件:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName localhost/site_a
    DocumentRoot /home/site_a/public

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

site_B.配置文件:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName localhost/site_b
    DocumentRoot /home/site_b/public

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

apache2.conf:

<Directory /home/site_a>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<Directory /home/site_b>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
ia2d9nvy

ia2d9nvy1#

由于您希望两个站点在同一个域上响应,因此您只能有1个VirtualHost。如果您只有1个VirtualHost,Apache就无法知道基于域的站点A和站点B是什么。

一个域,2个站点

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com

    documentRoot "YOUR_DIRECTORY_PATH/apache/htdocs"
    DirectoryIndex index.html

    ErrorLog "logs/80_error_log"
    CustomLog "logs/80_access_log" combined
</VirtualHost>

按如下方式设置文件:

  • 在您的目录路径/apache/htdocs下
  • mkdir site_a:在此处放置站点a的文件
  • mkdir site_b:在此放置站点B的文件
    一个域,2个端口
<VirtualHost *:80>
    SITE A CONFIG
</VirtualHost>

<VirtulaHost *:81>
    SITE B CONFIG
</VirtualHost>

要查看站点a:http://example.com/
要查看站点B:http://example.com:81/

相关问题