如何在 httpd.conf 中添加第二个站点

How to add a second site in httpd.conf

我在我的 CentOs 7 Apache 服务器中使用 httpd.conf 的以下配置到 运行 "site1":

ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
ServerName locahost:80
<Directory />
    AllowOverride none
    Require all denied
</Directory>
DocumentRoot "/var/www/html/site1"
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
<Directory "/var/www/html/site1">
        Options FollowSymLinks
        AllowOverride all
        Order allow,deny
        Allow from all
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf

当我访问“http://localhost”时,site1 及其 subpages/subfolders 正常工作。 我现在希望能够显示第二个网站:访问“http://localhost/site2”时,我想显示保存在“/test”下的文件 "test.html" 的内容;我应该如何编辑 httpd.conf 才能使其正常工作?

那不是一个不同的站点,它是一个不同的目录。不同的站点将涉及不同的主机名。

因为您已经有 DocumentRoot "/var/www/html/site1 并且您似乎不想创建新的虚拟主机,您可以使用 Alias

指向 new/different 目录

此外,您只希望 site2 在 /test/ 下加载 test.html 如果我在访问时理解正确,只需向其添加正确的 DirectoryIndex 指令即可。

这里是:

Alias /site2 /var/www/html/site2
<Directory /var/www/html/site2>
     DirectoryIndex /test/test.html
</Directory>

为此,您需要以下两个模块:

  • mod_alias
  • mod_dir(但您可能已经有了这个,因为您已经在使用 DirectoryIndex 指令)

您也可以在 site1 下 mkdir site2,但这看起来更干净。