每次重裝系統如何配置都上網找,搞半天,都是不對的,還不如自己記下來,以作參考呢。
我的項目目錄是 /home/feiffy/demo/test/public,映射的域名是 test.com,這樣在瀏覽器輸入 test.com 就可以直接打開我的項目啦。
下面是配置的步驟:
1.先保證自己項目目錄存在啊:mkdir /home/feiffy/demo/test
2.進入apache的配置文件目錄:cd /etc/apache2/sites-available/
3.建立一個以[域名.conf]為名的配置文件:sudo gedit test.com.conf
4. 輸入以下代碼並保存:
<VirtualHost *:80>
# 設置域名
ServerName test.com
# 設置項目目錄
DocumentRoot /home/feiffy/demo/test/public
# 設置目錄權限
<Directory /home/feiffy/demo/test/public>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
5. 啟用配置:sudo a2ensite test.com
6. 重啟 apache2:sudo service apache2 reload
7. 打開host文件:sudo gedit /etc/hosts
添加一行:127.0.0.1 test.com
8. 最后在瀏覽器輸入 test.com,就打開了主頁文檔 index.html,測試成功
[問題]
1. 打開瀏覽器輸入 test.com/index.html 出現:"Forbidden You don't have permission to access /index.html on this server".
這是沒有權限的問題,設置項目目錄權限即可
chown www-data -R /home/feiffy/demo/test/public
PS - 個人博客原文:[Ubuntu apache2.4 設置虛擬主機](https://feiffy.cc/ubuntu-apache2-4-vhost)