Centos5.8下编译安装PHP5.4和memcached, phalcon, yaf, apc

安装GIT

需要先安装gcc-c++ (sudo yum install gcc-c++)
sudo yum install gettext-devel expat-devel cpio perl openssl-devel zlib-devel
wget https://www.kernel.org/pub/software/scm/git/git-1.9.0.tar.gz
tar -zxvf git-1.9.0.tar.gz
cd git-1.9.0
./configure
sudo make
sudo make install
git -version

Install memcached

sudo yum install libevent-devel
wget http://www.memcached.org/files/memcached-1.4.17.tar.gz
tar -zxvf memcached-1.4.17.tar.gz
./configure -prefix=/opt/memcache
sudo make
sudo make install
sudo cp scripts/memcached.sysv /etc/rc.d/init.d/memcached
sudo vim /etc/rc.d/init.d/memcached

Two places need change:
==1======
PORT=11211
USER=memcached
MAXCONN=1024
CACHESIZE=64
OPTIONS=””

==2======
start () {
echo -n $”Starting $prog: ”
# insure that /var/run/memcached has proper permissions
chown $USER /opt/memcache/bin/memcached
daemon /opt/memcache/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $M
AXCONN -P /opt/memcache/bin/memcached/memcached.pid $OPTIONS

Add user memcached before starting memcached service:
sudo /usr/sbin/groupadd memcached
sudo /usr/sbin/useradd -g memcached memcached
sudo passwd memcached

安装PHP

yum -y install bzip2 bzip2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel libmcrypt libmcrypt-devel libxml2-devel

(mysql是编译安装的)
./configure -prefix=/opt/php -with-apxs2=/opt/apache2/bin/apxs -with-libxml-dir=/usr/lib64 -with-mysql=/opt/mysql -with-mysqli=/opt/mysql/bin/mysql_config -with-gd -enable-gd-native-ttf -with-zlib -with-mcrypt -with-pdo-mysql=/opt/mysql -enable-shmop -enable-soap -enable-sockets -enable-wddx -enable-zip -with-xmlrpc -enable-fpm -enable-mbstring -with-zlib-dir -with-bz2 -with-curl -enable-exif -enable-ftp -with-jpeg-dir=/usr/lib64 -with-png-dir=/usr/lib64 -with-freetype-dir=/usr/lib64

or
(mysql是通过官方提供的rpm 安装的情况, 注意安装的rpm除了server外, client, share, devel都要装, 最后一个 -with-mysql –with-libdir=/usr/lib64 费了我好久才试成功)
./configure -prefix=/opt/php -with-apxs2=/usr/sbin/apxs -with-libxml-dir=/usr/lib64 -with-pdo-mysql=/usr/bin/mysql -with-mysqli=/usr/bin/mysql_config -with-mysql –with-libdir=/usr/lib64 -with-gd -enable-gd-native-ttf -with-zlib -with-mcrypt -enable-shmop -enable-soap -enable-sockets -enable-wddx -enable-zip -with-xmlrpc -enable-fpm -enable-mbstring -with-zlib-dir -with-bz2 -with-curl -enable-exif -enable-ftp -with-jpeg-dir=/usr/lib64 -with-png-dir=/usr/lib64 -with-freetype-dir=/usr/lib64

如果使用centos5.8 自带的apache2.2, 找不到apxs的, 可以安装 yum install httpd-devel
./configure -prefix=/opt/php -with-apxs2=/usr/sbin/apxs -with-libxml-dir=/usr/lib64 -with-mysql=/opt/mysql -with-mysqli=/opt/mysql/bin/mysql_config -with-gd -enable-gd-native-ttf -with-zlib -with-mcrypt -with-pdo-mysql=/opt/mysql -enable-shmop -enable-soap -enable-sockets -enable-wddx -enable-zip -with-xmlrpc -enable-fpm -enable-mbstring -with-zlib-dir -with-bz2 -with-curl -enable-exif -enable-ftp -with-jpeg-dir=/usr/lib64 -with-png-dir=/usr/lib64 -with-freetype-dir=/usr/lib64

sudo make
sudo make install

看以下输出, libphp5.so已经被添加到httpd 的modules目录, 另外已经更新了httpd.conf
####################
Installing PHP SAPI module:       apache2handler
/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL=’/usr/lib64/apr-1/build/libtool’ libphp5.la /usr/lib64/httpd/modules
/usr/lib64/apr-1/build/libtool –mode=install cp libphp5.la /usr/lib64/httpd/modules/
cp .libs/libphp5.so /usr/lib64/httpd/modules/libphp5.so
cp .libs/libphp5.lai /usr/lib64/httpd/modules/libphp5.la
libtool: install: warning: remember to run `libtool –finish /home/milton/backup/php-5.5.13/libs’
chmod 755 /usr/lib64/httpd/modules/libphp5.so
[activating module `php5′ in /etc/httpd/conf/httpd.conf]
####################

