萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Apache + PHP3 + PostgreSQL 安裝與配置完整教學

本文示範在 Linux 上從原始碼編譯安裝 Apache 1.3.6、PHP 3.0.5 與 PostgreSQL 6.4,構建 Web 資料庫應用平台。

1. PostgreSQL 安裝與設定

1.1 取得原始碼

官網 postgresql.org 下載最新版(本文以 6.4 為例)。

1.2 建置環境準備

  • GNU make 3.75+、GCC 2.7.2+、bison、flex
  • 預設安裝路徑 /usr/local/pgsql,需預留 50MB 空間
  • 建立專用用戶 postgres:adduser postgres
  • FreeBSD 需在核心啟用 System V IPC:options SYSVSHM SYSVSEM SYSVMSG

1.3 編譯安裝

su postgres
tar -xzvf postgresql-v6.4.tar.gz
cd postgresql-v6.4/src
./configure --with-mb=EUC_CN   # 指定預設編碼(支援 BIG5、EUC_JP 等)
gmake all
gmake install
gmake install-man

1.4 初始化與環境變數

在 ~/.bashrc (bash) 或 ~/.cshrc (tcsh) 設定:

export PATH="$PATH":/usr/local/pgsql/bin
export POSTGRES_HOME=/usr/local/pgsql
export PGLIB=$POSTGRES_HOME/lib
export PGDATA=$POSTGRES_HOME/data
export MANPATH="$MANPATH":$POSTGRES_HOME/man
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"

初始化資料庫叢集:initdb(可用 --pgencoding 指定編碼)。啟動:postmaster -S。執行測試:cd test/regression && gmake all runtest。

1.5 開機自動啟動

Linux:在 /etc/rc.d/rc.local 加入啟動腳本。FreeBSD:建立 /usr/local/etc/rc.d/pgsql.sh。

1.6 用戶與資料庫管理

createuser nobody   # 為 Apache 用戶建立 DB 帳號
createdb dbname     # 建立資料庫
destroydb dbname    # 刪除資料庫
pgsql dbname        # 互動式 SQL 工具

1.7 安全設定

  • HBA (pg_hba.conf):host all 127.0.0.1 255.255.255.255 password passwd 允許本機密碼驗證。
  • SQL 權限:GRANT SELECT,INSERT ON table TO user / REVOKE。

2. Apache + PHP3 編譯安裝

2.1 取得原始碼

Apache: apache.org、PHP: php.net。

2.2 編譯流程

# 解壓並編譯 Apache
cd /usr/local/src
tar -xzvf /tmp/apache_1.3.6.tar.gz
cd apache_1.3.6
./configure
# 解壓 PHP
cd /usr/local/src
tar -xzvf /tmp/php3-3.0.5.tar.gz
# PHP 需指定 --with-apache=../apache_1.3.6 --with-pgsql=/usr/local/pgsql
# 然後重新編譯 Apache 以內嵌 PHP 模組

關鍵步驟:PHP 設定時需指定 Apache 原始碼路徑與 PostgreSQL 安裝路徑,再回到 Apache 目錄執行 ./configure --activate-module=src/modules/php3/libphp3.a 並 make、make install。

3. 後續設定

  • 編輯 httpd.conf 載入 PHP 模組、設定 AddType application/x-httpd-php3 .php3
  • 編輯 php3.ini 設定資料庫連線參數
  • 測試 phpinfo() 與資料庫連線程式
copyright © 萬盛學電腦網 all rights reserved