Httpd

將 httpd.conf 從一個項目重新配置為本地伺服器上的三個項目

  • March 9, 2019

問題

我需要正確配置我httpd.conf的 apache2 伺服器以將其從一個項目(http://localhost/)更改為三個項目。你會這麼好心並檢查以下程式碼和描述並幫助我進行正確的重新配置嗎?

目前一個項目目錄模型:

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)

因為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 中。

引用自:https://serverfault.com/questions/957439