在/etc/httpd/conf.d 下添加 php.conf, 写入如下几行:
#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
DirectoryIndex index.php

AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps

重启httpd

sudo cp /data/backup/php-5.4.26/php.ini-production /opt/php/etc/php.ini
上面这个不对, 如果不指定路径(-with-config-file-path=/opt/php/etc )的话, 从phpinfo可以看到, 这个路径是指向/opt/php/lib的, 所以得放到/opt/php/lib下去, 否则后面的修改不起作用

sudo vim conf/httpd.conf

1. 添加php支持。

AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps

2. 添加默认索引页面index.php,再找到“DirectoryIndex”,在index.html后面加上“ index.php”

DirectoryIndex index.html index.php

3. 不显示目录结构,找到“Options Indexes FollowSymLinks”,修改为

Options FollowSymLinks

4. 开启Apache支持伪静态,找到“AllowOverride None”,修改为

AllowOverride All

httpd开启非80端口, 因为有selinux的限制, 会出现类似于
Starting httpd: (13)Permission denied: make_sock: could not bind to address [::]:10080
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:10080
no listening sockets available, shutting down
Unable to open logs
的错误,
可以通过 sudo /usr/sbin/semanage port -l  查看当前的端口状态
(查看selinux是否启用: sudo /usr/sbin/sestatus)
(查看selinux状态: sudo /usr/sbin/getenforce)
(设置selinux状态: sudo /usr/sbin/setenforce  0 or 1)
通过
semanage port -a -t http_port_t -p tcp 10080
添加, 系统提示 /usr/sbin/semanage: Port tcp/10080 already defined
查看  
sudo /usr/sbin/semanage port -l |grep 10080
amanda_port_t                  tcp      10080, 10081, 10082, 10083
amanda_port_t                  udp      10080, 10081
再换一个
semanage port -a -t http_port_t -p tcp 10088

安装memcached php扩展

sudo yum install gcc44 gcc44-c++ libstdc++44-devel
这几个必须装, 否则 libmemcached 在make的时候会报错 ( error: cinttypes: No such file or directory)

先安装 libmemcached (这是一个memcache的客户端类库, 编译pecl memcached的时候要用到)
tar -zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
CC=gcc44 CXX=g++44 ./configure // 这里把编译器换成 gcc44
sudo make
sudo make install

sudo yum install cyrus-sasl-devel

sudo yum install autoconf
这个必须装, 因为phpize 要用到

wget http://pecl.php.net/get/memcached-2.1.0.tgz
tar -zxvf memcached-2.1.0.tgz
cd memcached-2.1.0
/opt/php/bin/phpize
./configure -enable-memcached -with-php-config=/opt/php/bin/php-config -with-libmemcacheddir=/usr/local/lib
上面的是错的, 妈的试了半天, 参数不对以外, 这个目录也有讲究, 必须包含的文件是: include/libmemcached/memcached.h (注意路径!), 不然会报错: error: Can’t find libmemcached 1.0.x headers
下面的才是对的:
./configure -enable-memcached -with-php-config=/opt/php/bin/php-config -with-libmemcached-dir=/usr/local/

然后是make, make install,
完成后打开 /opt/php/lib/php.ini, 找到
; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = “./”
上面这行取消注释, 换成make install结束时提供的路径, 一般是 extension_dir = “/opt/php/lib/php/extensions/no-debug-non-zts-20121212/”
然后在一系列的extension=XXX.dll 后面添加
extension=memcached.so

重启httpd后发现memcached没有启用, 查看httpd的error_log, 发现
PHP Warning:  PHP Startup: Unable to load dynamic library ‘/opt/php/lib/php/extensions/no-debug-non-zts-20121212/memcached.so’ – /opt/php/lib/php/extensions/no-debug-non-zts-20121212/memcached.so: failed to map segment from shared object: Permission denied in Unknown on line 0
这是因为selinux安全设置引起的错误
关闭selinux ( sudo /usr/sbin/setenforce  0) 后再重启httpd, memcached就可以用了.
解决办法, 参考 http://www.sysarchitects.com/apc_on_rhel5
查看so文件的安全信息:
ls -Z /opt/php/lib/php/extensions/no-debug-non-zts-20121212/
-rwxr-xr-x  root root user_u:object_r:usr_t            memcached.so
重置:
sudo /sbin/restorecon /opt/php/lib/php/extensions/no-debug-non-zts-20121212/memcached.so
再次查看
ls -Z /opt/php/lib/php/extensions/no-debug-non-zts-20121212/
-rwxr-xr-x  root root system_u:object_r:lib_t          memcached.so
-rwxr-xr-x  root root user_u:object_r:usr_t            opcache.a
-rwxr-xr-x  root root user_u:object_r:usr_t            opcache.so
启用selinux
sudo /usr/sbin/setenforce 1
再次重启httpd, 就可以用了

启用OPcache:
因为PHP5.5开始, opcache已经是默认安装的cache模块, 用于取代apc, 只是未启用
需要在php.ini 中添加
zend_extension=/opt/php/lib/php/extensions/no-debug-non-zts-20121212/opcache.so
注意必须用zend_extension
重启httpd时依然会碰到上面的Permission denied in Unknown on line 0问题, 一样的用restorecon处理就好了.

额外的: iptables 的用法
启动/关闭: sudo /sbin/service iptables start/stop
查看: sudo iptables -L -n
编辑: sudo vi /etc/sysconfig/iptables

安装phalcon

git clone -depth=1 git://github.com/phalcon/cphalcon.git
cd cphalcon/
cd build/

sudo ./install

在运行install之前, 需要修改install 脚本:
主要是: 两处phpize的路径, 一处增加-with-php-config=/opt/php/bin/php-config
如下:
#Clean current compilation
if [ -f Makefile ]; then
make clean
/opt/php/bin/phpize -clean
fi

#Perform the compilation
/opt/php/bin/phpize && ./configure -with-php-config=/opt/php/bin/php-config -e
nable-phalcon && make && make install && echo -e “\nThanks for compiling Phalcon
!\nBuild succeed: Please restart your web server to complete the installation”

安装 yaf
wget https://github.com/laruence/php-yaf/archive/yaf-2.2.9.tar.gz
mv yaf-2.2.9 yaf-2.2.9.tar.gz
cd php-yaf-yaf-2.2.9/
sudo /opt/php/bin/phpize
sudo ./configure -with-php-config=/opt/php/bin/php-config
sudo make
sudo make install

安装eAccelerator: make出错, 放弃

安装APC
sudo /opt/php/bin/pecl install apc
然后修改php.ini

安装 GraphicsMagick
wget http://downloads.sourceforge.net/project/graphicsmagick/graphicsmagick/1.3.19/GraphicsMagick-1.3.19.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fgraphicsmagick%2Ffiles%2Fgraphicsmagick%2F1.3.19%2F&ts=1394559460&use_mirror=ncu
tar -zxvf GraphicsMagick-1.3.19.tar.gz
cd GraphicsMagick-1.3.19
./configure -prefix=/opt/GraphicsMagick -enable-shared=yes
没有最后那个参数的话, php的扩展编译会出错
sudo make
sudo make install
/opt/GraphicsMagick/bin/gm
/opt/GraphicsMagick/bin/gm convert -list formats

安装graphicsmagick的php扩展:
因为是RC, 所以必须用channel 指定安装
sudo /opt/php/bin/pecl install gmagick channel://pecl.php.net/gmagick-1.1.7RC1

用yum 安装centos 自带版本:
sudo yum install php-pecl-memcache
sudo yum install php-pecl-apc

安装ImageMagick 比较麻烦, 按如下顺序:
sudo yum install ImageMagick
sudo yum install ImageMagick-devel  — 必须安装
sudo yum install php-devel — 必须安装, 要用到 phpnize
sudo yum install php-pear — 检查下 php-pear 是否安装, 然后才可以用pecl命令
sudo pecl install imagick
上一步完成后, 检查imagick.so 是否已经生成, 默认位置在 /usr/lib64/php/modules , 从前面安装的显示信息里能看到, 然后加到php.ini 里面
我用的这个版本的php, extension不是放在php.ini里面的, 而是放在 /etc/php.d/*.ini 里面, 所以在里面新建了一个 imagick.ini , 把extension 写进去
sudo vi imagick.ini
; Enable imagick extension module
extension=imagick.so
然后重启httpd 就可以了

    原文作者:memcached
    原文地址: https://www.cnblogs.com/milton/p/4215110.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