27.7 Apache HTTP Server

Contributed by Murray Stokely.

27.7.1 Overview

FreeBSD is used to run some of the busiest web sites in the world. The majority of web servers on the Internet are using the Apache HTTP Server. Apache software packages should be included on your FreeBSD installation media. If you did not install Apache when you first installed FreeBSD, then you can install it from the www/apache13 or www/apache22 port.

Once Apache has been installed successfully, it must be configured.

Note: This section covers version 1.3.X of the Apache HTTP Server as that is the most widely used version for FreeBSD. Apache 2.X introduces many new technologies but they are not discussed here. For more information about Apache 2.X, please see http://httpd.apache.org/.

27.7.2 Configuration

The main Apache HTTP Server configuration file is installed as /usr/local/etc/apache/httpd.conf on FreeBSD. This file is a typical UNIX® text configuration file with comment lines beginning with the # character. A comprehensive description of all possible configuration options is outside the scope of this book, so only the most frequently modified directives will be described here.

ServerRoot "/usr/local"

This specifies the default directory hierarchy for the Apache installation. Binaries are stored in the bin and sbin subdirectories of the server root, and configuration files are stored in etc/apache.

ServerAdmin you@your.address

The address to which problems with the server should be emailed. This address appears on some server-generated pages, such as error documents.

ServerName www.example.com

