欢迎光临
我们一直在努力

PHP安装攻略:PostgreSQL-PHP教程,PHP应用

建站超值云服务器,限时71元/月

以下将说明使用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/pgsql/ 目录下保证有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.255.0

  userauth: 对用户的认证方式,包括ident(rfc1413) 、trust(不进行认证) 、reject (拒绝符合条件的访问)、password [passwd_file](根据flat file口令文件进行认证) 、 crypt(使用postgresql的系统数据库pg_shadow进行认证)、kbr4/kbr5(kerberos v4/v5认证) 。如在作为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作为php3文件的扩展名,则将.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数据库系统的安装已经完成 🙂

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » PHP安装攻略:PostgreSQL-PHP教程,PHP应用
分享到: 更多 (0)

相关推荐

  • 暂无文章