新增 Apache2 VirtualHost 使用子網域以及 Freenom 免費網域的申請

最近正在煩惱著該如何部署 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 免費頂級網域申請]

  1. 進入 freenom.com 網站。
  2. 輸入一個沒有人註冊的網域名稱並按下『檢查可用性』,如下圖。
    %e5%bf%ab%e7%85%a710
  3. 註冊或登入一個帳號,並成功拿到網域之後,進到如下的畫面:點選 My Domain 接著選擇 Manage Domain。
    %e5%bf%ab%e7%85%a711
  4. 選擇 tab 名稱:Manage Freenom DNS
    %e5%bf%ab%e7%85%a713
    在『Add record』的部份新增一個 A 紀錄,紀錄 ipv4 位址。Name 空白以及在 Target 的地方,填寫自己的固定 ip 位址所以只適合 VPS 或實體有固定 ip 的主機。(TTL 數值可以用預設就好,不用修改它)
  5. 填寫完成之後,按下 Save Changes 按鈕,等一段時間之後,就會出現『Record added successfully 』就代表成功加入一筆紀錄了。
  6. 增加完 A 紀錄之後,就可以知道使用網域名稱來 request url 而且不需要打 ip 位址了。
  7. 接著再新增 CNAME 紀錄,用來做子網域使用,讓外面連進來的人也可以使用子網域連線(搭配 VirtualHost 使用)。
  8. 新增 CNAME 紀錄範例如下圖:
    %e5%bf%ab%e7%85%a714

[VirtualHost 設定]

  1. 需要先安裝好 Apache2 或是 LAMP server 可以參考之前的文章
  2. 照下面新增一個目錄(以 blog.peter279k.tk)
    sudo mkdir -p /var/www/blog.peter279k.tk/html
    
  3. 設定 permissions 權限,這裡可以將使用者權限設定成:www-data
    sudo chown -R www-data /var/www/blog.peter279k.tk/html
    
  4. 編輯一個測試的設定 HTML 檔。
    vi /var/www/blog.peter279k.tk/html/index.html
    
    <html>
    <head>
    <title>Welcome to Example.com!</title>
    </head>
    <body>
    <h1>Success! The example.com virtual host is working!</h1>
    </body>
    </html>
    
    sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/blog.peter279k.tk.conf
    
    # 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.com
    
    ServerAdmin admin@blog.peter279k.tk
    ServerName blog.peter279k.tk
    ServerAlias www.blog.peter279k.tk
    DocumentRoot /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:warn
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${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 指定到哪個目錄,網域名稱也需要改成自己改的名稱。

  5. 啟動 site 並重新啟動 Apache2 服務,即完成了。
    sudo a2ensite blog.peter279k.tk.conf
    sudo systemctl restart apache2
    

VirtualHost 設定參考文章:https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts