centos X64下安装php5.5.6

来源:互联网 发布:笑傲江湖ol 知乎 编辑:程序博客网 时间:2024/06/01 09:12

一.安装常用命令

yum -y install wget make zip unzip patch

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libevent libevent-devel

下载软件

mkdir ~/soft
cd ~/soft

wget -c http://cn2.php.net/distributions/php-5.5.16.tar.gz
wget -c http://tengine.taobao.org/download/tengine-2.0.3.tar.gz

wget -c http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.39-linux2.6-x86_64.tar.gz

wget http://www.imagemagick.org/download/ImageMagick-6.8.9-7.tar.gz
wget http://pecl.php.net/get/imagick-3.1.2.tgz

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
wget http://pecl.php.net/get/memcache-2.2.7.tgz
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz

yum install libmcrypt libmcrypt-devel mcrypt mhash

或者从下面下载包编译安装

wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?big_mirror=0"
wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?big_mirror=0"
wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?big_mirror=0"

一.安装mysql

cd ~/soft/

groupadd mysql
useradd -g mysql -d /usr/local/mysql -s /sbin/nologin mysql

需要先安装libaio库

#yum -y install libaio

tar zxvf mysql-5.5.39-linux2.6-x86_64.tar.gz
mv mysql-5.5.39-linux2.6-x86_64 /usr/local/mysql
cd /usr/local/mysql
chown -R mysql .
chgrp -R mysql .

scripts/mysql_install_db --user=mysql

chown -R root .
chown -R mysql data

修改 /etc/my.cnf 文件,将socket文件位置修改如下

socket=/tmp/mysql.sock

# create mysql pid work dir
mkdir /var/run/mysqld/
chown -R mysql:mysql /var/run/mysqld/
bin/mysqld_safe --user=mysql &

# Next command is optional

cp support-files/mysql.server /etc/init.d/mysql.server

#修改mysql密码

bin/mysqladmin -u root password 'new-password'
bin/mysql -u root -p

注册系统服务

cp support-files/mysql.server /etc/init.d/mysqld

#把msql的脚本文件拷到系统的启动目录下

cd /etc/init.d/
chkconfig --add mysqld #将mysql加到启动服务列表里
chkconfig mysqld on #让系统启动时自动打开mysql服务,如果指定级别,用--level参数

这里为了以后维护的时候,不用输入完整的路径,做了软链接

ln -s /usr/local/mysql/bin/mysqldump /usr/sbin/mysqldump
ln -s /usr/local/mysql/bin/mysqld_safe /usr/sbin/mysqld_safe
ln -s /usr/local/mysql/bin/mysqlslap /usr/sbin/mysqlslap
ln -s /usr/local/mysql/bin/mysql /usr/sbin/mysql
ln -s /usr/local/mysql/bin/mysqladmin /usr/sbin/mysqladmin

我们这里将sock放在了/tmp目录里.有时候系统会使用默认的/var/lib/mysql/mysql.sock文件,为了兼容这个情况可以建立一个链接:

ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

二.安装php

#cd ~/soft

1. 首先安装iconv,否则安装php的时候会提示这个错误

cd ~/soft/
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make
make install
cd ../

#####################################################################

#yum install libmcrypt libmcrypt-devel mcrypt mhash

或者使用下面源包编译安装

下载地址

http://sourceforge.net/projects/mcrypt/files/Libmcrypt/
http://sourceforge.net/projects/mcrypt/files/MCrypt/
http://sourceforge.net/projects/mhash/files/mhash/

先安装Libmcrypt,再安装mhash,再安装mcrypt

2 .先安装Libmcrypt

tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
#make
#make install 说明:libmcript默认安装在/usr/local
#cd ../

3.安装mhash

tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make
make install
cd ../

4.安装mcrypt

tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
LD_LIBRARY_PATH=/usr/local/lib ./configure
make
make install
cd ../

最后,还是检查下,是否安装成功

如果configure mcrypt时提示找不到libmcrypt的库,给个参考的解决方法

export LD_LIBRARY_PATH=/usr/local/lib: LD_LIBRARY_PATH

######################################################################

tar zxvf php-5.5.16.tar.gz
cd php-5.5.16
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear

make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-production /usr/local/php/etc/php.ini
cd ../

在configure过程中如果出现以下错误:

configure: error: Cannot find ldap libraries in /usr/lib.

解决办法:

cp -frp /usr/lib64/libldap* /usr/lib

如果出现“configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no”之类的错误
解决办法:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.so /usr/lib/
sudo ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18

在/etc/ld.so.conf.d中添加local.conf文件:

#echo "/usr/local/lib">> /etc/ld.so.conf.d/local.conf

更新动态库:

#ldconfig -v

如果在make的过程中出现以下错误:

/root/dev/php-5.*.*/sapi/cli/php: error while loading shared libraries:  libmysqlclient.so.18: cannot open shared object file: No such file or  directory
make: *** [ext/phar/phar.php] Error 127

