人世的風(fēng)雪,不是說(shuō)停就停,如同命運(yùn)的選擇,并非盡由人意。
————宮無(wú)后

初接觸前端,為了在手機(jī)上預(yù)覽自己的頁(yè)面,一個(gè)本地服務(wù)器會(huì)方便很多,今天介紹一下MacOS系統(tǒng)自帶的Apache服務(wù)器;
1.先記住這三個(gè)終端命令
//開(kāi)啟apache: sudo apachectl start
//重啟apache: sudo apachectl restart
//關(guān)閉apache: sudo apachectl stop
打開(kāi)終端輸入sudo apachectl start回車(chē)會(huì)提示輸入密碼,就是電腦密碼,然后打開(kāi)瀏覽器http://127.0.0.1/或者http://localhost測(cè)試一下;
It works!
表明服務(wù)器已經(jīng)成功啟動(dòng)了;
2.找到本地服務(wù)器文件夾位置
終端輸入open /Library/WebServer/Documents,打開(kāi)文件夾;

接下來(lái)就可以在該路徑下創(chuàng)建文件并訪問(wèn)了;
3.修改本地服務(wù)器文件夾路徑
Mac自帶的服務(wù)器文件夾路徑使用起來(lái)不是很方便,設(shè)置默認(rèn)的自定義文件路徑會(huì)更好用;
終端輸入open /etc,打開(kāi)文件夾;

第一步打開(kāi)apache2,這是系統(tǒng)自帶的Apache目錄;打開(kāi)配置文件httpd.conf,找到
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
...
</Directory>
將路徑修改為
DocumentRoot "/Users/用戶名/WebServer"
<Directory "/Users/用戶名/WebServer">
...
</Directory>
WebServer是在用戶文件夾下新建的,修改完之后,在該配置文件中找到
#LoadModule php7_module libexec/apache2/libphp7.so
#LoadModule userdir_module libexec/apache2/mod_userdir.so
去掉注釋?zhuān)▌h除前面的#),并保存,保存時(shí)會(huì)提示你輸入管理員密碼;
第二步,打開(kāi)apache2文件下的users文件夾,找到用戶名.conf配置文件(沒(méi)有可以新建一個(gè))并打開(kāi)修改為
<Directory "/Users/用戶名/WebServer/">
Options Indexes MultiViews
AllowOverride All
Require all granted
</Directory>
保存退出;打開(kāi)瀏覽器http://127.0.0.1/或者http://localhost測(cè)試一下;
It works!
表明服務(wù)器已經(jīng)成功啟動(dòng)了;
可以把創(chuàng)建的本地服務(wù)器文件夾拖入Finder中,使用更方便一點(diǎn)

第三步可能會(huì)遇到403 Forbidden You don't have permission to access / on this server.

本人小白,具體原因不清楚,只能把修改的配置文件內(nèi)容貼出來(lái)了
httpd.conf
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/Users/用戶名/WebServer"
<Directory "/Users/用戶名/WebServer">
Options FollowSymLinks Multiviews
MultiviewsMatch Any
AllowOverride None
Require all granted
</Directory>
用戶名.conf
<Directory "/Users/wsk/WebServer/">
Options Indexes MultiViews
AllowOverride All
Require all granted
</Directory>
終端輸入 ifconfig 查看自己電腦的 ip 地址,和電腦處于同一個(gè)局域網(wǎng)下的設(shè)備就可以通過(guò)這個(gè)ip地址訪問(wèn) /Users/用戶名/WebServer 目錄下的資源;