PHP开发之

1.准备工作:

1.1安装文件准备和安装环境确认

1.1.1源文件

1)Apache代理服务软件httpd-2.2.11

2)PHP开源代码包php-5.3.0

1.1.2安装环境确认

1) 操作系统版本【非必要环节】

OS:Linux 2.6.18-128.el5(RHAS 5.3)【默认安装】

gcc version 4.1.2 20080704

 

[root@localhost /]# cat /proc/version

Linux version 2.6.18-128.el5 (mockbuild@hs20-bc1-5.build.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-44)) #1 SMP Wed Dec 17 11:42:39 EST 2008

[root@localhost /]# lsb_release -a

LSB Version:    :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch

Distributor ID: RedHatEnterpriseServer

Description:    Red Hat Enterprise Linux Server release 5.3 (Tikanga)

Release:        5.3

Codename:       Tikanga

[root@localhost /]#

 

 2)基本环境确认

以下查询确认的GCC和libxml2一般在安装操作系统时都已经默认安装过了。如果未安装或版本有特定要求,下载对应的rpm包,自行安装。

GCC编译源码时需要GCC编译器,使用命令【rpm -qa | grep ^gcc】

查询确认是否已经安装。

[root@localhost ResourceDir]# rpm -qa | grep ^gcc

gcc-4.1.2-44.el5

gcc-c++-4.1.2-44.el5

gcc-gfortran-4.1.2-44.el5

[root@localhost ResourceDir]#

 

 


 

libxml2

安装php5需要libxml2,没有装libxml2可能会出现这样的错误:

configure: error: xml2-config not found. Please check your libxml2 installation。使用【rpm -qa | grep libxml】查询确认。

[root@localhost ResourceDir]# rpm -qa | grep libxml

libxml2-2.6.26-2.1.2.7

libxml2-python-2.6.26-2.1.2.7

libxml2-devel-2.6.26-2.1.2.7

[root@localhost ResourceDir]#

 

 

 

 

1.2目标安装程序的版本验证:

通常,Linux系统默认安装了Apache、PHP,但版本都比较低。更重要的一点是,在上述程序的安装配置过程中,有一些是相互关联的操作!虽然也可以手工配置,但是在安装过程中只是设置一些参数就能完成,还是比较方便的。所以建议如果已经安装的,就卸载掉,安装新版本。

1.2.1卸载Apache的代理服务httpd

1)查询是否已经安装

安装系统软件都是通过rpm包安装的,所以可以通过命令【rpm -qa | grep httpd】来查询可执行程序的安装列表:

-qa是指列出所有安装的软件,grep httpd,是只列出包含httpd的所有软件。

[root@localhost ResourceDir]# rpm -qa | grep httpd

httpd-manual-2.2.3-22.el5

httpd-2.2.3-22.el5

[root@localhost ResourceDir]#

 

 

 


 

本操作系统有关httpd的可执行程序列表如上。

2)卸载操作

使用【rpm -e httpd-2.2.3-22.el5】命令来卸载通过rpm包安装的可执行程序。如果要卸载的程序与其他程序存在依赖关系,则执行过程会中断,并且给出提示:

[root@localhost ResourceDir]# rpm -e httpd-2.2.3-22.el5

error: Failed dependencies:

        httpd-mmn = 20051115 is needed by (installed) mod_python-3.2.8-3.1.i386

        httpd-mmn = 20051115 is needed by (installed) mod_ssl-2.2.3-22.el5.i386

        httpd-mmn = 20051115 is needed by (installed) mod_perl-2.0.4-6.el5.i386

        httpd-mmn = 20051115 is needed by (installed) php-5.1.6-23.el5.i386

        webserver is needed by (installed) webalizer-2.01_10-30.1.i386

        httpd >= 2.0.40 is needed by (installed) mod_python-3.2.8-3.1.i386

        httpd = 0:2.2.3-22.el5 is needed by (installed) mod_ssl-2.2.3-22.el5.i386

[root@localhost ResourceDir]#

 

 

 

 

 

 

 

 


 

3)规避依赖关系的卸载操作

使用【rpm -e --allmatches --nodeps httpd-2.2.3-22.el5】命令来规避上述的依赖关系,完成卸载操作。具体过程如下:

[root@localhost ResourceDir]# which httpd

/usr/sbin/httpd

[root@localhost ResourceDir]# rpm -q -f /usr/sbin/httpd

httpd-2.2.3-22.el5

[root@localhost ResourceDir]# rpm -e --allmatches --nodeps httpd-2.2.3-22.el5

 [root@localhost ResourceDir]#

[root@localhost ResourceDir]# which httpd

/usr/bin/which: no httpd in

(/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin)

[root@localhost ResourceDir]#

 

 

 

1.2.2查询验证和卸载PHP

执行过程和上述Apache代理服务的卸载类似:

[root@localhost ResourceDir]# which php

/usr/bin/php

[root@localhost ResourceDir]# rpm -q -f /usr/bin/php

php-cli-5.1.6-23.el5

[root@localhost ResourceDir]# rpm -e --allmatches --nodeps php-cli-5.1.6-23.el5

[root@localhost ResourceDir]# which php

/usr/bin/which: no php in

(/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin)

[root@localhost ResourceDir]# rpm -qa |grep php

php-ldap-5.1.6-23.el5

php-common-5.1.6-23.el5

php-5.1.6-23.el5

[root@localhost ResourceDir]# rpm -e php-common-5.1.6-23.el5

error: Failed dependencies:

        php-common = 5.1.6-23.el5 is needed by (installed) php-5.1.6-23.el5.i386

        php-common = 5.1.6-23.el5 is needed by (installed) php-ldap-5.1.6-23.el5.i386

[root@localhost ResourceDir]# rpm -e php-ldap-5.1.6-23.el5

[root@localhost ResourceDir]# rpm -qa |grep php

php-common-5.1.6-23.el5

php-5.1.6-23.el5

[root@localhost ResourceDir]# rpm -e php-5.1.6-23.el5

[root@localhost ResourceDir]# rpm -e php-common-5.1.6-23.el5

[root@localhost ResourceDir]#

 

 

 

2. Apache和PHP的安装配置

2.1安装配置的准备工作

2.1.1建立安装程序的源文件目录【具体操作步骤见文本框】

1)创建目录;

2)上传或者copy安装程序源文件到相应目录;

3) 解压缩

4)修改新建目录及上传源文件的群组访问权限(保证安装过程中的正常读写访问)

[root@localhost /]#mkdir /opt/ ResourceDir/Apache

[root@localhost /]#mkdir /opt/ ResourceDir/PHP

[root@localhost /]#cd opt/ ResourceDir

[root@localhost ResourceDir]#cp /mnt/hgfs/ShareDocs/tools/httpd-2.2.11.tar.gz /opt/ResourceDir/Apache/

[root@localhost ResourceDir]# cp /mnt/hgfs/ShareDocs/tools/ php-5.3.0.tar.gz /opt/ResourceDir/PHP/

[root@localhost ResourceDir]#cd Apache

[root@localhost Apache]#tar –zxvf  httpd-2.2.11.tar.gz

……

……

[root@localhost Apache]#cd /opt/ ResourceDir/PHP

[root@localhost PHP]# tar –zxvf  php-5.3.0.tar.gz

……

……

[root@localhost PHP]#chmod 777  /opt/ ResourceDir

[root@localhost PHP]#

2.2安装配置Apache

2.2.1安装步骤【具体操作步骤见文本框】

1)进入解压的目录

2)运行configure安装配置参数;

./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-expires --enable-cache --enable-mem-cache

上述配置生效要执行一些时间。

configure指令参数:

--prefix是安装的目标路径;

enable后面模块选项,这里选择一些常用的模块:

so是支持动态链接机制上加载DSO【动态共享对象Dynamic Shared Objects】模块。后面装PHP时就会创建一个so的php模块。

rewrite就是常用到的重写。

expires是控制HTTP的"Expires:"和"Cache-Control:"头内容,PHP用header控制缓存时要用到。

3)生成makefile文件;

4)make

编译源码时间较长。

5)make test(非必要)

6)make install

[root@localhost PHP]#cd  /opt/ ResourceDir/Apache/httpd-2.2.11

[root@localhost httpd-2.2.11]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-expires --enable-cache --enable-mem-cache

……

……

[root@localhost httpd-2.2.11]# ls -tsl Makefile

16 -rw-r--r-- 1 root root 8999 08-19 15:50 Makefile

[root@localhost httpd-2.2.11]#

[root@localhost httpd-2.2.11]#make

……

……

[root@localhost httpd-2.2.11]#make install

……

……

 

 

 

 

 

 

 

 

 

 

 

 

 


 

经过上述步骤,Apache的代理服务httpd已经成功安装完成,后续要做一些小的配置调整。

2.2.2安装验证

安装完成后,做如下验证操作:

1)监听进程查看

 

 

 

 

[root@localhost php-5.3.0]# netstat -ant

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address               Foreign Address             State     

tcp        0      0 127.0.0.1:2208              0.0.0.0:*                   LISTEN     

tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN     

tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN    

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      

tcp        0      0 127.0.0.1:2207              0.0.0.0:*                   LISTEN     

tcp        0      0 0.0.0.0:831                 0.0.0.0:*                   LISTEN     

tcp      0      0 :::80                   :::*                    LISTEN      

tcp        0      0 :::22                       :::*                        LISTEN     

[root@localhost php-5.3.0]#

 

 

 

 

 

 


 

2)启动、重启动、停止apache服务

调用apachectl命令,须保证apachectl的调用路径准确。当然这与安装过程中./configure的prefix的配置参数有关。

[root@localhost httpd-2.2.11]#/usr/local/httpd/bin/apachectl -k start

[root@localhost httpd-2.2.11]# /usr/local/httpd/bin/apachectl –k restart

[root@localhost httpd-2.2.11]# /usr/local/httpd/bin/apachectl –k stop

[root@localhost httpd-2.2.11]# /usr/local/httpd/bin/apachectl -k start

 

 

 


 

   

 

3)服务访问验证

在浏览器打输入http://10.166.138.199/index4Apache.html

【默认安装的Apache,只需要输入发布服务的IP地址即可,默认访问80端口】

出现 It works!这说明服务已经安装部署成功,可以正常访问了。

2..2.3配置调整

暂无其他调整

 

 

2.3安装PHP

2.3.1安装步骤【具体操作步骤见文本框】

1)进入解压的目录

2)运行configure安装配置参数;

./configure --prefix=/usr/local/php--with-config-file-path=/usr/local/lib --with-apxs2=/usr/local/httpd/bin/apxs --with-zlib --enable-mbstring --enable-xml

Configure配置需要执行一些时间

#configure参数:

#--prefix是安装目录。

#--with-config-file-path是指定php配置文档php.ini所在的目录。

#--with-zlib安装zlib扩展,前提是linux已经安装了zlib,未安装可以去掉这个选项。

#安装mbstring和xml扩展。这些都是常用的模块。

3)生成makefile文件;

4)make

编译源码时间较长。

5)make test(非必要)

6)make install

7) 将php配置文档复制到/usr/local/lib

[root@localhost PHP]#cd  /opt/ ResourceDir/ PHP / php-5.3.0

[root@localhost php-5.3.0]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/lib --with-apxs2=/usr/local/httpd/bin/apxs  --with-zlib --enable-mbstring --enable-xml

……

……

[root@localhost php-5.3.0]# ls -tsl Makefile

216 -rw-r--r-- 1 root root 209220 08-23 14:36 Makefile

