蘋果環境

Apache

本機 Web 伺服器

❗️ 若電腦已採用 Docker 容器化環境,可不用安裝 Apache 服務。 安裝時需注意自己電腦內的位置,以下為 OA 本人的設定值,請注意自己的路徑是否正確。

安裝步驟

  • 更新 Xcode command linexcode-select --install
  • 更新 Brewbrew update
  • 安裝 opensslbrew install openssl
  • 移除停止使用 mac 原生的 Apache
    • 先暫停 Apache:sudo apachectl stop
    • 關閉自動開啟:sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
  • 安裝新的 Apache
    • 用 Brew 安裝brew install httpd
    • 啟動 Apachebrew services start httpd
    • 設定:ps -aef | grep httpd
    • 啟動伺服器:brew services start httpd

      啟動如有發生錯誤是正常現象,後續步驟會修正。

設定 httpd.conf

位置在 /opt/homebrew/etc/httpd/httpd.conf;因採 Homebrew 安裝,路徑可寫成 $(brew --prefix)/etc/httpd/httpd.conf。設定步驟:

  • 設定方法兩種擇一:
    • 快速(一行):sudo chmod 777 $(brew --prefix)/etc/httpd/httpd.conf && ln -s $(brew --prefix)/etc/httpd/httpd.conf ~/Documents/httpd.conf && subl $(brew --prefix)/etc/httpd/httpd.conf
    • 分步:
      • 為避免每次修改都要輸密碼,直接將權限開到最大:sudo chmod 777 $(brew --prefix)/etc/httpd/httpd.conf
      • 建立連結方便快速找到:ln -s $(brew --prefix)/etc/httpd/httpd.conf ~/Documents/httpd.conf
      • 用 Sublime Text 編輯:subl $(brew --prefix)/etc/httpd/httpd.conf
  • 修改
    • 搜尋 Listen 8080 改為 Listen 80
    • 搜尋 DocumentRoot "/opt/homebrew/var/www" 改為 DocumentRoot "/Users/oa/Workspace"
    • 搜尋 <Directory "/opt/homebrew/var/www"> 改為 <Directory "/Users/oa/Workspace">
    • 搜尋 mod_rewrite.so,拿掉 LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so 前方的 # 註解
    • 搜尋 ServerName,拿掉前方的 # 註解,並改成 ServerName localhost
    • 搜尋 httpd-vhosts,拿掉 Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf 前方的 # 註解

設定 Host

位置在 /etc/hosts。設定步驟:

  • 設定方法兩種擇一:

    • 快速(一行):sudo chmod 777 /etc/hosts && ln -s /etc/hosts ~/Documents/hosts && subl /etc/hosts
    • 分步:
      • 為避免每次修改都要輸密碼,直接將權限開到最大:sudo chmod 777 /etc/hosts
      • 建立連結方便快速找到:ln -s /etc/hosts ~/Documents/hosts
      • 用 Sublime Text 編輯 hostssubl /etc/hosts,調整內容
  • 修改內容參考:

    127.0.0.1    dev.case.ioa.tw

設定 Virtual Host

位置在 /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf;因採 Homebrew 安裝,路徑可寫成 $(brew --prefix)/etc/httpd/extra/httpd-vhosts.conf。設定步驟:

  • 設定方法兩種擇一:

    • 快速(一行):sudo chmod 777 $(brew --prefix)/etc/httpd/extra/httpd-vhosts.conf && ln -s $(brew --prefix)/etc/httpd/extra/httpd-vhosts.conf ~/Documents/httpd-vhosts.conf && subl $(brew --prefix)/etc/httpd/extra/httpd-vhosts.conf
    • 分步:
      • 為避免每次修改都要輸密碼,直接將權限開到最大:sudo chmod 777 $(brew --prefix)/etc/httpd/extra/httpd-vhosts.conf
      • 建立連結方便快速找到:ln -s $(brew --prefix)/etc/httpd/extra/httpd-vhosts.conf ~/Documents/httpd-vhosts.conf
      • 用 Sublime Text 編輯 httpd-vhosts.confsubl $(brew --prefix)/etc/httpd/extra/httpd-vhosts.conf,調整內容,常用內容如下
  • 修改內容參考:

    # root
    <VirtualHost *:80>
      ServerAdmin oawu.tw@gmail.com
      DocumentRoot "/Users/oa/Workspace"
      <Directory "/Users/oa/Workspace">
          Options FollowSymLinks MultiViews
          AllowOverride All
          Order allow,deny
          Allow from all
      </Directory>
    </VirtualHost>
    
    # Case
    <VirtualHost *:80>
      ServerName dev.case.ioa.tw
      ServerAlias dev.case.ioa.tw
      DocumentRoot "/Users/oa/Workspace/case"
      <Directory "/Users/oa/Workspace/case">
          Options FollowSymLinks MultiViews
          AllowOverride All
          Order allow,deny
          Allow from all
      </Directory>
    </VirtualHost>

指令

完成上述步驟後,記得執行 brew services restart httpd。本次安裝的伺服器相關指令:

  • 關閉:brew services stop httpd
  • 開啟:brew services start httpd
  • 重啟:brew services restart httpd

設定 https 的 SSL 功能

❗️ SSL 功能一般來說不太會用到,不一定要安裝;不需要的話直接跳過。

本機開發若需 https,可藉由 openssl 輔助,方法如下:

  • 編輯 Conf:subl $(brew --prefix)/etc/httpd/httpd.conf

  • 將以下三個註解拿掉:

    • LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
    • LoadModule ssl_module lib/httpd/modules/mod_ssl.so
    • Include /opt/homebrew/etc/httpd/extra/httpd-ssl.conf
  • 編輯 SSL Conf:subl $(brew --prefix)/etc/httpd/extra/httpd-ssl.conf

  • 搜尋 Listen 8443 改為 Listen 443

  • 搜尋 改為 <VirtualHost _default_:443>

  • 搜尋 DocumentRoot "/opt/homebrew/var/www",在前方加上 # 註解

  • 搜尋 **ServerName www.example.com:8443**,在前方加上 # 註解

  • 編輯 vhost:subl $(brew --prefix)/etc/httpd/extra/httpd-vhosts.conf

    <VirtualHost *:443>
        DocumentRoot "/Users/oa/Workspace"
        ServerName localhost
        SSLEngine on
        SSLCertificateFile "/opt/homebrew/etc/httpd/server.crt"
        SSLCertificateKeyFile "/opt/homebrew/etc/httpd/server.key"
    </VirtualHost>
  • 第一次設定需產生 key,所以要執行 openssl

  • 進入目錄:cd $(brew --prefix)/etc/httpd

  • 產生 ssl key:openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt

    • 中間要輸入資料,因為是本機開發,簡單輸入即可
  • 測試設定:sudo apachectl configtest

  • 重新啟動:brew services restart httpd

重點整理

  • Conf 位置:$(brew --prefix)/etc/httpd/httpd.conf
  • Vhosts 位置:$(brew --prefix)/etc/httpd/extra/httpd-vhosts.conf
  • 開啟伺服器:brew services start httpd
  • 關閉伺服器:brew services stop httpd
  • 重啟伺服器:brew services restart httpd

相關參考