如題,升級到最新系統MacOS12 Monterey后,網上很多資料已經不適合用了,因此花費了很長時間進行一一測試。
一、曾經嘗試的方法(可能是本人配置問題吧):
1、Mac自帶Apache,但是最新的MacOS12已經不支持內置PHP了,安裝PHP修改配置依舊無效
2、安裝brew,通過brew來重新安裝httpd Apache服務器,再安裝PHP環境,最后修改配置到自己工作目錄,全部啟動,依舊沒有運行成功,有興趣的同學可以嘗試,
二、符合本人項目的虛擬機 - XAMPP(嘗試過小皮PHPstudy MAc版,配置無效)
下載對應的PHP版本哦,低PHP版本好操作,網上教程多是低PHP版本,適合新人。之前盲目下載最新版本導致一度配置后無法訪問問題。
因為項目最低PHP版是7.1,因此我找打7.1.30版本MAc下載,雖然界面老了一點,但是沒很實用,不需要全開,只需要開啟apache服務就可,開啟時可以實時看日志,server Events 或 Log
這里面可以找到對應的PHP版本,點擊進去,不要點上面綠色下載按鈕,看列表中的安裝文件installer.dmg,可以看下載量,點擊下載,然后一直next安裝即可。
該低版本的XAMPP會在應用程序中新建一個XAMMP文件夾,里面就是相關的配置文件
快捷進入方法:
1、點擊訪達 -》 應用程序 -》找到文件夾 XAMPP -> etc (建議分欄模式,層級關系)
2、打開XAMPP軟件,第一頁Welcome,點擊右下方第二按鈕 open Application Folder,找到當前同級的etc即可
XAMPP第二頁面Manage Servers
選擇 Apache Web Server,選擇右邊Start,只需要開啟這即可
XAMPP第三頁面Server Events 是監控Manager Servers開啟關閉狀態日志,可以更加日志定位問題
修改配置
可以直接修改
etc中http.conf,建議自己先備份一份,也可以修改
etc -> extra中的httpd-xampp.conf文件,
這里說一下,
httpd-xampp-conf是對http.conf配置的覆蓋,目的是不想用戶直接修改http.conf原件,其實沒必要,可以直接修改隨意。修改的配置的網上很多
1、修改host
1.1、
打開終端輸入: sudo vim /etc/hosts
1.2、
按A,開始移動光標修改、添加,127.0.0.1 xx
1.3、
修改完成后,按一下ESC,然后按住shift + :(冒號),再輸入wq(保存退出)
注:也可以使用第三方插件,如utools、火絨,本人用utools,因為其插件真豐富,自己也會寫着玩,utools中搜hosts,安裝后在開發環境輸入127.0.0.1 xx,最后右鍵開發環境選擇應用此文件即可
2、修改虛擬機映射 https-vhosts.conf
應用程序 -》XAMPP -》etc -》extra -》https-vhosts.conf
最下面添加自己的項目
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/Users/myMac/work/test" ServerName test.com ErrorLog "logs/test.com-error_log" CustomLog "logs/test.com-access_log" common
</VirtualHost>
3、修改 httpd-conf
3.1、引入httpd-vhosts.conf
找到 Virtual hosts,去掉下面一行的 #
# Virtual hosts
Include etc/extra/httpd-vhosts.conf
3.2、修改用戶,防止403報錯,沒有權限
User xiaoming // 這是xiaoming表示是自己的用戶名,不能有空格,如果格式不對,請修改自己用戶名,可自行百度Mac修改用戶名即可 Group daemon
3.3 如果直接修改httpd-conf,請在最后一行中,即 # XAMPP 下面引入的覆蓋文件注釋掉,前面加#即可
# XAMPP Include etc/extra/httpd-xampp.conf # Include /Applications/XAMPP/xamppfiles/apache2/conf/httpd.conf
3.4、修改DcumentRoot,路徑和相關配置
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# blocks below.
#
AllowOverride All
Require all granted
Order deny,allow
Allow from all
DocumentRoot "/Users/work/test" <directory " users="" work="" test"> <="" directory <="" div=""> # # 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/trunk/mod/core.html#options # for more information. # #Options Indexes FollowSymLinks # XAMPP Options Indexes FollowSymLinks ExecCGI Includes # # AllowOverride controls what directives may be placed in .htaccess files. # It can be All, None, or any combination of the keywords: # Options FileInfo AuthConfig Limit # #AllowOverride None # since XAMPP 1.4: AllowOverride All # # Controls who can get stuff from this server. # Require all granted Order deny,allow Allow from all
3.5、配置完這些,在XAMPP第二頁面Manage Servers,選中apache web server,點擊restart即可,如果有報錯,請看第三頁面Server Events的日志,哪個地方配置問題
