本站教程收集整理的这篇文章主要介绍了CentOS安装PHP-5.6.4+扩展安装+安全配置+性能配置,本站教程本站觉得挺不错的,现在分享给大家,也给大家做个参考。
??
注:以下所有操作均在CentOS 6.5 x86_64位系统下完成。
#准备工作#
前段时间php官方发布了一个重要的安全升级公告,修复了两个unserialize函数的严重漏洞,目前受影响的版本有:
- <5.4.36
- <5.5.20
- <5.6.4
这里我们直接下载5.6.4的版本进行安装配置,并且在这之前需要先把MysqL和Apache已经安装好,最好Nginx也先安装好.
#php的安装#
开始下载php并进行编译安装:
# wget http://cn2.php.net/diStributions/php-5.6.4.tar.gz # tar zxf php-5.6.4.tar.gz # cd php-4 # export LD_LIBRARY_PATH=/usr/local/MysqL/lib # ./configure --prefix=/usr/local/php-4 --with-config-file-path=/usr/local/php-香港vps4/etc --with-MysqL=/usr/local/MysqL --with-pdo-MysqL=/usr/local/MysqL --with-MysqLi=/usr/local/MysqL/bin/MysqL_config --with-apxs2=/usr/local/apache/bin/apxs --enable-fpm --enable-shared --enable-zip --with-bz2 --enable-ftp --with-jpeg-dir --with-png-dir --with-freetype-dir --with-libxml-dir --with-xmlrpc --with-zlib-dir --with-gd --with-gmp --enable-gd-native-ttf --with-curl --with-regex=php --with-pic --with-xsl --enable-wddx --with-iconv --with-gettext --with-pear --enable-ctype --enable-calendar --enable-mbString --enable-bcmath --enable-sockets --enable-exif --disable-rpath --with-mcrypt --with-mhash --with-openssl --enable-sysvsem --enable-sigchild --enable-sysvshm --enable-soap --disable-fileinfo --enable-opcache=no ... Thank you for using php. config.status: creaTing php5.spec config.status: creaTing main/build-defs.h config.status: creaTing scripts/phpize config.status: creaTing scripts/man1/phpize.1 config.status: creaTing scripts/php-config config.status: creaTing scripts/man1/php-config. config.status: creaTing sapi/cli/php. config.status: creaTing sapi/fpm/php-fpm.conf config.status: creaTing sapi/fpm/init.d.php-fpm config.status: creaTing sapi/fpm/php-fpm.service config.status: creaTing sapi/fpm/php-fpm.8 config.status: creaTing sapi/fpm/status.html config.status: creaTing sapi/cgi/php-cgi. config.status: creaTing ext/phar/phar. config.status: creaTing ext/phar/phar.phar. config.status: creaTing main/php_config.h config.status: execuTing default commands # @H_460_36@make ... Build complete. Don't forget to run '@H_460_36@make test. # @H_460_36@make install ... Installing php SAPI module: apache2handler /usr/local/apache-2.4.10/build/instdso.sh SH_LIBTOOL=/usr/local/apr/build-1/libtool' libphp5.la /usr/local/apache-10/@H_77_40@modules /usr/local/apr/build-1/libtool --mode=install install libphp5.la /usr/local/apache-10/modules/ install .libs/libphp5.so /usr/local/apache-10/modules/libphp5.so install .libs/libphp5.lai /usr/local/apache-libphp5.la libtool: install: warning: remember to run `libtool --finish /usr/local/src/php-4/libs' chmod 755 /usr/local/apache-libphp5.so [activaTing module `php5 in /usr/local/apache-2.4.10/conf/httpd.conf] Installing php CLI binary: /usr/local/php-4/bin/ Installing php CLI @H_460_36@man page: /usr/local/php-4/php/@H_460_36@man/man1/ Installing php FPM binary: /usr/local/php-4/sbin/ Installing php FPM config: /usr/local/php-4/etc/ Installing php FPM @H_460_36@man/man8/ Installing php FPM status page: /usr/local/php-4/php/fpm/ Installing php CGI binary: /usr/local/php- Installing php CGI Installing build environment: /usr/local/php-4/lib/php/build/ Installing header files: /usr/local/php-4/include/php/ Installing Helper programs: /usr/local/php- program: phpize program: php-config Installing @H_460_36@man pages: /usr/local/php- page: phpize. page: php-config. Installing PEAR environment: /usr/local/php-4/lib/php/ [PEAR] Archive_Tar - installed: 1.3.12 [PEAR] Console_Getopt - installed: [PEAR] Structures_Graph- installed: 1.0. [PEAR] XML_Util - installed: 1.2.3 [PEAR] PEAR - installed: 1.9.5 Wrote PEAR system config file at: /usr/local/php-pear.conf You may want to add: /usr/local/php-4/lib/php to your php.ini include_path /usr/local/src/php-4/build/shtool install -c ext/phar/phar.phar /usr/local/php-4/bin ln -s -f /usr/local/php-4/bin/phar.phar /usr/local/php-phar Installing PDO headers: /usr/local/php-4/include/php/ext/pdo/ # ln -s /usr/local/php-4/ /usr/local/php
给php的upload和session添加目录支持:
# @H_460_36@mkdir -p /data/php/upload
# @H_460_36@mkdir -p /data/php/session
至此,php已经@R_737_9443@
#php-FPM的启动/关闭/重启#
自php-5.3.3开始,php源码中包含了php-fpm,不需要再单独通过布丁的方式来安装php-fpm,在编译的时候加入参数–enable-fpm即可。
FPM(FastCGI Process Manager)用于替换php FastCGI的大部分附加功能,对于高负载网站非常有用。
首先将bin/php-fpm加入到系统PATH中:
# vim /etc/profile export php_HOME=/usr/local/php export PATH=$PATH:$php_HOME/bin:$php_HOME/sbin $ source /etc/profile $ php -version php 4 (cli) (built: Jan 3 2015 11:16:17) Copyright (C) 1997-2014 The php Group Zend ENGIne v2.6.0,Copyright (C) 1998-2014 Zend Technologies
然后复制默认的配置文件:
# cp /usr/local/src/php-4/php.ini-production /usr/local/php-php.ini # cp /usr/local/php-4/etc/php-fpm.conf.default /usr/local/php-4/etc/php-fpm.conf
接下来修改默认配置文件:
# vim /usr/local/php/etc/php.ini date.timezone = Asia/Shanghai # vim /usr/local/php/etc/php-fpm.conf [global] pid = /usr/local/php-4/var/run/php-fpm.pid error_log = /usr/local/php-4/var/log/php-fpm.log log_level = error [www] user = www group = www listen = 127.0.0.1:9000 listen.owner = www listen.group = www listen.mode = 0666
添加启动脚本,之后可以使用service来启动php-fpm程序,并且设置开机自启动:
# 4/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # chmod +x /etc/init.d/php-fpm # service php-fpm Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status} # chkconfig php-fpm on
启动php-fpm:
# service php-fpm start StarTing php-fpm done
如果启动的时候出现错误(实际操作中发现存在该情况):
StarTing php-fpm /usr/local/php-4/sbin/php-fpm: error while loading shared libraries: libMysqLclient.so.18: cAnnot open shared object file: No such file or directory Failed
则将MysqL的so库文件copy到系统下:
# cp /usr/local/MysqL/lib/libMysqLclient.so.18 /usr/lib64/
再来重新启动php,这次启动成功。
#php+Nginx#
现在来编辑Nginx.conf以支持php解析,查看当前php环境是否可以正常运行:
# vim /usr/local/Nginx/conf/Nginx.conf http { server { listen 80; server_name localhost; root html; index index.html index.php; LOCATIOn ~ \.php$ { try_files $uri = 404; include fastcgi.conf; fastcgi_pass ; fastcgi_index index.php; fastcgi_param SCRIPT_FILename $document_root$fastcgi_script_name; } } }
然后编写一个简单的php文件,用来显示当前php环境信息:
# vim /usr/local/Nginx-1.6.2/html/phpinfo.php <?php phpinfo(); ?>chown -R www:www /usr/local/Nginx-2
启动Nginx,并打开浏览器访问地址http://youripaddress/phpinfo.php应该可以看到:
至此,Nginx与php已经可以正常协同工作。
#php的扩展安装#
很多时候我们还需要给php安装各种扩展支持,比如memcache、redis、mongodb等,下面用几个例子来说明下。
1、安装memcache扩展:
# pecl.php.net/get/memcache-2.2.7.tgz # tar zxf memcache-2.2.7.tgz # cd memcache- # /usr/local/php-phpize # ./configure --with-php-config=/usr/local/php-4/bin/php-config # @H_460_36@make && Installing shared extensions: /usr/local/php-4/lib/php/extensions/no-debug-zts-20131226/
2、安装memcached扩展(支持SASL):
1)首先可以使用yum安装SASL环境:
# yum install cyrus-sasl-plain cyrus-sasl cyrus-sasl-devel cyrus-sasl-lib
2)然后下载并安装libmemecached:
# wget https:launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz # tar zxf libmemcached-18..gz # cd libmemcached-18 # ./configure --prefix=/usr/local/libmemcached-18 --enable-sasl # install
3)最后再安装memcached扩展:
# pecl.php.net/get/memcached-2.2.0.tgz # tar zxf memecached-0.tgz # cd memecached-4/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached-18 --enable-memcached-20131226/
注:前面安装的libmemcached必须支持SASL,否则可能出现如下错误:
configure: error: no,libmemcached sasl support is not enabled. Run configure with --disable-memcached-sasl to disable this check
3、安装redis扩展:
# pecl.php.net/get/redis-2.2.7.tgz # tar zxf redis-.tgz # cd redis-20131226/
扩展安装完之后,还要打开php.ini文件编辑如下:
# vim /usr/local/php-php.ini
extension=@H_77_40@memcache.so
extension=@H_77_40@memcached.so
extension=redis.so
#php的安全配置#
1、控制脚本访问权限。由于php默认配置允许php脚本程序访问服务器上的任意文件,为避免php脚本访问不该访问的文件,需要设置php只能访问网站目录或其他必须可访问的目录。比如:
# vim /usr/local/php/etc/php.ini
open_basedir=/data/php/uploads:/data/www/proj1:/data/www/proj2
2、禁止使用php危险函数,这些函数都是php木马常用的,比如:
# vim /usr/local/php/etc/php.ini
disable_functions = dl,assert,exec,popen,system,passthru,sHell_exec,proc_close,proc_open,pcntl_exec
3、关闭注册全局变量(php-5.3.*和php-5.4.*中已废除)
register_globals = Off
4、开启magic_quotes_gpc(php-5.3.*和php-5.4.*中已废除),由于magic_quotes_gpc会把引用的数据中包含单引号’和双引号”以及反斜线 \自动加上反斜线,自动转译符号,确保数据操作的正确运行,magic_quotes_gpc的设定值将会影响通过Get/Post/Cookies获得的数据,可以有效的防止sql注入漏洞。
@H_193_35@magic_quotes_gpc = On
5、关闭错误信息提示,因为这些错误信息可能泄漏服务器的路径信息和数据库信息等。
display_errors = Off
6、开启错误日志记录,可以考虑跟Web服务器的日志放在一起,比如:
log_errors = On
error_log = /data/logs/php/php_error.log
7、禁止访问远程文件,因为访问URL远程资源使得程序的漏洞变得更加容易被利用,关闭之,如果要访问远程服务器建议采用其他方式比如libcurl库。
allow_url_fopen = Off
allow_url_include = Off
8、开启php安全模式(php-5.3.*和php-5.4.*中已废除)
safe_mode = On
9、补上Nginx文件解析漏洞。
cgi.fix_pathinfo = 0
10、确保php(FastCGI)以非root权限启动。如果是php-cgi进程,需要su道普通用户再启动;php-fpm进程默认已是非root用户进行,配置中配置即可,不能修改为root运行。比如这里:
root 28953 0.0 1.1 196060 5736 ? Ss 12:21 0:00 php-fpm: master process (/usr/local/php-4/etc/php-fpm.conf) www 28954 1.0 5504 ? S 00 php-fpm: pool www www 28955 fpm: pool www root 28974 0.1 103252 836 pts/0 S+ 22 00 grep php-fpm
注:这里只有master是root用户权限,其他两个pool中的进程都是www用户,这是正确的。
#php的性能配置#
性能配置主要是为了让php能够运行得更好,这里很多时候需要根据业务的需求和当前系统的配置来设置,以下的配置只作为参考作用。
1、配置上传文件大小限制(一般不超过2MB)
# vim /usr/local/php/etc/php.ini file_uploads = On upload_tmp_dir = /data/php/upload upload_max_filesize = 5M post_max_size = 8M max_execution_time = 30 max_input_time = 60 memory_limit = 32M
2、使用阿里云的OCS(memcache)来代替文件作为session的存储(这里需要前面安装memcached的扩展库,并且支持SASL),比如:
# vim /usr/local/php/etc/php.ini ;session.save_handler = files ;session.save_path = "/tmp" ;session.save_path = /data/php/session ;session.gc_maxlifetime = 1440 session.save_handler = memcached session.save_path = something.m.cnszalist3pub001.ocs.aliyuncs.com:11211 session.gc_maxlifetime = 3600 [memcached] memcached.use_sasl = On memcached.sess_binary = On memcached.sess_sasl_username = yourusername memcached.sess_sasl_password = yourpassword memcached.sess_locking = Off memcached.sess_prefix = @H_861_94@memc.sess.key."
注:使用其他的诸如memcache或redis缓存也是类似上面的配置。
可以编写一个简单的OCS的php代码来进行测试,比如:
# vim ocs.php <?php $mem = new Memcached(ocs); $mem->setOption(Memcached::OPT_COMPRESSION,false); $mem->setOption(Memcached::OPT_BINARY_PROTOCOL,255)">true); $mem->addServer(something.m.cnszalist3pub001.ocs.aliyuncs.com",11211); $mem->setSaslAuthData(); $key = key; $mem->set($key,0)">ocs cache value); $cache = $mem->get($key); if (empty($cachE)) { echo Oh,No!; } else { Thanks God,the cache value is '{$cachE}'; } ?>
本站总结
以上是本站教程为你收集整理的CentOS安装PHP-5.6.4+扩展安装+安全配置+性能配置全部内容,希望文章能够帮你解决CentOS安装PHP-5.6.4+扩展安装+安全配置+性能配置所遇到的程序开发问题。
如果觉得本站教程网站内容还不错,欢迎将本站教程推荐给好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。