[轉(zhuǎn)]Apache+PHP3+PostgreSQL作為基于Web的數(shù)據(jù)庫(kù)平臺(tái)的安裝
Jun 21, 2016 am 09:13 AMapache|web|數(shù)據(jù)|數(shù)據(jù)庫(kù)
發(fā)信人: xiaoning (我要飛), 信區(qū): Linux
標(biāo)??題: Apache+PHP3+PostgreSQL作為基于Web的數(shù)據(jù)庫(kù)平臺(tái)的安
發(fā)信站: BBS 水木清華站 (Thu Jan 20 16:00:28 2000)
??
以下將說明使用Apache+PHP3+PostgreSQL作為基于Web的數(shù)據(jù)庫(kù)平臺(tái)的安裝和配置方法
。
關(guān)于Apache、PHP3和PostgreSQL的更多內(nèi)容可以從軟件的附帶文檔、Linux的HOWTO文件
??
以及以下站點(diǎn)處找到:
Apache: http://www.apache.org
PHP3: http://www.php.net
PostgreSQL: http://www.postgresql.org
1. PostgreSQL的安裝和設(shè)置
1.1 獲得源程序
PostgreSQL最新版本的源程序可以在http://www.postgresql.org找到。目前的最新版本
??
是6.5.X。以下以6.4 版為例說明安裝方法。
1.2 準(zhǔn)備工作
編譯PostgreSQL需要3.75版以上的GNU make (用gmake -v檢查版本號(hào)) ,2.7.2版以上的
??
GNU C(用gcc -v 檢查版本號(hào))以及bison和flex(通常這兩種工具都已經(jīng)安裝了) 。
PostgreSQL的默認(rèn)安裝位置為/usr/local/pgsql/,系統(tǒng)文件約需3-10M空間。附帶的測(cè)
試
程序在運(yùn)行時(shí)需要約20M空間,所以安裝時(shí)應(yīng)注意預(yù)留足夠的空間,建議/usr/local/pg
sql/
目錄下保證有50M以上空間。另外展開和編譯源程序約需30-60M空間。
對(duì)多用戶的應(yīng)用環(huán)境,建議設(shè)置一個(gè)專用用戶名,例如 postgres :
$ su 首先登錄為root
# /usr/sbin/adduser postgres
另外PostgreSQL使用了System V的共享內(nèi)存機(jī)制。FreeBSD默認(rèn)狀態(tài)不支持該機(jī)制。如使
用
中的內(nèi)核的設(shè)置文件中無(wú)以下項(xiàng)目則需追加后重新編譯內(nèi)核:
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 [假設(shè)下載文件保存在/tmp目錄下]
完成后應(yīng)生成一個(gè)postgresql-v6.4目錄,下面開始編譯:
$ cd /usr/local/src/pgsql/postgresql-v6.4/src
$ ./configure --with-mb=EUC_CN
其中,--with-mb=指定系統(tǒng)默認(rèn)字符編碼。除GB碼(EUC_CN) 外,還可以指定為日語(yǔ)
(EUC_JP)、韓語(yǔ)(EUC_KR) 、臺(tái)灣(EUC_TW) 、UNICODE、MULE_INTERNAL、LATIN1等。
注意,如使用6.3.2版需用--with-template=... 指定操作系統(tǒng),具體內(nèi)容見源程序附帶
的說明文件。
configure如順利完成,將生成GNUmakefile,Makefile.global和Makefile.port等文件。
??
$ gmake all
在編譯結(jié)束后將顯示以下信息:
All of PostgreSQL is successfully made. Ready to install.
安裝:
$ gmake install
正常完成后,PostgreSQl的執(zhí)行文件和庫(kù)文件等將被安裝到/usr/local/pgsql目錄下。
??
安裝附帶文檔:
$ gmake install-man
$ cd /usr/local/src/postgresql-v6.4/doc
$ make install
至此PostgreSQL的編譯安裝已經(jīng)完成,下面開始初期設(shè)置。
1.4 初期設(shè)置
設(shè)置環(huán)境變量:
如使用的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"
然后執(zhí)行 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"
然后執(zhí)行 source ~/.cshrc
以上環(huán)境變量是所有使用數(shù)據(jù)庫(kù)的用戶都需要設(shè)置的。
數(shù)據(jù)庫(kù)目錄的初始化:
$ initdb
可以使用的參數(shù):
--pgdata=/pgsql/db 指定數(shù)據(jù)庫(kù)目錄,默認(rèn)使用環(huán)境變量PGDATA指定的位置
--pgencoding=EUC_CN 指定數(shù)據(jù)庫(kù)的字符編碼,默認(rèn)使用configure時(shí)指定的編碼。
需要注意的是執(zhí)行initdb的用戶將擁有所建數(shù)據(jù)庫(kù)目錄的管理權(quán)。
使用以下命令啟動(dòng)PostgreSQL:
$ postmaster -S
運(yùn)行測(cè)試程序:
$ cd test/regression
$ gmake all runtest
如測(cè)試程序能正常運(yùn)行則說明PostgreSQL能正常運(yùn)行。
為了讓 PostgreSQL在系統(tǒng)啟動(dòng)時(shí)能自動(dòng)啟動(dòng),需作以下變更:
首先以root登錄
$ su
Linux: 在/etc/rc.d/rc.local中追加以下內(nèi)容:
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的文件,內(nèi)容為:
#! /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文件的權(quán)限:
# chmod 755 pgsql.sh
現(xiàn)在PostgreSQL的安裝和設(shè)定基本已經(jīng)完成了,但是目前為止能使用數(shù)據(jù)庫(kù)的用戶只有
postgres。
為使其他用戶可以使用數(shù)據(jù)庫(kù),需要登錄數(shù)據(jù)庫(kù)用戶和生成用戶數(shù)據(jù)。
例如通過Web服務(wù)器Apache進(jìn)行查詢的用戶,用戶名為nobody:
% createuser nobody
Enter user's 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可以同時(shí)管理多個(gè)數(shù)據(jù)庫(kù)(但數(shù)據(jù)庫(kù)之間不能進(jìn)行join等操作)。新建數(shù)據(jù)庫(kù)
:
$ createdb 數(shù)據(jù)庫(kù)名
新建的數(shù)據(jù)庫(kù)將被放在/usr/local/pgsql/data/base的同名目錄下(環(huán)境變量PGDATA指定
??
路徑的base目錄下)。如省略數(shù)據(jù)庫(kù)名參數(shù),將自動(dòng)以用戶名作為數(shù)據(jù)庫(kù)名。
例如 名為db1的數(shù)據(jù)庫(kù)將被保存在/usr/local/pgsql/data/base/db1目錄下。
也可使用initlocation命令指定其他位置:
$ initlocation /pgsql/data
$ export PGDATA2=/pgsql/data
$ createdb -D PGDATA2 db2
則數(shù)據(jù)庫(kù)db2將被保存在/pgsql/data目錄下。另外PostgreSQL 6.4版可以在建立數(shù)據(jù)庫(kù)
時(shí)
指定文字編碼:
createdb -E “字符編碼” “字符編碼” 參見
configure、initdb。
刪除數(shù)據(jù)庫(kù):destroydb 數(shù)據(jù)庫(kù)名
PostgreSQL最基本的數(shù)據(jù)庫(kù)管理工具是pgsql. 基本使用方法:pgsql 數(shù)據(jù)庫(kù)名
1.5 安全設(shè)置:
PostgreSQL提供了基于主機(jī)的認(rèn)證方式host based authentication(HBA) 、基于口令的
??
用戶身份認(rèn)證和用戶操作權(quán)限設(shè)置等安全機(jī)制。
1.5.1 HBA方式和基于口令的用戶身份認(rèn)證方式
HBA方式的設(shè)置文件為pg_hba.conf。其格式為:
host DBNAME IP_ADDRESS ADDRESS_MASK USRAUTH [AUTH_ARGUMENT]
host: 固定標(biāo)志,不能修改
DBNAME: 數(shù)據(jù)庫(kù)名,all代表所有數(shù)據(jù)庫(kù)
IP_ADDRESS,ADDRESS_MASK:指定IP地址,也可指定子網(wǎng),如192.168.10.0/255.255.2
55.0
USERAUTH: 對(duì)用戶的認(rèn)證方式,包括ident(RFC1413) 、trust(不進(jìn)行認(rèn)證) 、reject
(拒絕符合條件的訪問) 、password [passwd_file](根據(jù)flat file口令文件進(jìn)行認(rèn)證)
、
crypt(使用PostgreSQL的系統(tǒng)數(shù)據(jù)庫(kù)pg_shadow進(jìn)行認(rèn)證) 、kbr4/kbr5(Kerberos V4/V
5認(rèn)
證) 。如在作為Web 數(shù)據(jù)庫(kù)使用, 使用passwd口令文件進(jìn)行本地登錄時(shí)可如下設(shè)置:
Host all 127.0.0.1 255.255.255.255 password passwd
passwd文件默認(rèn)位置為/usr/local/pgsql/data, 有數(shù)據(jù)庫(kù)管理權(quán)限的用戶可以使用
pg_passwd命令管理該文件(注意:flat file口令認(rèn)證方式采用明文傳送口令,故要從網(wǎng)
絡(luò)
上其他主機(jī)登錄,建議使用crypt認(rèn)證方式) 。
1.5.2 用戶操作權(quán)限設(shè)置
使用SQL命令grant/revoke可以設(shè)置用戶/用戶組可否使用select/insert/update/rule命
令。
根據(jù)SQL標(biāo)準(zhǔn),一個(gè)數(shù)據(jù)表文件(table) 在剛建立時(shí)只有建立該表文件的用戶有權(quán)訪問。
??
要分配給用戶訪問權(quán)限,可以使用grant命令:
grant on to
權(quán)限種類:all,select,insert,update,delete,rule
table名:被設(shè)置用戶訪問權(quán)限的表文件
用戶/組:public(所有用戶) 或用戶名/組名
revoke命令作用與grant命令相反,用法如下:
revoke on from
table已設(shè)置的用戶權(quán)限可以使用pgsql的\z 命令查看。
2. Apache+PHP3的安裝及設(shè)置
2.1 源程序
Apache和PHP3的最新版本源程序可以從http://www.apache.org/和http://www.php.net
/找到。
以下以Apache 1.3.6和PHP3 3.0.5 為例。
2.2 編譯
假設(shè)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目錄,并將以下文件
??
復(fù)制到該目錄下:
Makefile.libdir libmodphp3.a mod_php3.c php_version.h
Makefile.tmpl libphp3.module mod_php3.h
編譯和安裝Apache:
設(shè)置環(huán)境變量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 修改設(shè)置文件
# cd /usr/local/src/php-3.0.5
# cp php3.ini-dist /usr/local/lib/php3.ini
將/usr/local/apache/etc/srm.conf中以下兩行內(nèi)容前的#號(hào)刪除,如果以.php3作為PH
P3
文件的擴(kuò)展名,則將.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 啟動(dòng)Apache
# /usr/local/apache/sbin/apachectl start
apachectl start: httpd started
在本地機(jī)上啟動(dòng)網(wǎng)絡(luò)瀏覽器,地址欄輸入http://localhost/(或在其他計(jì)算機(jī)上的瀏覽
器
地址欄輸入Apache服務(wù)器地址),如能顯示出Apache的開始畫面則說明Apache已經(jīng)
安裝成功。下面測(cè)試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文件的運(yùn)行結(jié)果是否正常。如日期函數(shù)能顯示正確結(jié)果,則說明PHP3模塊工作
正常。
至此,基于Apache Web服務(wù)器和服務(wù)器端腳本語(yǔ)言PHP3的PostgreSQL數(shù)據(jù)庫(kù)系統(tǒng)的安裝
已經(jīng)完成 :-)?

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Software preparation I am using a virtual machine with CentOS-6.6, with the host name repo. Refer to the steps to install a Linux virtual machine in Windows, I installed JDK in that virtual machine, refer to the guide to installing JDK in Linux. In addition, the virtual machine is configured with a key-free login itself, and the settings for configuring key-free login between each virtual machine are referenced. The download address of Hadoop installation package is: https://mirrors.aliyun.com/apache/hadoop/common/. I am using hadoop 2.6.5 version. Upload the Hadoop installation package to the server and unzip [root@repo~]#tarzxv

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

