Macbook 本機 apache 虛擬主機和網站,多域名、多虛擬目錄,並且允許列舉所有文件和目錄

20190613 很久不再使用 apache 服務器,最近試試我的本機上的 apache2,發現又不能用了!

我希望在本機調試 php 程序,所以需要 apache 支持 php

爲了調試方便,我需要直接列舉虛擬主機上的所有文件夾,通過鼠標點擊選擇網站目錄和文件,操作簡單!反正也不會拿他當服務器使用!

先找到我自己以前的操作記錄

https://my.oschina.net/u/1440971/blog/824415
mac 系統升級之後,經常會發生一些變化,但是,以前的操作總是可以參考的!

參考資料:Apache 官網

http://httpd.apache.org/docs/2.4/vhosts/

*** 本次 mac PS 版本:mac OS Mojave 10.14.5

1、準備工作

  1. 直接瀏覽器上輸入 127.0.0.1
    It works!
    說明缺省虛擬主機配置是正確的!
    輸入 127.0.0.1/info.php ,結果一片空白!

  2. 通過 Finder ,設置我的虛擬目錄權限,全部可讀寫
    ** 缺省虛擬主機目錄 DocumentRoot “/Library/WebServer/Documents”
    ** 使用次數不多,本次不再單獨建立自己的虛擬主機目錄了

  3. 打開 Terminal 終端,查看php 和 apache2 信息
    $ php -v

    PHP 7.1.23 (cli) (built: Feb 22 2019 22:19:32) ( NTS )
    

    Copyright © 1997-2018 The PHP Group
    Zend Engine v3.1.0, Copyright © 1998-2018 Zend Technologies

    $ httpd -v

    Server version: Apache/2.4.34 (Unix)
    

    Server built: Feb 22 2019 20:20:11

    $ which httpd

    /usr/sbin/httpd
    

    $ httpd -S

    在這裏可以看到 DocumentRoot 、ErrorLog 等信息
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using dhbm-on-mac20180816.local. Set the 'ServerName' directive globally to suppress this message
    VirtualHost configuration:
    ServerRoot: "/usr"
    Main DocumentRoot: "/Library/WebServer/Documents"
    Main ErrorLog: "/private/var/log/apache2/error_log"
    Mutex default: dir="/private/var/run/" mechanism=default 
    Mutex mpm-accept: using_defaults
    PidFile: "/private/var/run/httpd.pid"
    Define: DUMP_VHOSTS
    Define: DUMP_RUN_CFG
    User: name="_www" id=70 not_used
    Group: name="_www" id=70 not_used
    

2、修改配置文件

  1. 打開 php 支持
    sudo vim /etc/apache2/httpd.conf
    找到以下 libphp7.so 模塊地方,去掉前面的註釋 #

     # by wzh 20190613 enable php7
     LoadModule php7_module libexec/apache2/libphp7.so
    
  2. 放開虛擬目錄權限
    sudo vim /etc/apache2/httpd.conf
    找到 < Directory /> 的地方,放開 AllowOverride 和 Require 限制

     <Directory />
    	 # by wzh 20190613
     # AllowOverride none
     # Require all denied
    
     allow from all
     AllowOverride All
     Require all granted
    

同樣找到
DocumentRoot “/Library/WebServer/Documents”
<Directory “/Library/WebServer/Documents”>
放開限制

# by wzh 20190613 AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted

# add by wzh 20190613 none ->all
 Options All
 AllowOverride All
 Allow from all
  1. 測試 php
    curl 127.0.0.1/info.php
    curl localhost/info.php
    瀏覽器測試
    http://localhost/info.php
    http://127.0.0.1/info.php

在這裏插入圖片描述
4. 測試虛擬目錄
http://localhost/
http://127.0.0.1/
在這裏插入圖片描述

*** mac OS 升級經常會造成 apache2 配置的一些變化,但是,感覺每次都會變的越來越簡單!比起以前折騰過的兩次,本次是最省事的一次!
*** 主要就是:1、加載 php 模塊 2、放開目錄權限 3、放開虛擬目錄權限
*** 也許有些變化在上一次折騰的時候已經做過了!

