# Apache設定虛擬網域(Mac) ## 透過本機 hosts 自定義網域名稱:jasper.com hosts 路徑:/private/etc 開啟方式: * 複製到桌面後打開,改完之後再覆蓋回去 * 使用VSCODE編輯儲存 說明:新增本機IP及自定義的網域名稱至hosts檔案內,完成後儲存並重啟Apache Apache指令: 啟動-sudo apachectl start 停止-sudo apachectl stop 重啟-sudo apachectl restart Apache啟動後,可於瀏覽器輸入網域名稱或IP即可顯示It's Works!證實Apache啟動中 ### hosts: ![](https://i.imgur.com/54VfIKq.png) ## 本機 apache config 設定網域jasper.com及對應到clone下來的專案資料夾 httpd.conf 路徑:/private/etc/apache2 httpd.vhosts.conf 路徑:/private/etc/apache2/extra 開啟方式: * 複製到桌面後打開,改完之後再覆蓋回去 * 使用VSCODE編輯儲存 ### httpd.conf ![](https://i.imgur.com/1BTSj7s.png) 說明:打開註解之後會載入httpd.vhosts.conf,為了可讀性高將下列的程式碼設定在httpd.vhosts.conf or 也可以把下列的程式碼輸入在httpd.conf內,就不需要打開Include /private/etc/apache2/other/*.conf的註解,預期結果相同 ``` <VirtualHost *:80> DocumentRoot "資料夾目錄/路徑" ServerName 對應hosts內的網域名稱 <directory "資料夾目錄/路徑"> Options Indexes FollowSymLinks Includes execCGI AllowOverride All Require all granted </directory> </VirtualHost> ``` ### httpd.vhosts.conf ![](https://i.imgur.com/FQUBQ5P.png) 說明:設定完成後,記得Apache需重啟,就可以開始測試是否設定成功,在對應的資料夾建立一支index.html並建置內容Hello World,然後於瀏覽器輸入網域名稱或IP,預期顯示Hello World ### 輸入網域名稱: ![](https://i.imgur.com/yMfmPtU.png) ### 輸入IP: ![](https://i.imgur.com/RbwSZ8m.png)