1.安装依赖的类库:
yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses curl curl-devel openssl-devel gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel libmcrypt-devel libmcrypt
2.执行编译命令:
下载并解压php-5.6.21.tar.bz2
cd php-5.6.21
./configure --prefix=/usr/local/php –with-mysql=mysqlnd –with-mysqli=mysqlnd –with-pdo-mysql=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
在安装过程中可能会遇到报错,比如libxml2、bzip2之类的问题,解决方法是安装他们的开发包就好,开发包一般为包名加devel,然后重新配置(比如:yum install bzip2-devel),
编译出现的问题:Cannot find libmysqlclient_r under /usr/local/mysql.
解决方法:编译之前,先处理一下mysql的库,默认查找libmysqlclient_r.so,可是mysql默认为libmysqlclient.so,内容完全一样,做个链接即可
# cd /usr/local/mysql/lib
# ln -s libmysqlclient.so.15.0.0 libmysqlclient_r.so
3.编译通过后
# make && make install
4.编译安装完成后为PHP提供配置文件:
# cp php.ini-production /etc/php.ini
编辑apache配置文件httpd.conf,让apache支持php,添加如下内容:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
让Apache识别 .php 文件,定位到DirectoryIndex修改为:
DirectoryIndex index.php index.html
之后要重启apache
5.然后修改网站首页文件看看是否正常解析PHP网页了:
cd /usr/local/apache/htdocs/
mv index.html index.php
vim index.php
添加如下内容:
<?php
phpinfo();
?>
浏览器访问网页如果显示php的相关配置页则安装成功了
6.测试mysql与php
在mysql中执行如下操作:
mysql> create table person( id int auto_increment primary key, name varchar(225) );
Query OK, 0 rows affected (0.41 sec)
mysql> insert into person values (null,'liming');
Query OK, 1 row affected (1.79 sec)
mysql> insert into person values (null,'zhangsan');
Query OK, 1 row affected (0.05 sec)
mysql> insert into person values (null,'wangwu');
Query OK, 1 row affected (0.00 sec)
测试的php文档如下
<?php
$link = mysql_connect('localhost', 'root', '你的密码');
mysql_select_db('demo',$link);
mysql_query('set names utf-8');
$query_sql = 'select * from person';
$result = mysql_query($query_sql);
$data = array();
while($row = mysql_fetch_assoc($result)){
$data[] = $row;
}
echo '<pre>';
print_r($data);
echo '</pre>';
mysql_close($link);
?>
访问以上测试文档,如果能正确显示数据库信息则表示成功
7.其他
出现的问题a:
MySQL通过localhost无法连接127.0.0.1可以连接的问题,发现默认的localhost不能连接mysql数据库,但是通过127.0.0.1又可以连接。
解决方法:
最保守的方法就是先执行 sync 命令进行数据写入。然后重启apache2服务器。一般来说,不用执行这两个命令也行的。
find / -name mysql.sock
或
mysqld --verbose --help | grep socket
Buffer length for TCP/IP and socket communication
…………...省略
socket /var/lib/mysql/mysql.sock(重点)
然后根据相关目录/var/lib/mysql/mysql.sock修改/etc/php.ini:
msyql.default_socket = /var/lib/mysql/mysql.sock
msyql.default_socket = /var/lib/mysql/mysql.sock
然后sync,并重启apache
问题b(在安装php的过程中可能会遇到):
Starting MySQL.. ERROR! The server quit without updating PID file (/usr/local/mysql/data/virtualdarkgel.com.pid).
解决方法
在/etc/my.conf中加
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid