如何在 macOS 12 Monterey 上設置 MAMP本地服務器


  Apple 於 2021 年 10 月 25 日發布了 2021 年最新的 macOS 12 Monterey。今年又到了,如果您從 macOS 11 Big Sur 或更舊版本升級,您可能需要重新配置 macOS。

或者,如果您有一台裝有 macOS 12 Monterey 的全新 Mac,以下配置步驟應該可以幫助您在 macOS 平台上設置本地 Web 服務器。

在 macOS 12 Monterey 上,系統內置了 Apache。但是,當您查看 Apache 內置的 httpd.conf 時,您會注意到有一行說:

#PHP was deprecated in macOS 11 and removed from macOS 12

 

從 macOS 12 Monterey 開始,由於沒有內置 PHP,如果您想設置 MAMP(macOS、Apache、MySQL、PHP)堆棧,我建議使用 Apache 的 Homebrew 版本以及 PHP。

禁用 macOS 內置 Apache

由於我們不會使用 macOS 12 內置版本的 Apache,如果您的內置 Apache 正在運行,請發出以下命令來停止 Apache 服務。

打開終端Terminal並輸入:

sudo apachectl stop

安裝Homebrew

您需要做的第一件事是將 Homebrew 安裝到您的 macOS 12 Monterey 系統(如果您還沒有安裝 Homebrew)。

檢查您是否安裝了 Homebrew 或知道安裝的 Homebrew 版本

打開Terminal並輸入brew -v

Homebrew 3.3.0 
Homebrew/homebrew-core(git 修訂版 359f9f16171;最后一次提交 2021-10-25)

這意味着您已經安裝了 Homebrew。如果您沒有看到以上內容,請按照以下步驟在您的系統中安裝 Homebrew。

轉到https://brew.sh/

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"


打開Terminal並粘貼上面的命令。

注意:如果您沒有安裝 Xcode 的命令行工具,Homebrew 安裝程序將首先下載並安裝 Xcode 的命令行工具,然后它將繼續完成 Homebrew 到您的系統的安裝。

導出環境路徑

當您嘗試發出 brew 命令時,您可能會收到以下錯誤。

zsh: command not found: brew

如果出現上述錯誤,則必須通過發出以下命令來導出路徑。

export PATH="/opt/homebrew/bin:$PATH"

打開Terminal並鍵入以下命令,然后按 Enter。

echo 'export PATH="/opt/homebrew/bin:$PATH"' >> $HOME/.zshrc

然后鍵入以下命令並按 Enter。

關閉Terminal並重新打開一個新Terminal窗口。

 

通過 Homebrew 安裝 Apache

安裝 Homebrew 並正確設置導出后,下一步是使用 Homebrew 安裝 Apache。

打開 Terminal

輸入 brew install httpd

配置 Homebrew 版本的 Apache(httpd.conf 文件)

打開 Terminal

輸入 cd /opt/homebrew/etc/httpd/

鍵入 sudo cp httpd.conf httpd.conf.bak並按 Enter(如果您想保留原始配置文件的副本,則此步驟是可選的。)

輸入 sudo nano httpd.conf,進行編輯

啟用模塊和配置

您需要啟用(取消注釋該行)並修改/更新httpd.conf文件中的配置

要啟用模塊,首先,您需要找到要啟用的模塊。

用於調control + w出搜索功能,查找以下模塊並確保取消注釋。(刪除#每行前面的。)

 

LoadModule authn_core_module lib/httpd/modules/mod_authn_core.so
LoadModule authz_host_module lib/httpd/modules/mod_authz_host.so
LoadModule userdir_module lib/httpd/modules/mod_userdir.so
LoadModule include_module lib/httpd/modules/mod_include.so
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
Include /opt/homebrew/etc/httpd/extra/httpd-userdir.conf

修改 DocumentRoot

使用control + w並搜索DocumentRoot注釋掉(放在#下面每一行的前面。

DocumentRoot "/Users/mymac/work/"
<Directory "/Users/mymac/work/">

    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks Multiviews
    MultiviewsMatch Any

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>ed

修改監聽端口

默認情況下,它將偵聽 port 8080如果要監聽端口80,修改Listen 8080Listen 80.

使用Control + w並搜索Listen 8080然后替換為Listen 80

在您的用戶名下創建站點文件夾

由於我們將 DocumentRoot 定義為/Users/mymac/work/

我們需要Sites在用戶“ developer創建這個“ ”文件夾

打開Finder並導航到用戶的文件夾。

創建新文件夾並將其命名為“ work

 

通過 Homebrew 安裝 PHP

打開 Terminal

輸入 brew install PHP

等待安裝完成。

修改 httpd.conf 以在 Apache 上啟用 PHP

打開終端

輸入 cd /opt/homebrew/etc/httpd/

輸入 sudo nano httpd.conf 編輯

添加以下幾行。

 

LoadModule php7_module /usr/local/Cellar/php@7.1/7.1.33_4/lib/httpd/modules/libphp7.so 
# 注意這里要根據你具體的php路徑,如果是7.x 中間php7_module,如果是8.x,則php_module,后面也是libphp.so
<FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>

使用Control + w並搜索DirectoryIndex,然后添加index.php

 

創建 phpinfo.php 文件

phpinfo.php 文件將用於檢查 PHP 是否正常工作。

打開 Terminal

輸入 cd ~/work

類型 sudo nano phpinfo.php

打開 nano 應用程序后,添加以下行。

<?php phpinfo(); ?>

然后按control + o保存然后control + x退出nano應用程序。

 

啟動Apache服務器

完成Apache和PHP的配置后,讓我們啟動Apache服務器。

打開 Terminal

輸入 brew services restart httpd

要檢查 Apache 服務是否正在運行,請鍵入 brew services list

你應該看到類似的東西:

httpd 啟動開發人員 /Users/developer/Library/LaunchAgents/homebrew.mxcl.httpd.plist
// 注:如果php顯示none,則表示php沒開啟,開啟方法: brew services restart php@7.1 根據自己的版本號調整,關閉stop、重啟restart

如果您看到“已啟動”狀態,則表示 Apache 服務器已啟動並正在運行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM