PHP7的性能表现的确让人眼前一亮,近来新开了一台Centos服务器,于是折腾起来。

首先,我们安装一下 RPM 包(根据你的系统版本安装):

#安装RPM源 CentOS/RHEL 7.x

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

#安装RPM源 CentOS/RHEL 6.x

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm

#安装php7和常用扩展

yum install -y php70w php70w-devel php70w-fpm php70w-opcache php70w-mysqlnd php70w-pdo php70w-gd php70w-mbstring

#安装php-memcache扩展

wget https://github.com/websupport-sk/pecl-memcache/archive/php7.zip -O pecl-php-memcached.zip
unzip pecl-php-memcached.zip
cd pecl-memcache*
phpize
./configure
make
make install
echo "[memcache]
extension=memcache.so" >/etc/php.d/memcache.ini

#检测 MEMCACHE 扩展是否正常

php -m | grep memcache

#安装PHP-RAR扩展

wget https://github.com/Jan-E/php_pecl_rar7/archive/master.zip -O pecl-php-rar.zip
unzip pecl-php-rar.zip
phpize
./configure
make 
make install
# 写入rar配置
echo "[rar]
extension=rar.so" >/etc/php.d/rar.ini

#检测 RAR 扩展是否正常

php -m | grep rar

安装完毕,再设置一下基础参数(请视需求自行修改):

sed -i "s#;pid = run/php-fpm.pid#pid = run/php-fpm.pid#g" /etc/php-fpm.d/www.conf
sed -i "s#;error_log#error_log#g" /etc/php-fpm.d/www.conf
sed -i "s#;log_level = notice#log_level = warning#g" /etc/php-fpm.d/www.conf
sed -i "s#; process.max = 128#process.max = 128#g" /etc/php-fpm.d/www.conf
sed -i "s#;slowlog#slowlog#g" /etc/php-fpm.d/www.conf
sed -i "s#; events.mechanism = epoll#events.mechanism = epoll#g" /etc/php-fpm.d/www.conf
sed -i "s#pm.max_children = 5#pm.max_children = 10#g" /etc/php-fpm.d/www.conf
sed -i "s#pm.start_servers = 2#pm.start_servers = 4#g" /etc/php-fpm.d/www.conf
sed -i "s#pm.min_spare_servers = 1#pm.min_spare_servers = 2#g" /etc/php-fpm.d/www.conf
sed -i "s#pm.max_spare_servers = 3#pm.max_spare_servers = 5#g" /etc/php-fpm.d/www.conf
sed -i "s#;pm.max_requests = 500#pm.max_requests = 500#g" /etc/php-fpm.d/www.conf
sed -i "s#;listen.allowed_clients#listen.allowed_clients#g" /etc/php-fpm.d/www.conf
sed -i "s#;listen.owner = nobody#listen.owner = www#g" /etc/php-fpm.d/www.conf
sed -i "s#;listen.group = nobody#listen.group = www#g" /etc/php-fpm.d/www.conf
sed -i "s#user = apache#user = www#g" /etc/php-fpm.d/www.conf
sed -i "s#group = apache#group = www#g" /etc/php-fpm.d/www.conf
#sed -i "s#listen = 127.0.0.1:9000#;listen = 127.0.0.1:9000\nlisten = /tmp/php-cgi.sock#g" /etc/php-fpm.d/www.conf
sed -i "s#;listen.mode = 0666#listen.mode = 0666#g" /etc/php-fpm.d/www.conf
sed -i "s#;slowlog#slowlog#g" /etc/php-fpm.d/www.conf

sed -i "s#;access.format#access.format#g" /etc/php-fpm.d/www.conf
sed -i "s#;access.log = log/$pool.access.log#access.log = /usr/local/php/var/log/$pool.access.log#g" /etc/php-fpm.d/www.conf

sed -i 's@expose_php = On@expose_php = Off@g' /etc/php.ini
sed -i 's@short_open_tag = Off@short_open_tag = On@g' /etc/php.ini
sed -i 's@max_execution_time = 30@max_execution_time = 60@g' /etc/php.ini
sed -i 's@post_max_size = 8M@post_max_size = 50M@g' /etc/php.ini
sed -i 's@upload_max_filesize = 2M@upload_max_filesize = 50M@g' /etc/php.ini

最后,启动服务:

service php-fpm restart