[root@localhost php-5.3.0]#make

……

……

[root@localhost php-5.3.0]#make install

……

……

[root@localhost php-5.3.0]# cp php.ini-development  /usr/local/lib/php.ini

[root@localhost php-5.3.0]# ls -tsl  /usr/local/lib

鎬昏 76

76 -rw-r--r-- 1 root root 68448 08-23 15:38 php.ini

[root@localhost php-5.3.0]#

 

 

 

 


注:PHP的其他验证工作要由Apache代理配合完成。后续完成配置修改后,进行验证。

 

2.4修改Apache和PHP配置

2.4.1配置apache关联php

1)打开apache配置文档httpd.conf,进行编辑或确认

vi /usr/local/httpd/conf/httpd.conf

vi编辑器打开httpd.conf,或者本地修改确认后,再上传。

2)确认LoadModule

打开后,直接输入/LoadModule后回车。确认LoadModule已经包含:

LoadModule php5_module       modules/libphp5.so

这个配置在安装PHP的configure参数已经设置好,安装时会修改Apache的httpd.conf配置文件,并将php标准库文件生成到/modules目录,这里需要确认下。

[root@localhost PHP]#vi /usr/local/httpd/conf/httpd.conf

……

# Dynamic Shared Object (DSO) Support

# To be able to use the functionality of a module which was built as a DSO you

# have to place corresponding `LoadModule' lines at this location so the

# directives contained in it are actually available _before_ they are used.

# Statically compiled modules (those listed by `httpd -l') do not need

# to be loaded here.

#

# Example:

# LoadModule foo_module modules/mod_foo.so

LoadModule php5_module       modules/libphp5.so

#LoadModule test_module        modules/test_module.so

#

<IfModule !mpm_netware_module>

<IfModule !mpm_winnt_module>

#

# If you wish httpd to run as a different user or group, you must run

# httpd as root initially and it will switch.

#

# User/Group: The name (or #number) of the user/group to run httpd as.

# It is usually good practice to create a dedicated user and group for

# running httpd, as with most system services.

/LoadModule

注:【/LoadModule是向下查找 LoadModule。而 ?LoadModule 则是向上查找。查找下一个则是按键n。】

 

 

 

 

 

 

 

 

 

 

3)确认DocumentRoot

用同样的方法,找到DocumentRoot。确认是:

DocumentRoot "/usr/local/httpd/htdocs"

之后发布页面文件的根目录就是指这个目录。以后就可以在这里创建php、html文件。

4)确认Directory

如果要修改上述的DocumentRoot,也要同步修改Directory。

<Directory "/usr/local/httpd/htdocs">

5)增加AddType类型

输入/AddType,回车查找。找到后,追加(a【vi】)或者插入(i【vi】)如下内容:

AddType application/x-httpd-php .php

然后点击 Esc ,退出插入编辑模式

6)增加或编辑DirectoryIndex

输入 /DirectoryIndex,回车查找。找到后,同样用追加或插入的方式,更新为如下内容:

<IfModule dir_module>

     DirectoryIndex index.php  index.html

</IfModule>

修改发布后,浏览器访问时如果没有指明那个文件,就默认打开index.php

7)修改项确认完毕,保存退出

点击 Esc 退出插入编辑模式,输入:wq后回车,即完成上述的修改保存。注:wq,保存并退出。

 

 

 


2.4.2配置PHP

[root@localhost PHP]#vi /usr/local/lib/php.ini

 

1) 打开php配置文档php.ini,进行修改或确认

 

2)修改php时区

php5默认是格林威治时间,不修改的话与北京时间相差8小时。

输入/timezone,回车查找

改成 date.timezone = PRC

3)显示notice警告提示

php5默认关闭notice警告提示,如果是在本地调试,建议打开notice警告提示。

输入/error_reporting,回车查找

改成

error_reporting = E_ALL

