[转]Apache+PHP3+PostgreSQL作为基于Web的数据库平台的安装-PHP教程,PHP应用
来源:作者: 发布时间:2007-12-26 02:12:51


发信人: xiaoning (我要飞), 信区: linux
标 题: apache+php3+postgresql作为基于web的数据库平台的安
发信站: bbs 水木清华站 (thu jan 20 16:00:28 2000)
以下将说明使用apache+php3+postgresql作为基于web的数据库平台的安装和配置方法
。
关于apache、php3和postgresql的更多内容能够从软件的附带文档、linux的howto文档
连同以下站点处找到:
apache: http://www.apache.org
php3: http://www.php.net
postgresql: http://www.postgresql.org
1. postgresql的安装和配置
1.1 获得源程式
postgresql最新版本的源程式能够在http://www.postgresql.org找到。现在的最新版本
是6.5.x。以下以6.4 版为例说明安装方法。
1.2 准备工作
编译postgresql需要3.75版以上的gnu make (用gmake -v检查版本号) ,2.7.2版以上的
gnu c(用gcc -v 检查版本号)连同bison和flex(通常这两种工具都已安装了) 。
postgresql的默认安装位置为/usr/local/pgsql/,系统文档约需3-10m空间。附带的测
试
程式在运行时需要约20m空间,所以安装时应注意预留足够的空间,建议/usr/local/pg
sql/
目录下确保有50m以上空间。另外展开和编译源程式约需30-60m空间。
对多用户的应用环境,建议配置一个专用用户名,例如 postgres :
$ su 首先登录为root
# /usr/sbin/adduser postgres
另外postgresql使用了system v的共享内存机制。freebsd默认状态不支持该机制。如使
用
中的内核的配置文档中无以下项目则需追加后重新编译内核:
options sysvshm
options sysvsem
options sysvmsg
建保存源程式的目录/usr/local/src/pgsql和安装目录/usr/local/pgsql:
#mkdir /usr/local/pgsql
#chown postgres:postgres /usr/local/pgsql
#mkdir /usr/local/src
#mkdir /usr/local/src/pgsql
#chown postgres:postgres /usr/local/src/pgsql
1.3 编译
以postgres用户登录, 解压缩源程式:
# su postgres
$ tar -xzvf /tmp/postgresql-v6.4.tar.gz [假设下载文档保存在/tmp目录下]
完成后应生成一个postgresql-v6.4目录,下面开始编译:
$ cd /usr/local/src/pgsql/postgresql-v6.4/src
$ ./configure --with-mb=euc_cn
其中,--with-mb=指定系统默认字符编码。除gb码(euc_cn) 外,还能够指定为日语
(euc_jp)、韩语(euc_kr) 、台湾(euc_tw) 、unicode、mule_internal、latin1等。
注意,如使用6.3.2版需用--with-template=... 指定操作系统,具体内容见源程式附带
的说明文档。
configure如顺利完成,将生成gnumakefile,makefile.global和makefile.port等文档。
$ gmake all
在编译结束后将显示以下信息:
all of postgresql is successfully made. ready to install.
安装:
$ gmake install
正常完成后,postgresql的执行文档和库文档等将被安装到/usr/local/pgsql目录下。
安装附带文档:
$ gmake install-man
$ cd /usr/local/src/postgresql-v6.4/doc
$ make install
至此postgresql的编译安装已完成,下面开始初期配置。
1.4 初期配置
配置环境变量:
如使用的shell为bash, 则在.bashrc中添加以下命令:
path="$path":/usr/local/pgsql/bin
export postgres_home=/usr/local/pgsql
export pglib=$postgres_home/lib
export pgdata=$postgres_home/data
export manpath="$manpth":$postgres_home/man
export ld_library_path="$ld_library_path":"$pglib"
然后执行 source ~/.bashrc
如使用的shell为csh/tcsh则在.cshrc中添加以下命令:
setenv path="$path":/usr/local/pgsql/bin
setenv postgres_home=/usr/local/pgsql
setenv pglib=$postgres_home/lib
setenv pgdata=$postgres_home/data
setenv manpath="$manpth":$postgres_home/man
setenv ld_library_path="$ld_library_path":"$pglib"
然后执行 source ~/.cshrc
以上环境变量是任何使用数据库的用户都需要配置的。
数据库目录的初始化:
$ initdb
能够使用的参数:
--pgdata=/pgsql/db 指定数据库目录,默认使用环境变量pgdata指定的位置
--pgencoding=euc_cn 指定数据库的字符编码,默认使用configure时指定的编码。
需要注意的是执行initdb的用户将拥有所建数据库目录的管理权。
使用以下命令启动postgresql:
$ postmaster -s
运行测试程式:
$ cd test/regression
$ gmake all runtest
如测试程式能正常运行则说明postgresql能正常运行。
为了让 postgresql在系统启动时能自动启动,需作以下变更:
首先以root登录
$ su
linux: 在/etc/rc.d/rc.local中追加以下内容:
postgresdir=/usr/local/pgsql
if [ -x $postgresdir/bin/postmaster -a -d $postgresdir/data ];then
rm -f /tmp/s.pgsql.5432
su - postgres -c "postmaster -s -i"
echo -n postmaster
fi
freebsd: 在/usr/local/etc/rc.d中建立名为pgsql.sh的文档,内容为:
#! /bin/sh
postgresdir=/usr/local/pgsql
if [ -x $postgresdir/bin/postmaster -a -d $postgresdir/data ];then
rm -f /tmp/s.pgsql.5432
su - postgres -c "postmaster -s -i"
echo -n postmaster
fi
修改pgsql.sh文档的权限:
# chmod 755 pgsql.sh
现在postgresql的安装和设定基本已完成了,但是现在为止能使用数据库的用户只有
postgres。
为使其他用户能够使用数据库,需要登录数据库用户和生成用户数据。
例如通过web服务器apache进行查询的用户,用户名为nobody:
% createuser nobody
enter users postgres id or return to use unix user id: 1000 ->1000
is user "nobody" allowed to create dataase(y/n)n
is user "nobody" allowed to add users?(y/n)n
createuser: nobody was successfully added
删除用户能够使用命令destroyuser 用户名
postgresql能够同时管理多个数据库(但数据库之间不能进行join等操作)。新建数据库
:
$ createdb 数据库名
新建的数据库将被放在/usr/local/pgsql/data/base的同名目录下(环境变量pgdata指定
路径的base目录下)。如省略数据库名参数,将自动以用户名作为数据库名。
例如 名为db1的数据库将被保存在/usr/local/pgsql/data/base/db1目录下。
也可使用initlocation命令指定其他位置:
$ initlocation /pgsql/data
$ export pgdata2=/pgsql/data
$ createdb -d pgdata2 db2
则数据库db2将被保存在/pgsql/data目录下。另外postgresql 6.4版能够在建立数据库
时
指定文字编码:
createdb -e “字符编码” “字符编码” 参见
configure、initdb。
删除数据库:destroydb 数据库名
postgresql最基本的数据库管理工具是pgsql. 基本使用方法:pgsql 数据库名
1.5 安全配置:
postgresql提供了基于主机的认证方式host based authentication(hba) 、基于口令的
用户身份认证和用户操作权限配置等安全机制。
1.5.1 hba方式和基于口令的用户身份认证方式
hba方式的配置文档为pg_hba.conf。其格式为:
host dbname ip_address address_mask usrauth [auth_argument]
host: 固定标志,不能修改
dbname: 数据库名,all代表任何数据库
ip_address,address_mask:指定ip地址,也可指定子网,如192.168.10.0/255.255.2
55.0
userauth: 对用户的认证方式,包括ident(rfc1413) 、trust(不进行认证) 、reject
(拒绝符合条件的访问) 、password [passwd_file](根据flat file口令文档进行认证)
、
crypt(使用postgresql的系统数据库pg_shadow进行认证) 、kbr4/kbr5(kerberos v4/v
5认
证) 。如在作为web 数据库使用, 使用passwd口令文档进行本地登录时可如下配置:
host all 127.0.0.1 255.255.255.255 password passwd
passwd文档默认位置为/usr/local/pgsql/data, 有数据库管理权限的用户能够使用
pg_passwd命令管理该文档(注意:flat file口令认证方式采用明文传送口令,故要从网
络
上其他主机登录,建议使用crypt认证方式) 。
1.5.2 用户操作权限配置
使用sql命令grant/revoke能够配置用户/用户组可否使用select/insert/update/rule命
令。
根据sql标准,一个数据表文档(table) 在刚建立时只有建立该表文档的用户有权访问。
要分配给用户访问权限,能够使用grant命令:
grant <权限种类> on to <用户/组>
权限种类:all,select,insert,update,delete,rule
table名:被配置用户访问权限的表文档
用户/组:public(任何用户) 或用户名/组名
revoke命令作用和grant命令相反,用法如下:
revoke <权限种类> on from <用户/组>
table已配置的用户权限能够使用pgsql的\z 命令查看。
2. apache+php3的安装及配置
2.1 源程式
apache和php3的最新版本源程式能够从http://www.apache.org/和http://www.php.net
/找到。
以下以apache 1.3.6和php3 3.0.5 为例。
2.2 编译
假设apache和php3的源程式都保存在/tmp目录下,首先登录为root:
$ su
# cd /usr/local/src
# tar -xzvf /tmp/apache_1.3.6.tar.gz
# ./configure
# cd /usr/local/src
# tar -xzvf /tmp/php3-3.0.5.tar.gz
# cd php-3.0.5
# ./configure --with-pgsql --with-apache=../apache_1.3.6 --enable-track-vars
# make
# make install
最后一步将建立/usr/local/src/apache_1.3.6/src/modules/php3目录,并将以下文档
复制到该目录下:
makefile.libdir libmodphp3.a mod_php3.c php_version.h
makefile.tmpl libphp3.module mod_php3.h
编译和安装apache:
配置环境变量ld_library_path csh和tcsh shell使用以下命令:
# setenv ld_library_path /usr/local/pgsql/lib
sh和bash使用如下命令:
# ld_library_path=/usr/local/pgsql/lib
# export ld_library_path
然后
# cd /usr/local/src/apache_1.3.6
# ./configure --activate-module=src/modules/php3/libphp3.a
# make
# make install
2.3 修改配置文档
# cd /usr/local/src/php-3.0.5
# cp php3.ini-dist /usr/local/lib/php3.ini
将/usr/local/apache/etc/srm.conf中以下两行内容前的#号删除,假如以.php3作为ph
p3
文档的扩展名,则将.phtml 更改为 .php3:
#addtype application/x-httpd-php3 .php3
#addtype application/x-httpd-php3-source .phps
在srm.conf文档的directoryindex下增加 index.php3:
directoryindex index.html index.php3
2.4 启动apache
# /usr/local/apache/sbin/apachectl start
apachectl start: httpd started
在本地机上启动网络浏览器,地址栏输入http://localhost/(或在其他电脑上的浏览
器
地址栏输入apache服务器地址),如能显示出apache的开始画面则说明apache已
安装成功。下面测试php3模块工作是否正常:
# cd /usr/local/apache/share/htdocs/
# ln -s /usr/local/src/php-3.0.5 .
然后在浏览器的地址栏输入http://localhost/php-3.0.5/examples/date.php3
观察php3文档的运行结果是否正常。如日期函数能显示正确结果,则说明php3模块工作
正常。
至此,基于apache web服务器和服务器端脚本语言php3的postgresql数据库系统的安装
已完成 :-)
|
还没有关于此文章的相关评论!