1、安装php
(1)获得PHP源码:php-5.4.1.tar.gz(最新版本为5.4.2),保存到/usr/local目录下。下载地址:http://cn.php.net/get/php-5.4.2.tar.gz/from/a/mirror。
(2)解压缩源码文件:
#tar -zvxf php-5.4.1.tar.gz
(3)安装libxm12以及libXML2-devel,不然编译源码时会出现“Configure: error: xml2-config not found. Please check your libxml2 installation.”问题。
#yum install -y libxml2 libxm12-devel
(4)编译源码:
#cd /usr/local/php-5.4.1
#./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-MySQL=/usr/local/mysql
#make
(5)安装
#make install
2、配置PHP
(1)将PHP源码包(/usr/local/php-5.4.1)中的php.ini-development文件复制到/usr/local/lib/下,更名为php.ini。
#cp /usr/local/php-5.4.1/php.ini-development /usr/local/lib/php.ini
(2)修改Apache配置文件(gedit /usr/local/apache2/conf/httpd.conf)以支持对PHP的解析。如果httpd.conf中没有下列语句,就将它们分别添加到LoadModule和AddType项的后面。
LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php
在DirectoryIndex index.html index.html.var一行后加入index.php,即改为:
DirectoryIndex index.html index.html.var index.php
重启Apache服务器:
#/usr/local/apache2/bin/apachectl restart
(3)测试PHP:
在Apache服务器的文件根目录(/usr/local/apache2/htdocs/)下新建一个PHP文件test.php,并输入以下内容:
《?
phpinfo();
?>
在浏览器中输入http://localhost/test.php,如果看到下图,则表示已成功安装了PHP。
以上参考《基于Linux的Web程序设计PHP网站开发》一书。
3、问题及解决
问题1:“Cannot load /usr/local/apache2/modules/libphp5.so into server: /usr/local/apache2/modules/libphp5.so”
[root@localhost ~]# /usr/local/apache2/bin/apachectl restart
httpd: Syntax error on line 144 of /usr/local/apache2/conf/httpd.conf: Cannot load /usr/local/apache2/modules/libphp5.so into server: /usr/local/apache2/modules/libphp5.so: cannot restore segment prot after reloc: Permission denied
[root@localhost ~]# ls -lh /usr/local/apache2/modules/libphp5.so
-rwxr-xr-x 1 root root 18M May 5 19:21 /usr/local/apache2/modules/libphp5.so
[root@localhost ~]# service httpd restart
Stopping httpd: [FAILED]
Starting httpd: [ OK ]
[root@localhost ~]# setenforce 0
[root@localhost ~]# chcon -c -v -R -u system_u -r object_r -t textrel_shlib_t /usr/local/apache2/modules/libphp5.so
context of /usr/local/apache2/modules/libphp5.so changed to system_u:object_r:textrel_shlib_t
[root@localhost ~]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@localhost ~]# setenforce 1
参考:Cannot load /usr/local/apache2/modules/libphp5.so into server: /usr/local/apache2/modules/libphp5.s
问题2:“(98)Address already in use: AH00072: make_sock: could not bind to address [::]:80”
[root@localhost ~]# /usr/local/apache2/bin/apachectl restart
httpd not running, trying to start
(98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
(98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
AH00015: Unable to open logs
[root@localhost ~]# netstat -lnp|grep 80
tcp 0 0 :::80 :::* LISTEN 30735/httpd
unix 2 [ ACC ] STREAM LISTENING 18053 4742/ssh-agent /tmp/ssh-alQwqZ4706/agent.4706
unix 2 [ ACC ] STREAM LISTENING 18337 4803/metacity /tmp/orbit-bingo/linc-12c3-0-1fd1658ce18f6
unix 2 [ ACC ] STREAM LISTENING 18394 4809/nautilus /tmp/orbit-bingo/linc-12c9-0-253df8021e3e
unix 2 [ ACC ] STREAM LISTENING 18431 4807/gnome-panel /tmp/orbit-bingo/linc-12c7-0-253df802d473
unix 2 [ ACC ] STREAM LISTENING 18460 4823/gnome-volume-m /tmp/orbit-bingo/linc-12cb-0-253df803776b
[root@localhost ~]# ps 30735
PID TTY STAT TIME COMMAND
30735 ? Ss 0:00 /usr/sbin/httpd
[root@localhost ~]# kill 30735
[root@localhost ~]# /usr/local/apache2/bin/apachectl restart
httpd not running, trying to start
[root@localhost ~]# /usr/local/apache2/bin/httpd -k start
httpd (pid 31918) already running
¥40.00
¥9.90
¥20.00
¥516.00