4)修改项确认完毕,保存退出

点击 Esc 退出插入编辑模式,输入:wq后回车,即完成上述的修改保存。

[root@localhost PHP]#

……

;;;;;;;;;;;;;;;;;;;

; Module Settings ;

;;;;;;;;;;;;;;;;;;;

 

[Date]

; Defines the default timezone used by the date functions

; http://php.net/date.timezone

;date.timezone =

date.timezone = PRC

……

; http://php.net/error-reporting

;error_reporting = E_ALL | E_STRICT

erroe_reporting =E_ALL

……

 

 

 

 

 

 

 

 

 

 

 

 

 



2.5建立PHP测试页面

2.5.1建立页面

1)vi vi /usr/local/httpd/htdocs/index.php

没有此文件的话,vi命令就会新建。

2)打开文件后,按键i,即进入修改模式,输入以下内容:

[root@localhost PHP]#vi /usr/local/httpd/htdocs/index.php

<html><title>Test PHP</title>

<head>

<?php

echo "Hello PHP!";

phpinfo();

?>

 

 

</head>

<body>

<br>

<hl>It works!</hl>

</body>

</html>

~

 

 

 

 

 

 

 

 

 

 

 

 

 

 


3)按键ESC,即退出修改编辑模式。

4)按键:wq,退出并保存上述修改内容。

 

 


2.6配置完成,验证PHP和Apache服务

2.6.1重启apache服务

[root@localhost PHP]# /usr/local/httpd/bin/apachectl -k restart

 

2.6.2错误提示修订

如果出现类似的这样的错误,说明受selinux限制。

[root@localhost PHP]#

httpd: Syntax error on line 53 of

/usr/local/httpd/conf/httpd.conf: Cannot load

/usr/local/httpd/modules/libphp5.so into server:

/usr/local/httpd/modules/libphp5.so: cannot restore segment prot after reloc: Permission denied

 

 

 

 

 

 

 


桌面系统上会有“提示”弹出,可以根据提示将其关闭。或者执行以下命令:

[root@localhost PHP]#chcont texrel_shlib_t /usr/local/httpd/modules/libphp5.so

 

 

 


再次重启apache

2.6.3浏览访问验证

在浏览器输入 http://10.166.138.199

回车后,可浏览以下内容,比较多,挑选了几个作为示例【phpinfo()根据php标准库的libphp5.so动态共享对象来获取服务内容】:

1)PHP基本信息

2)xml和zlib

3)mbstring

 

2.7随系统启动Apache代理服务

默认安装的Apache并不是随操作系统的启动而启动。可以通过如下修改,随系统启动apache代理服务。

[root@localhost PHP]# vi /etc/rc.d/rc.local

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.

 

touch /var/lock/subsys/local

/usr/local/httpd/bin/apachectl -k start

~

1)vi 模式打开rc.local文件,做如下修改:

 

 

 

 

2)点击 Esc,退出插入编辑模式

3)直接输入:wq

回车,即保存并退出。

以后,启动操作系统时,Apache代理服务即可自启动了。

OK!Apache和PHP的安装配置基本完成,可以在虚拟机上正常发布访问了。下一步去做一个简单的PHP开源代码扩展模块练练手吧!

 

3. PHP-WEB(添加php5开源代码的扩展模块test_module)

3.1创建PHP扩展模块及修改配置:

1)进入.. php-5.3.0/ext目录;

2)运行./ext_skel --extname=test_module【test_module为自定义名称,不与ext目录下其他模块名称重复即可】

3)生成扩展模块test_module

具体过程如下:

[root@localhost ~]# cd /opt/ResourceDir/PHP/php-5.3.0/ext

[root@localhost ext]# ./ext_skel --extname=test_module

Creating directory test_module

Creating basic files: config.m4 config.w32 .cvsignore test_module.c php_test_module.h CREDITS EXPERIMENTAL tests/001.phpt test_module.php [done].

 

To use your new extension, you will have to execute the following steps:

 

1.  $ cd ..

2.  $ vi ext/test_module/config.m4

3.  $ ./buildconf

4.  $ ./configure --[with|enable]-test_module

5.  $ make

6.  $ ./php -f ext/test_module/test_module.php

7.  $ vi ext/test_module/test_module.c

8.  $ make

 

Repeat steps 3-6 until you are satisfied with ext/test_module/config.m4 and

step 6 confirms that your module is compiled into PHP. Then, start writing

code and repeat the last two steps as often as necessary.

 

[root@localhost ext]#

 

 4)进入扩展模块路径

[root@localhost ext]# cd test_module/

[root@localhost test_module]# ls -l

总计 64

-rw-r--r-- 1 root root 2194 08-22 14:22 config.m4

-rw-r--r-- 1 root root  331 08-22 14:22 config.w32

-rw-r--r-- 1 root root   11 08-22 14:22 CREDITS

-rw-r--r-- 1 root root    0 08-22 14:22 EXPERIMENTAL

-rw-r--r-- 1 root root 2951 08-22 14:22 php_test_module.h

-rw-r--r-- 1 root root 5438 08-22 14:22 test_module.c

-rw-r--r-- 1 root root  517 08-22 14:22 test_module.php

drwxr-xr-x 2 root root 4096 08-22 14:22 tests

[root@localhost test_module]#

 

 

5)修改../test_module/config.m4:

[root@localhost test_module]# vi config.m4

 

 


输入/ PHP_ARG_ENABLE,回车找到后,i进入编辑模式,将如下内容:

dnl Otherwise use enable:

 

dnl PHP_ARG_ENABLE(test_module, whether to enable test_module support,

dnl Make sure that the comment is aligned:

dnl [  --enable-test_module           Enable test_module support])

 

 修改为:

dnl Otherwise use enable:

 

PHP_ARG_ENABLE(test_module, whether to enable test_module support,

Make sure that the comment is aligned:

[  --enable-test_module           Enable test_module support])

 

 

[root@localhost ext]#cd /opt/ResourceDir/PHP/php-5.3.0/

[root@localhost php-5.3.0]#./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/lib --with-apxs2=/usr/local/httpd/bin/apxs  --with-zlib --enable-mbstring --enable-xml

6 )执行configure参数配置

 

[root@localhost ext]#cd /opt/ResourceDir/PHP/php-5.3.0/

[root@localhost php-5.3.0]#./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/lib --with-apxs2=/usr/local/httpd/bin/apxs  --with-zlib --enable-mbstring --enable-xml

   

7)修改扩展模块php_test_module.h

修改 php_test_module.h,将confirm_my_module_compiled函数修改为test_module;增加函数sayhello;如下:

[root@localhost test_module]#cd ext/test_module

[root@localhost test_module]#vi php_test_module.h

PHP_FUNCTION(confirm_my_module_compiled);

PHP_FUNCTION(sayhello);

 

修改为

PHP_FUNCTION(test_module);

PHP_FUNCTION(sayhello);

 

8)修改扩展模块test_module.c

修改 PHP_FE(test_module,NULL),并追加PHP_FE(sayhello,NULL)的定义;

/*

  +----------------------------------------------------------------------+

  | PHP Version 5                                                        |

  +----------------------------------------------------------------------+

  | Copyright (c) 1997-2008 The PHP Group                                |

  +----------------------------------------------------------------------+

  | This source file is subject to version 3.01 of the PHP license,      |

  | that is bundled with this package in the file LICENSE, and is        |

  | available through the world-wide-web at the following url:           |

  | http://www.php.net/license/3_01.txt                                  |

  | If you did not receive a copy of the PHP license and are unable to   |

  | obtain it through the world-wide-web, please send a note to          |

  | license@php.net so we can mail you a copy immediately.               |

  +----------------------------------------------------------------------+

  | Author:                                                              |

  +----------------------------------------------------------------------+

*/