解决办法  (http://blog.haohtml.com/archives/12479)

ln -s /usr/local/mysql/lib/libmysqlclient.so.18  /usr/lib64/

如果按上面的操作,再次执行 make ZEND_EXTRA_LIBS='-liconv' 后提示以下错误:

"chmod: cannot access `ext/phar/phar.phar': No such file or directory"

只需要重装执行上面的configure命令即可.只需要在./configure的后面加上--without-pear 即可解决办法:http://blog.haohtml.com/archives/12482

如果在make install的时候还提示上面类似的错误,只需要重新从./configure开始再执行一下就可以了.

其它错误参考:http://hi.baidu.com/cfanbo/item/8ff7d41f4fde6c001994ec43

三.安装php扩展

tar zxvf memcache-2.2.7.tgz
cd memcache-2.2.7/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

########################################

tar zxvf ImageMagick-6.8.9-7.tar.gz
cd ImageMagick-6.8.9-7/
./configure
make
make install
cd ../

########################################

tar zxvf imagick-3.1.2.tgz
cd imagick-3.1.2/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

可是出错了一个问题.就是死说找不到MagickWand.h:

checking for MagickWand.h header file... configure: error: Cannot locate header file MagickWand.h

可是我明明正确安装了ImageMagick的呀.GOOGLE了半天,也解决不了这个问题.后面对照另一个服务器上的ImageMagick才发现,原来ImageMagick 6.8这个版后的目录结构变了,旧版本头文件是放在/usr/local/include/ImageMagick目录的,而ImageMagick 6.8则是放在/usr/local/include/ImageMagick-6

添加软连接,命令如下:

ln -s /usr/local/include/ImageMagick-6 /usr/local/include/ImageMagick
make && make && install

编译通过!

#############################################

安装pdo_mysql扩展

cd ~/soft/php-5.5.16/ext/
cd pdo_mysql
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make
make install

#如果这里的方法不行,需要使用独立的安装包来编译了http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz,这里使用这种方法有问题。

//配置php.ini

vi /usr/local/php/etc/php.ini

找到extension_dir,删除前面的;,在后面指定为下面的目录位置

/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/

并在此行后增加以下几行,然后保存:

extension = "memcache.so"
extension = "imagick.so"
extension = "pdo_mysql.so"

1.再查找output_buffering = Off,修改为 output_buffering = On.

2.为了安装起见,隐藏http头信息里的php信息,查找 expose_php = on 修改为 expose_php = off

3.再查找; cgi.fix_pathinfo=0,把前面的;注释符号删除,改为 cgi.fix_pathinfo=0,预防方法:防止Nginx文件类型错误解析漏洞。

4.找到;date.timezone= 修改为date.timezone = PRC,修正php中于真实时间相关8小时的问题.

在php.ini文件最后添加启用opcache功能的选项

zend_extension=opcache.so
[opcache]
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable=1
opcache.enable_cli=1

配置php-fpm

//创建www用户,php-fpm和nginx统一使用这个

groupadd www
useradd -g www www

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

vi /usr/local/php/etc/php-fpm.conf

修改以下两行,并将服务用户名和用户所在组(nobody),修改为www

user = www
group = www

启用php-fpm

/usr/local/php/sbin/php-fpm

停止的话,用

killall php-fpm

四 安装NGINX

#安装正则表达式库,支持rewite

cd ~/soft
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35/
./configure
make && make install
cd ../

tar zxvf tengine-2.0.3.tar.gz
cd tengine-2.0.3/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../

vi /usr/local/nginx/conf/nginx.conf

修改 /usr/local/nginx/conf/nginx.conf,删除user nobody;行前面的注释,并修改为 user www www;
将以下几行前面的注释删除,将修改fastcgi_param后面的路径

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}

测试nginx.conf配置文件

/usr/local/nginx/sbin/nginx -t

启用nginx

/usr/local/nginx/sbin/nginx

重新加载配置文件

/usr/local/nginx/sbin/nginx -s reload

五.全局配置

vi /etc/rc.local

按shift+g快捷键,在末尾增加以下内容:

ulimit -SHn 65535
/usr/local/php/sbin/php-fpm
/usr/local/nginx/sbin/nginx

测试是否支持php

vi /usr/local/nginx/html/phpinfo.php

输入内容 <?php phpinfo(); ?>

浏览http://ip地址/phpinfo.php,可以看到php的相关信息,可以查看扩展是否支持.

以上使用的是nginx的默认配置,为了充分发挥nginx的性能,实际生产过程中,我们还需要对nginx进行一些配置优化,请参考:Nginx优化配置(转)

参考文章:http://blog.haohtml.com/archives/6051

参考:http://hi.baidu.com/cfanbo/item/9482bf3ced39f0c11b969681