歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習Linux

CentOS安裝PHP-5.6.4+擴展安裝+安全配置+性能配置,centosphp-5.6.4

CentOS安裝PHP-5.6.4+擴展安裝+安全配置+性能配置,centosphp-5.6.4

CentOS安裝PHP-5.6.4+擴展安裝+安全配置+性能配置,centosphp-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也先安裝好,具體見:《CentOS安裝MySQL-5.6.10+安全配置》、《CentOS安裝Apache-2.4.10+安全配置》。

 

#PHP的安裝#

開始下載PHP並進行編譯安裝: 

# wget http://cn2.php.net/distributions/php-5.6.4.tar.gz
# tar zxf php-5.6.4.tar.gz
# cd php-5.6.4
# export LD_LIBRARY_PATH=/usr/local/mysql/lib
# ./configure --prefix=/usr/local/php-5.6.4 --with-config-file-path=/usr/local/php-5.6.4/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.1
config.status: creating sapi/cli/php.1
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.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

# make
...
Build complete.
Don't forget to run 'make test'.

# 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-2.4.10/modules
/usr/local/apr/build-1/libtool --mode=install install libphp5.la /usr/local/apache-2.4.10/modules/
install .libs/libphp5.so /usr/local/apache-2.4.10/modules/libphp5.so
install .libs/libphp5.lai /usr/local/apache-2.4.10/modules/libphp5.la
libtool: install: warning: remember to run `libtool --finish /usr/local/src/php-5.6.4/libs'
chmod 755 /usr/local/apache-2.4.10/modules/libphp5.so
[activating module `php5' in /usr/local/apache-2.4.10/conf/httpd.conf]
Installing PHP CLI binary:        /usr/local/php-5.6.4/bin/
Installing PHP CLI man page:      /usr/local/php-5.6.4/php/man/man1/
Installing PHP FPM binary:        /usr/local/php-5.6.4/sbin/
Installing PHP FPM config:        /usr/local/php-5.6.4/etc/
Installing PHP FPM man page:      /usr/local/php-5.6.4/php/man/man8/
Installing PHP FPM status page:   /usr/local/php-5.6.4/php/fpm/
Installing PHP CGI binary:        /usr/local/php-5.6.4/bin/
Installing PHP CGI man page:      /usr/local/php-5.6.4/php/man/man1/
Installing build environment:     /usr/local/php-5.6.4/lib/php/build/
Installing header files:          /usr/local/php-5.6.4/include/php/
Installing helper programs:       /usr/local/php-5.6.4/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php-5.6.4/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php-5.6.4/lib/php/
[PEAR] Archive_Tar    - installed: 1.3.12
[PEAR] Console_Getopt - installed: 1.3.1
[PEAR] Structures_Graph- installed: 1.0.4
[PEAR] XML_Util       - installed: 1.2.3
[PEAR] PEAR           - installed: 1.9.5
Wrote PEAR system config file at: /usr/local/php-5.6.4/etc/pear.conf
You may want to add: /usr/local/php-5.6.4/lib/php to your php.ini include_path
/usr/local/src/php-5.6.4/build/shtool install -c ext/phar/phar.phar /usr/local/php-5.6.4/bin
ln -s -f /usr/local/php-5.6.4/bin/phar.phar /usr/local/php-5.6.4/bin/phar
Installing PDO headers:          /usr/local/php-5.6.4/include/php/ext/pdo/

# ln -s /usr/local/php-5.6.4/ /usr/local/php

給PHP的upload和session添加目錄支持:

# mkdir -p /data/php/upload
# mkdir -p /data/php/session

至此,PHP已經安裝完畢。

 

#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 5.6.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-5.6.4/php.ini-production /usr/local/php-5.6.4/etc/php.ini
# cp /usr/local/php-5.6.4/etc/php-fpm.conf.default /usr/local/php-5.6.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-5.6.4/var/run/php-fpm.pid
error_log = /usr/local/php-5.6.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程序,並且設置開機自啟動:

# cp /usr/local/src/php-5.6.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-5.6.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   127.0.0.1:9000;
            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-1.6.2

啟動Nginx,並打開浏覽器訪問地址http://youripaddress/phpinfo.php應該可以看到:

至此,Nginx與PHP已經可以正常協同工作。

 

#PHP的擴展安裝#

很多時候我們還需要給PHP安裝各種擴展支持,比如memcache、redis、mongodb等,下面用幾個例子來說明下。

1、安裝memcache擴展: 

# wget http://pecl.php.net/get/memcache-2.2.7.tgz
# tar zxf memcache-2.2.7.tgz
# cd memcache-2.2.7
# /usr/local/php-5.6.4/bin/phpize
# ./configure --with-php-config=/usr/local/php-5.6.4/bin/php-config
# make && make install
Installing shared extensions:     /usr/local/php-5.6.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-1.0.18.tar.gz
# cd libmemcached-1.0.18
# ./configure --prefix=/usr/local/libmemcached-1.0.18 --enable-sasl
# make && make install

 

3)最後再安裝memcached擴展:

# wget http://pecl.php.net/get/memcached-2.2.0.tgz
# tar zxf memecached-2.2.0.tgz
# cd memecached-2.2.0
# /usr/local/php-5.6.4/bin/phpize
# ./configure --with-php-config=/usr/local/php-5.6.4/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached-1.0.18 --enable-memcached-sasl
# make && make install
Installing shared extensions:     /usr/local/php-5.6.4/lib/php/extensions/no-debug-zts-20131226/

 

注:前面安裝的libmemcached必須支持SASL,否則可能出現如下錯誤:

configure: error: no, libmemcached sasl support is not enabled. Run configure with --disable-memcached-sasl to disable this check

 

3、安裝redis擴展:

# wget http://pecl.php.net/get/redis-2.2.7.tgz
# tar zxf redis-2.2.7.tgz
# cd redis-2.2.7
# /usr/local/php-5.6.4/bin/phpize
# ./configure --with-php-config=/usr/local/php-5.6.4/bin/php-config
# make && make install
Installing shared extensions:     /usr/local/php-5.6.4/lib/php/extensions/no-debug-zts-20131226/

擴展安裝完之後,還要打開php.ini文件編輯如下:

# vim /usr/local/php-5.6.4/etc/php.ini

extension=memcache.so
extension=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注入漏洞。

magic_quotes_gpc = On

 

5、關閉錯誤信息提示,因為這些錯誤信息可能洩漏服務器的路徑信息和數據庫信息等。

display_errors = Off

6、開啟錯誤日志記錄,可以考慮跟Web服務器的日志放在一起,比如:

log_errors = On
error_log = /data/logs/php/php_error.log

 

7、禁止訪問遠程文件,因為訪問URL遠程資源使得PHP應用程序的漏洞變得更加容易被利用,關閉之,如果要訪問遠程服務器建議采用其他方式比如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-5.6.4/etc/php-fpm.conf)                                                        
www      28954  0.0  1.0 196060  5504 ?        S    12:21   0:00 php-fpm: pool www                                                                                                            
www      28955  0.0  1.0 196060  5504 ?        S    12:21   0:00 php-fpm: pool www                                                                                                               
root     28974  0.0  0.1 103252   836 pts/0    S+   12:22   0: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 = "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, true);
$mem->addServer("something.m.cnszalist3pub001.ocs.aliyuncs.com", 11211);
$mem->setSaslAuthData("yourusername", "yourpassword");

$key = 'key';
$mem->set($key, 'ocs cache value');
$cache = $mem->get($key);
if (empty($cache)) {
    echo 'Oh, No!';
} else {
    echo "Thanks God, the cache value is '{$cache}'";
}
?>

 

http://xxxxxx/Linuxjc/1178062.html TechArticle

Copyright © Linux教程網 All Rights Reserved