ServerName allows you to set a host name which is sent back to clients for your server if it is different to the one that the host is configured with (i.e., use www instead of the host's real name).

DocumentRoot "/usr/local/www/data"

DocumentRoot: The directory out of which you will serve your documents. By default, all requests are taken from this directory, but symbolic links and aliases may be used to point to other locations.

It is always a good idea to make backup copies of your Apache configuration file before making changes. Once you are satisfied with your initial configuration you are ready to start running Apache.

27.7.3 Running Apache

Apache 如同許多網路 server 均不透過 inetd 這個 super server。 因為它是設定以 standalone 方式會對 client 端瀏覽器所連入的 HTTP requests 才會有更好的效能。 而且其有附上讓 server 啟動、停止、 重啟更簡單的 shell script。 第一次要啟動 Apache 時,請打:

# /usr/local/sbin/apachectl start

隨時都可以打下列指令,使之停止:

# /usr/local/sbin/apachectl stop

無論是為了甚麼理由而修改設定檔,改完之後要重啟才會生效:

# /usr/local/sbin/apachectl restart

若不欲打斷既有的網頁連線來重新啟動 Apache ,請改用:

# /usr/local/sbin/apachectl graceful

細節部分請參閱 apachectl(8) 說明。

若要開機時自動啟動 Apache, 請在 /etc/rc.conf 加上下列設定:

apache_enable="YES"

或者 Apache 2.2 則應為:

apache22_enable="YES"

若要修改 Apachehttpd 在開機啟動參數,那麼就在 rc.conf 加上如下設定,並將參數寫在引號內:

apache_flags=""

或者 Apache 2.2 則應為:

apache22_flags=""

現在 web server 已經在運作當中,可透過瀏覽器以 http://localhost/ 網址去看網站。 預設所顯示的網頁位於 /usr/local/www/data/index.htmlApache 2.2 則是 /usr/local/www/apache22/data/index.html

27.7.4 Virtual Hosting

Apache supports two different types of Virtual Hosting. The first method is Name-based Virtual Hosting. Name-based virtual hosting uses the clients HTTP/1.1 headers to figure out the hostname. This allows many different domains to share the same IP address.

To setup Apache to use Name-based Virtual Hosting add an entry like the following to your httpd.conf:

NameVirtualHost *

If your webserver was named www.domain.tld and you wanted to setup a virtual domain for www.someotherdomain.tld then you would add the following entries to httpd.conf:

<VirtualHost *>
ServerName www.domain.tld
DocumentRoot /www/domain.tld
</VirtualHost>

<VirtualHost *>
ServerName www.someotherdomain.tld
DocumentRoot /www/someotherdomain.tld
</VirtualHost>

Replace the addresses with the addresses you want to use and the path to the documents with what you are using.

For more information about setting up virtual hosts, please consult the official Apache documentation at: http://httpd.apache.org/docs/vhosts/.

27.7.5 Apache 模組

有許多不同的 Apache 模組可讓基本的 server 提供許多附加功能。 而 FreeBSD 的 Ports Collection 則為 Apache 及這些好用的附加模組,提供了相當便利的安裝方式。

27.7.5.1 mod_ssl

mod_ssl 模組採用 OpenSSL library 以提供 Secure Sockets Layer (SSL v2/v3) 及 Transport Layer Security (TLS v1) 協定方面更安全的加密。 該模組 提供可從信任憑證簽署機構申請簽署憑證所需的所有工具,因此可在 FreeBSD 上建構安全網站。

若尚未裝過 Apache, 那麼也可以考慮直接連同 mod_ssl 一起裝的 Apache 1.3.X,可透過 www/apache13-modssl 來裝。 此外, SSL 支援在 Apache 2.X 起已是預設即有,可透過 www/apache22 來安裝。

27.7.5.2 Language Bindings

大多數主要的 scripting language 均有 Apache 相對應的模組, 這些模組讓 Apache 模組完全以某一種 scripting language 來寫變為可行。 它們通常也被嵌入到 server 作為常駐解譯器,以避免啟動外部解譯器對於動態網站(將於下一節提到) 的時間與資源消耗。

27.7.6 動態網站

在過去的十年裡,越來越多企業為增加收益與曝光率而投入 Internet, 這也同時增加對於互動網頁內容的需求。 有些公司像是 Microsoft® 均紛紛推出自家產品的解決方案,而 open source 社群也對這些動態網頁內容需求當然更不落人後,較流行的有像是 Django、 Ruby on Rails、mod_perlmod_php

27.7.6.1 Django

Django 是 BSD licensed 的一套網頁 framework, 以讓開發者快速寫出高性能、高品質的 web 程式。 它提供物件關聯(Object-Relational)對映工具,讓資料類型可被當為 Python 物件,並提供豐富的動態資料庫存取 API,讓開發者甚至不用寫 SQL。 此外還提供可擴展的 template 機制,讓程式的邏輯部分可以與 HTML 表達層面分離。

欲裝 Django 則必須裝 mod_pythonApache、以及打算要用的 SQL 資料庫引擎。 FreeBSD Port 會在您選好相關要用的版本後, 自動裝妥這些必須的套件,像是:

Example 27-3. 安裝 Django 搭配 Apache2、mod_python3、PostgreSQL

# cd /usr/ports/www/py-django; make all install clean -DWITH_MOD_PYTHON3 -DWITH_POSTGRESQL

在裝完 Django 及相關相依套件之後, 必須建立自己 Django 程式要放的目錄,然後設定 Apache 以便在相關 URL 時使用內嵌的 Python 解譯器。

Example 27-4. Apache 的 Django/mod_python 設定

在 apache 的 httpd.conf 設定檔加入以下內容 ,以便把特定的 URL request 傳給該 web 程式:

<Location "/">
    SetHandler python-program
    PythonPath "['/dir/to/your/django/packages/'] + sys.path"
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings
    PythonAutoReload On
    PythonDebug On
</Location>

27.7.6.2 Ruby on Rails

Ruby on Rails 則是另一個 open source web framework, 其提供全面的開發框架,以助 web 開發者工作更具成效, 以及快速寫出強力的應用程式。 Ports 安裝方式相當簡單:

# cd /usr/ports/www/rubygem-rails; make all install clean

27.7.6.3 mod_perl

Apache/Perl 整合計劃,將 Perl 程式語言的威能與 Apache HTTP Server 緊密結合在一起。 透過 mod_perl 模組可完全以 Perl 來撰寫 Apache 模組。 此外,server 內嵌的常駐解譯器,也有助避免啟動 Perl 外部解譯器所浪費的多餘時間。

mod_perl 有許多安裝方式。 請注意:mod_perl 的一些版本限制, mod_perl 1.0 只能搭配 Apache 1.3,而 mod_perl 2.0 只能搭配 Apache 2.X。 mod_perl 1.0 可透過 www/mod_perl 來裝, 而靜態編譯的則是透過 www/apache13-modperl 來裝。 mod_perl 2.0 則可透過 www/mod_perl2 來裝。

27.7.6.4 mod_php

Written by Tom Rhodes.

PHP,又稱為 “PHP: Hypertext Preprocessor” 乃是特別適用於 Web 開發的通用 scripting language。 它可以很容易嵌入到 HTML, 而其語法相當類似 C 、Java™、Perl,以讓 web 開發者快速撰寫動態產生的網頁。

要讓 Apache web server 可以支援 PHP5 的話,,則可以從安裝 lang/php5 開始。

第一次安裝 lang/php5 時會自動顯示可用的 OPTIONS(安裝選項) 選單, 若沒出現選單,或者之前有裝過 lang/php5 等等其他因素, 則可以用下列指令以再次出現選單畫面,請在該 port 目錄內執行:

# make config

在選單中請勾選 APACHE 選項, 即可以編譯出搭配 Apache web server 所使用的 mod_php5 動態模組。

Note: 由於各種原因(例如:相容問題或者已經開發的成品),許多網站仍在用 PHP4。 若您需要的是 mod_php4 而非 mod_php5,那請改裝 lang/php4lang/php4lang/php5 一樣提供許多設定、 編譯的選項。

上述動作會安裝並設定用以支援 PHP 程式所需的模組,接著請檢查是否有將下列設定加到 /usr/local/etc/apache/httpd.conf 內:

LoadModule php5_module        libexec/apache/libphp5.so
AddModule mod_php5.c
    <IfModule mod_php5.c>
        DirectoryIndex index.php index.html
    </IfModule>
    <IfModule mod_php5.c>
        AddType application/x-httpd-php .php
        AddType application/x-httpd-php-source .phps
    </IfModule>

設完之後,需要以 apachectl 指令來作 graceful restart 重啟動作,以便載入PHP 模組:

# apachectl graceful

日後升級 PHP 時,就不必再做 make config 了,因為您所作的 OPTIONS 選擇會自動被 FreeBSD Ports 存起來。

在 FreeBSD 的 PHP 支援是採高度模組化, 因此基本安裝的功能相當有限,若要增加其他 PHP extension 支援,請透過 lang/php5-extensions 來安裝即可。 該 port 提供選單介面來勾選要裝的 PHP extension。 此外,直接透過相對應的 port 來單獨安裝亦可。

例如要增加 PHP5 的 MySQL 資料庫支援,只需裝 databases/php5-mysql 即可。

裝完新的 PHP extension 之後,要記得重啟 Apache,以重新讀取新的設定:

# apachectl graceful

本文及其他文件,可由此下載:ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/

若有 FreeBSD 方面疑問,請先閱讀 FreeBSD 相關文件,如不能解決的話,再洽詢 <questions@FreeBSD.org>。
關於本文件的問題,請洽詢 <doc@FreeBSD.org>。