最近正在煩惱著該如何部署 Laravel 到專案的子目錄下,比如說,請求的網址:http://localhost/blog 而不是 http://localhost
使用 .hatcess 之後,還是無法設定成功,讓網址可以導向到請求專案的根目錄名稱一樣也可以成功。
目前也有人有這樣的問題,於是有人在 Laravel 的 Github 的專案上開了這樣的 issue: https://github.com/laravel/framework/pull/3918 問題也是跟我相同,需要請求網址的時候可以多一個專案根目錄名稱。
目前比較好的作法是使用:Apache2 的 VirtualHost 來解決這個問題,透過設定 VirtualHost 可以解決請求網址包含專案名稱的問題。下面的教學為使用 Apache2 VirtualHost 並搭配 Freenom 免費的頂級網域名稱來做到。
作業系統:LUbuntu 16.04 LTS
[Freenom 免費頂級網域申請]
- 進入 freenom.com 網站。
- 輸入一個沒有人註冊的網域名稱並按下『檢查可用性』,如下圖。

- 註冊或登入一個帳號,並成功拿到網域之後,進到如下的畫面:點選 My Domain 接著選擇 Manage Domain。

- 選擇 tab 名稱:Manage Freenom DNS

在『Add record』的部份新增一個 A 紀錄,紀錄 ipv4 位址。Name 空白以及在 Target 的地方,填寫自己的固定 ip 位址所以只適合 VPS 或實體有固定 ip 的主機。(TTL 數值可以用預設就好,不用修改它) - 填寫完成之後,按下 Save Changes 按鈕,等一段時間之後,就會出現『Record added successfully 』就代表成功加入一筆紀錄了。
- 增加完 A 紀錄之後,就可以知道使用網域名稱來 request url 而且不需要打 ip 位址了。
- 接著再新增 CNAME 紀錄,用來做子網域使用,讓外面連進來的人也可以使用子網域連線(搭配 VirtualHost 使用)。
- 新增 CNAME 紀錄範例如下圖:

[VirtualHost 設定]
- 需要先安裝好 Apache2 或是 LAMP server 可以參考之前的文章。
- 照下面新增一個目錄(以 blog.peter279k.tk)
1
sudomkdir-p/var/www/blog.peter279k.tk/html - 設定 permissions 權限,這裡可以將使用者權限設定成:www-data
1
sudochown-R www-data/var/www/blog.peter279k.tk/html - 編輯一個測試的設定 HTML 檔。
1
vi/var/www/blog.peter279k.tk/html/index.html12345678<html><head><title>Welcome to Example.com!</title></head><body><h1>Success! The example.com virtual host is working!</h1></body></html>1sudocp/etc/apache2/sites-available/000-default.conf/etc/apache2/sites-available/blog.peter279k.tk.conf1234567891011121314151617181920212223242526272829303132333435# blog.peter279k.tk.conf<VirtualHost *:80># The ServerName directive sets the request scheme, hostname and port that# the server uses to identify itself. This is used when creating# redirection URLs. In the context of virtual hosts, the ServerName# specifies what hostname must appear in the request's Host: header to# match this virtual host. For the default virtual host (this file) this# value is not decisive as it is used as a last resort host regardless.# However, you must set it for any further virtual host explicitly.#ServerName www.example.comServerAdmin admin@blog.peter279k.tkServerName blog.peter279k.tkServerAlias www.blog.peter279k.tkDocumentRoot/var/www/blog.peter279k.tk/html/blog/public# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,# error, crit, alert, emerg.# It is also possible to configure the loglevel for particular# modules, e.g.#LogLevel info ssl:warnErrorLog ${APACHE_LOG_DIR}/error.logCustomLog ${APACHE_LOG_DIR}/access.log combined<Directory"/var/www/blog.peter279k.tk/html/blog/public">AllowOverride All</Directory># For most configuration files from conf-available/, which are# enabled or disabled at a global level, it is possible to# include a line for only one particular virtual host. For example the# following line enables the CGI configuration for this host only# after it has been globally disabled with "a2disconf".#Include conf-available/serve-cgi-bin.conf</VirtualHost>DocumentRoot 和 Directory 需要指定自己的 folder 指定到哪個目錄,網域名稱也需要改成自己改的名稱。
- 啟動 site 並重新啟動 Apache2 服務,即完成了。
12
sudoa2ensite blog.peter279k.tk.confsudosystemctl restart apache2
VirtualHost 設定參考文章:https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts






