在本地服务器上将 httpd.conf 从一个项目重新配置为三个项目

在本地服务器上将 httpd.conf 从一个项目重新配置为三个项目

问题

我需要正确配置我的httpd.confapache2 服务器以便从一个项目更改它(http://本地主机/) 到三个项目。您能否检查以下代码和说明并帮助我进行正确的重新配置?

当前一个项目目录模型:

Library
+++ WebServer

+++ +++ Documents
+++ +++ database

所需的三项目目录模型:

Library
+++ WebServer

+++ +++ project_1
+++ +++ +++ public_html
+++ +++ +++ database

+++ +++ project_2
+++ +++ +++ public_html
+++ +++ +++ database

+++ +++ project_3
+++ +++ +++ public_html
+++ +++ +++ database

无障碍设施

一个项目:

三个项目:

与某项目相关的部分httpd.conf(目前负责某项目):

DocumentRoot "/Library/WebServer/Documents"

<Directory "/Library/WebServer/Documents">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>

httpd.conf多个项目的建议部分:

<VirtualHost>
   ServerAdmin localhost
    DocumentRoot "/Library/WebServer/project_1/public_html"
    ServerName localhost
</VirtualHost>  

<Directory "/Library/WebServer/project_1/public_html">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>


<VirtualHost>
   ServerAdmin localhost
    DocumentRoot "/Library/WebServer/project_2/public_html"
    ServerName localhost
</VirtualHost>  

<Directory "/Library/WebServer/project_2/public_html">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>


<VirtualHost>
   ServerAdmin localhost
    DocumentRoot "/Library/WebServer/project_3/public_html"
    ServerName localhost
</VirtualHost>  

<Directory "/Library/WebServer/project_3/public_html">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>

服务器版本:Apache/2.4.28 (Unix)

答案1

因为url路径部分和库结构不匹配,所以要进行欺骗。

技巧 1:每个项目都有虚拟主机和本地专用域。

技巧 2:在虚拟主机中使用别名(或者不使用 VirtualHost)

技巧 1 示例:

<VirtualHost *:80>
   ServerAdmin localhost
    DocumentRoot "/Library/WebServer/project_1/public_html"
    ServerName project1.emma

    <Directory "/Library/WebServer/project_1/public_html">
        Options Indexes FollowSymLinks MultiViews
        MultiviewsMatch Any
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost> 

<VirtualHost *:80>
   ServerAdmin localhost
    DocumentRoot "/Library/WebServer/project_2/public_html"
    ServerName project2.emma

    <Directory "/Library/WebServer/project_2/public_html">
        Options Indexes FollowSymLinks MultiViews
        MultiviewsMatch Any
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
   ServerAdmin localhost
    DocumentRoot "/Library/WebServer/project_3/public_html"
    ServerName project3.emma

    <Directory "/Library/WebServer/project_3/public_html">
        Options Indexes FollowSymLinks MultiViews
        MultiviewsMatch Any
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

ServerName 很重要,这样你就可以在浏览器中通过 ServerName 访问该项目了:http://project1.emma。为了使其正常工作,您应该在 /etc/hosts 文件中添加以下行:

127.0.0.1 project1.emma project2.emma project3.emma

ServerName 可以是任何东西。Tld 可以是任何东西。重要的是要在 hosts 文件中,并且在浏览器中输入时,始终使用协议前缀 (http://)

技巧 2 示例:

您不需要 VirtualHost 来执行此操作。

Alias /project1 /Library/WebServer/project_1/public_html
Alias /project2 /Library/WebServer/project_2/public_html
Alias /project3 /Library/WebServer/project_3/public_html

<Directory "/Library/WebServer/project_1/public_html">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>

<Directory "/Library/WebServer/project_2/public_html">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>

<Directory "/Library/WebServer/project_3/public_html">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>

我喜欢技巧 1。它更加简洁,并迫使我将所有内容组织到 VirtualHost 中。

相关内容