Mac系統自帶apache服務器
-
查看apache版本 sudo apachectl -v 啟動apache sudo apachectl start 重啟apache sudo apachectl restart
- 配置apache
apache的主配置文件在路徑/etc/apache2/下- 修改httpd.conf 文件
-
備份原來的文件
sudo cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.backup -
修改主配置文件
$vi /etc/apache2/httpd.conf -
主要修改內容
//181行 User _www Group _www //改為(rootname為本機用戶名) User rootname Group wheel //219行 <Directory /> AllowOverride none Require all denied </Directory> //改為(修改apache配置:設置訪問代理資源被默認不受限制) <Directory /> Require all granted AllowOverride all </Directory> //498行 # Virtual hosts #Include /private/etc/apache2/extra/httpd-vhosts.conf //改為(去掉前面的#,這樣就開啟了httpd-vhosts虛擬主機文件) # Virtual hosts Include /private/etc/apache2/extra/httpd-vhosts.conf
-
小技巧
如果你不會使用vi編輯器,沒關系- 打開Finder
- 按快鍵盤 Command + Shift + G調出前往文件夾
- 輸入/etc/apache2即可看到apache配置文件,用自己熟悉編輯器打開httpd.conf就可以求改了,保存的時候需要輸入root密碼
-
- 修改httpd.conf 文件
- 設置虛擬主機
apache的默認的根目錄在/Library/WebServer/下,配置虛擬主機后可以不用理會默認的網站根目錄,根據自己的需要在合適的地方建立不同的網站目錄- 修改httpd-vhosts.conf文件,文件位置在/etc/apache2/extra/
- 備份原來的文件
sudo cp /etc/apache2/extra/httpd-vhosts.conf /etc/apache2/extra/httpd-vhosts.conf.backup - 修改主配置文件
$sudo vi /etc/apache2/extra/httpd-vhosts.conf - 主要修改內容
//在文件里加入,前面的例子可以用# 注釋掉 <VirtualHost *:80> DocumentRoot "項目文檔根目錄" ServerName 服務器名稱 ErrorLog "/private/var/log/apache2/mysites-error_log" CustomLog "/private/var/log/apache2/mysites-access_log" common <Directory "項目文檔根目錄"> Options FollowSymLinks Multiviews Indexes MultiviewsMatch Any AllowOverride None Require all granted </Directory> <Proxy *> Order deny,allow Allow from all </Proxy> // 配置請求轉發服務器 和 環境路徑(反向代理) ProxyPass /web http://example.com/web ProxyPassReverse /web http://example.com/web </VirtualHost>
- 備份原來的文件
- 修改httpd-vhosts.conf文件,文件位置在/etc/apache2/extra/
- 打開/etc/hosts文件,加入
127.0.0.1 你的網站地址 - 重啟Apache服務器
sudo apachectl restart - 打開瀏覽器輸入http://你的網站地址
apache設置用戶名密碼登錄驗證
實現效果
google 瀏覽器
Safari 瀏覽器
1.修改http.conf文件設置指定目錄的配置,例如設定test目錄的配置,增加驗證。
<Directory "/www/test"> Options Indexes AllowOverride AuthConfig Order allow,deny Allow from all </Directory>
2.在指定目錄下增加.htaccess文件
文件內容如下:
AuthName "sys" AuthType Basic AuthUserFile /www/test/.htpasswd require user admin
第一行,驗證提示信息。
第二行,驗證類別。
第三行,密碼文件所在路徑,絕對路徑。
第四行,指定登錄用戶名。指定admin用戶登錄。
3.增加.htpasswd文件
/usr/local/apache2/bin/htpasswd -c /www/test/.htpasswd admin // 如果加上 -b 選項可以直接在 admin 輸入用戶密碼,否則會在回車后提示輸入密碼
利用Apache附帶的程序htpasswd,生成包含用戶名和密碼的文本文件
輸入上面命令,htpasswd目錄寫自己的目錄,最后一個參數即為登錄賬號名,會提示輸入兩次密碼。
重啟apache。
訪問設定的地址,看下是否已經有apache的登錄驗證。