欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

Centos5.8 安装 PHP5.5 和 memcached

程序员文章站 2024-02-17 11:41:46
...

安装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 ./

安装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

安装PHP5.5

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

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 做适当修改(已经改在 php.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处理就好了.