3、新建本地虛擬目錄和網站

  1. 增加一個虛擬目錄
    sudo vim /etc/apache2/httpd.conf

    ** 本次爲了簡化,直接在缺省虛擬目錄下新建子目錄test123,作爲測試網站站點的目錄
    ** 每增加一個站點,就要先增加一個目錄(我這裏建立了 2 個)

     # add by wzh 20190613 new dir for other website:test123.wzh
     # for vhost test
     DocumentRoot "/Library/WebServer/Documents/test123"
     <Directory "/Library/WebServer/Documents/test123">
         Require all granted
     
      # by wzh 20190613 none ->all
          Options All
          AllowOverride All
          Allow from all
     
     </Directory>
     
     # add by wzh 20190613 new dir for other website : tp123.wzh
     # for ThinkPHP test on 8080 port
     DocumentRoot "/Library/WebServer/Documents/tp123"
     <Directory "/Library/WebServer/Documents/tp123">
         Require all granted
     
      # by wzh 20190613 none ->all
          Options All
          AllowOverride All
          Allow from all
     
     </Directory>
    
  2. 增加一個虛擬站點
    $ sudo vim /etc/apache2/extra/httpd-vhosts.conf
    *** 在文件最後增加以下 2 個測試站點

     # add by wzh 20190613
     <VirtualHost *:80>
         ServerAdmin [email protected]
         DocumentRoot "/Library/WebServer/Documents/test123/"
         ServerName test123.wzh
         ErrorLog "/private/var/log/apache2/test123-error_log"
         CustomLog "/private/var/log/apache2/test123-access_log" common
     </VirtualHost>
    
     # add by wzh 20190613
     <VirtualHost *:80>
         ServerAdmin [email protected]
         DocumentRoot "/Library/WebServer/Documents/test123/"
         ServerName test123.wzh
         ErrorLog "/private/var/log/apache2/test123-error_log"
         CustomLog "/private/var/log/apache2/test123-access_log" common
     </VirtualHost>
     
     # add by wzh 20190613
     <VirtualHost *:8080>
         ServerAdmin [email protected]
         DocumentRoot "/Library/WebServer/Documents/tp123/"
         ServerName tp123.wzh
         ErrorLog "/private/var/log/apache2/tp123-error_log"
         CustomLog "/private/var/log/apache2/tp123-access_log" common
     </VirtualHost>
    

3.增加一條本地映射
** 僅供本地測試用,外網是找不到這 2 個域名的
$ sudo vim /etc/hosts
在最後增加 2 條記錄

	# add by wzh 20190623
	127.0.0.1       test123.wzh
	127.0.0.1       tp123.wzh
  1. 增加一個本地 8080 端口 listen
    $ sudo vim /etc/apache2/extra/httpd-vhosts.conf

    ** 如果不用80 端口之外的其他端口,可以忽略這一步
    ** 找到以下位置,增加 2 條自己的 listen

     #Listen 12.34.56.78:80
     <IfDefine SERVER_APP_HAS_DEFAULT_PORTS>
         Listen 8080
     </IfDefine>
     <IfDefine !SERVER_APP_HAS_DEFAULT_PORTS>
         Listen 80
     </IfDefine>
     
     # add by wzh 20190613
      Listen 127.0.0.1:80
      Listen 127.0.0.1:8080
    
  2. 重啓生效
    $ sudo apachectl restart

4、測試一下

** 爲了簡化,先去關閉mac OS 防火牆
** mac OS 防火牆 從以前的 ipfw 改到現在的 pf
** 目前爲止我也不知道怎麼開放指定端口

ping tp123.wzh
ping test123.wzh

$ curl test123.wzh
$ curl test123.wzh/index.php

$ curl test123.wzh:8080
$ curl test123.wzh:8080/index.php
** 如果發現錯誤 curl: (7) Failed to connect to tp123.wzh port 8080: Connection refused
** 請參照上 一步:增加一個本地 8080 端口 listen