The steps to deploy a Joomla website on PhpStudy include: 1) Configure PhpStudy, ensure that Apache and MySQL services run and check PHP version compatibility; 2) Download and decompress PhpStudy's website from the official Joomla website, and then complete the installation through the browser according to the installation wizard; 3) Make basic configurations, such as setting the website name and adding content.

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

PHP code can be executed in many ways: 1. Use the command line to directly enter the "php file name" to execute the script; 2. Put the file into the document root directory and access it through the browser through the web server; 3. Run it in the IDE and use the built-in debugging tool; 4. Use the online PHP sandbox or code execution platform for testing.

Updating the Tomcat version in the Debian system generally includes the following process: Before performing the update operation, be sure to do a complete backup of the existing Tomcat environment. This covers the /opt/tomcat folder and its related configuration documents, such as server.xml, context.xml, and web.xml. The backup task can be completed through the following command: sudocp-r/opt/tomcat/opt/tomcat_backup Get the new version Tomcat Go to ApacheTomcat's official website to download the latest version. According to your Debian system

Reasons for system performance not recovered after uninstalling the Apache service may include resource occupancy by other services, error messages in log files, resource consumption by abnormal processes, network connection problems, and file system residues. First, check whether there are other services or processes before uninstalling with Apache; second, pay attention to the operating system's log files and find error messages that may occur during the uninstallation process; second, check the system's memory usage and CPU load, and find out abnormal processes; then, use the netstat or ss command to view the network connection status to ensure that no ports are occupied by other services; finally, clean up the remaining configuration files and log files after uninstallation to avoid occupying disk space.