/* $Id: header,v 1.16.2.1.2.1.2.1 2008/02/07 19:39:50 iliaa Exp $ */

 

#ifdef HAVE_CONFIG_H

#include "config.h"

#endif

 

#include "php.h"

#include "php_ini.h"

#include "ext/standard/info.h"

#include "php_test_module.h"

 

/* If you declare any globals in php_test_module.h uncomment this:

ZEND_DECLARE_MODULE_GLOBALS(test_module)

*/

/* True global resources - no need for thread safety here */

static int le_test_module;

 

/* {{{ test_module_functions[]

 * * Every user visible function must have an entry in test_module_functions[].

 */

const zend_function_entry test_module_functions[] = {

        PHP_FE(sayhello,NULL)

       PHP_FE(test_module,NULL)               /* For testing, remove later. */

        {NULL, NULL, NULL}      /* Must be the last line in test_module_functions[] */

};

/* }}} */

 


 

并在文件最后,追加实现上述两个函数:

/* Remove the following function when you have succesfully modified config.m4

   so that your module can be compiled into PHP, it exists only for testing

   purposes. */

 

/* Every user-visible function in PHP should document itself in the source */

/* {{{ proto string confirm_test_module_compiled(string arg)

   Return a string to confirm that the module is compiled in */

PHP_FUNCTION(test_module)

{

        RETURN_STRING("This is a Test Module!",1);

}

/* }}} */

/* The previous line is meant for vim and emacs, so it can correctly fold and

   unfold functions in source code. See the corresponding marks just before

   function definition, where the functions purpose is also documented. Please

   follow this convention for the convenience of others editing your code.

*/

 

PHP_FUNCTION(sayhello)

{

        zend_printf("hello world!\n");

}

 

3.2编译扩展模块test_module

1)进入../php-5.3.0/ext路径

2)执行以下操作,生成.so文件的DSO:

[root@localhost test_module]# cd /opt/ResourceDir/PHP/php-5.3.0/ext/

[root@localhost ext]# gcc -fpic -DCOMPILE_DL_TEST_MODULE=1 -I/usr/local/include -I. -I../main -I.. -I../TSRM -I../Zend -c -otest_module/test_module.o test_module/test_module.c

[root@localhost ext]# gcc -shared -L/usr/local/lib -rdynamic -otest_module/test_module.so test_module/test_module.o

 3)生成的test_module.so文件在test_module目录下。

 

3.3手动加载DSO(Dynamic Share Object)

将上述的test_module.so文件copy到Apache目录,以备自动加载。具体目标路径,验证查看的操作如下:

[root@localhost test_module]# cp test_module.so /usr/local/httpd/modules/

……

[root@localhost test_module]#ls –l /usr/local/httpd/modules/

-rw-r--r-- 1 root root     9046 08-19 15:53 httpd.exp

-rwxr-xr-x 1 root root 19606350 08-19 16:46 libphp5.so

-rwxr-xr-x 1 root root     6102 08-23 17:01 test_module.so

 

 

 

 

 


3.4修改php.ini[w2] 

[root@localhost test_module]# vi /usr/local/lib/php.ini

……

   extension=/usr/local/httpd/modules/test_module.so

 

 

 [w2]   加载多个扩展extension=/usr/local/httpd/modules/XXX_web.so

    extension=/usr/local/httpd/modules/test_module.so


3.5编写扩展模块的验证页面

[root@localhost test_module]# vi /usr/local/httpd/htdocs/test.php

……

 

<html><title>Test Externtion</title>

<head>

<?php

echo "Hello PHP!\n";

sayhello();

$value_test=test_module();

echo $value_test;

?>

</head>

<body>

<br>

<hl>It works!</hl>

</body>

</html>

~

 

 保存退出后,即可进行访问验证。

3.6浏览器访问验证

打开浏览器,输入http://10.166.138.199/test.php


 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值