$ curl tp123.wzh
$ curl tp123.wzh/index.php

$ curl tp123.wzh:8080
$ curl tp123.wzh:8080/index.php

瀏覽器測試
http://test123.wzh/
http://test123.wzh/index.php

http://tp123.wzh/
http://tp123.wzh/index.php
http://test123.wzh:8080/
http://test123.wzh:8080/index.php

以上結果僅貼圖一個爲例

5、配置多虛擬目錄、域名支持

參考
https://www.cnblogs.com/maowenqiang/p/5120767.html
寫得非常清楚,這裏再抄寫一次

cd /etc/apache2
sudo vim httpd.conf

# by wzh 20190628 啓用apache的虛擬主機功能
# LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so

我這裏沒有打開這個!直接所有域名網站都放在 httpd-vhosts.conf 裏面

# Virtual hosts
# by wzh 20190628 多域名支持
Include /private/etc/apache2/extra/httpd-vhosts.conf

cd /etc/apache2/extra
sudo vim httpd-vhosts.conf
加上新創建的虛擬目錄對應的域名

# add by wzh 20190628 tp6.wzh
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/Users/dhbm/php20190628"
    ServerName tp6.wzh
    ErrorLog "/private/var/log/apache2/tp6--error_log"
    CustomLog "/private/var/log/apache2/tp6-access_log" common
</VirtualHost>

cd /etc
sudo vim hosts
加上剛剛創建的域名和虛擬主機

# add by wzh 20190628
127.0.0.1       tp6.wzh

重啓 apache2 生效
sudo apachectl restart

測試一下
http://tp6.wzh/tp/public/
在這裏插入圖片描述

6、錯誤處理

  1. 配置了多個虛擬目錄之後,總是全部指向最後一個
    按照以上 5、配置多虛擬目錄、域名支持
    cd /etc/apache2
    sudo vim httpd.conf
    放開以下 2 處 的 # 註釋

     	# by wzh 20190628 啓用apache的虛擬主機功能
     	# LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
    
     	# Virtual hosts
     	# by wzh 20190628 多域名支持
     	Include /private/etc/apache2/extra/httpd-vhosts.conf
    

    這樣的話,所有的網站都會到 /etc/apache2/extra/httpd-vhosts.conf 裏面去查找,按照 ServerName 對應響應的虛擬目錄

  2. 配置了vhost 支持之後,localhost 和 127.0.0.1 反而打不開了
    ping 127.0.0. 和 ping localhost 都不通,因爲 mac 開着防火牆?
    測試一下 apache 語法
    httpd -t
    顯示以下的錯誤

     AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using dhbm-on-mac20180816.local. Set the 'ServerName' directive globally to suppress this message
    

    百度以下,找到這個博客 https://blog.csdn.net/weixin_43245095/article/details/89556756

按照他的建議修改
sudo vim /etc/apache2/httpd.conf
找到 ServerName 修改如下

# add by wzh 20190629
# 解決 httpd -t 錯誤
ServerName 127.0.0.1:80

restart 之後,再次 測試
httpd -t

Syntax OK
  1. 查看 apache 狀態
    sudo apachectl status 顯示如下提示

     Go to http://localhost:80/server-status in the web browser of your choice.
    

Note that mod_status must be enabled for this to work.

參考 https://blog.csdn.net/wangkepermit/article/details/72954995
需要開啓status功能

我的理解:

找到以下 mod_status.so
LoadModule status_module libexec/apache2/mod_status.so

另外 httpd-info 的地方,缺省是註釋的
# Real-time info on requests and configuration
#Include /private/etc/apache2/extra/httpd-info.conf

可能涉及到安全性問題,目前暫時不需要,所以,這裏保持不動!

記錄一下簡單命令

啓動、停止 apache
sudo apachectl start
sudo apachectl stop
sudo apachectl restart
sudo apachectl status

檢查 httpd 語法
httpd -t

查看apache 加載了那些模塊
sudo apachectl -M
或 sudo apachectl -t -D DUMP_MODULES

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章