标签:Centos 6.9编译安装LAMP Zend OPcach
Centos 6.9编译安装LAMP apache 2.4 + Mysql 5.7 + php5.6.34 + Fast-cgi + Zend OPcach实验环境:VMware Workstation Pro 14(试用版)
系统平台:
CentOS release 6.9 (Final) 内核 2.6.32-696.el6.x86_64
Apache/2.4.29 (Unix)
PHP 5.6.34 (cli)
Zend OPcach
参考Centos 6.9 apahce 2.4.29编译安装
参考CentOS 6.9 自定义单实例 二进制方式 安装mysql5.7.21
# wget http://hk1.php.net/distributions/php-5.6.34.tar.bz2
有个别包需要EPEL源,可提前配置好Aliyun的Yum源
注意:以下依赖包仅仅限于下面演示的编译参数,实际按需。
#yum install bzip2-devel libxml2-devel libmcrypt-devel libmcrypt curl-devel gd-devel
如果需要后期动态添加模块,还需要安装autoconf
注意:php-7.0以上版本使用--enable-mysqlnd --withmysqli=mysqlnd ,原--with-mysql不再支持
编译参数
对于mysql的api方法,先了解一下:
PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包。
mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖)。
从PHP 5.4开始,对于未明确指定--with-mysql的情形,mysql本地驱动将会被安装。
可以参考如下配置:
比如:
--with-mysql > 相当于该参数值为mysqlnd
--with-mysqli > 相当于该参数值为mysqlnd
--with-pdo-mysql > 相当于该参数值为mysqlnd
因为,--with-mysqli=/usr/local/mysql/bin/mysql_config 这种才是明确指定的表示方法
# tar xvf php-5.6.34.tar.bz2
# cd php-5.6.34
./configure --prefix=/usr/local/php-5.6.34 --enable-opcache --with-openssl --enable-mysqlnd --with-mysql=/usr/local/mysql --with-mysqli --with-pdo-mysql --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-iconv --with-gd --with-curl --disable-debug --enable-calendar
编译与安装
# make -j 8
出现Build complete. 那么,恭喜编译成功
# make install
以下这些提示,按需。
Wrote PEAR system config file at: /usr/local/php-5.6.34/etc/pear.conf
You may want to add: /usr/local/php-5.6.34/lib/php to your php.ini include_path
/app/httpd/php-5.6.34/build/shtool install -c ext/phar/phar.phar /usr/local/php-5.6.34/bin
ln -s -f phar.phar /usr/local/php-5.6.34/bin/phar
Installing PDO headers: /usr/local/php-5.6.34/include/php/ext/pdo/
创建一个软链接,方便管理版本
#cd /usr/local/
#ln -s php-5.6.34/ php
注意,这些文件是在源码目录里
# cp php.ini-production /etc/php.ini
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
创建一个存放其他扩展配置的目录
# mkdir /etc/php.d
# vim /etc/rc.d/init.d/php-fpm > 这一步不修改也行,只不过这里是为了后续切换不同版本时方便
prefix=/usr/local/php > 把这行修改为指定的编译路径
# sed -ri.bak s#php-5.6.34#php#g /usr/local/php/etc/php-fpm.conf.default
# mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# chkconfig --add php-fpm
# chkconfig php-fpm on
# vim /etc/httpd2.4/httpd.conf
确保以下2条取消注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
# 如果使用的是虚拟主机形式,把下面4行添加到主机标签中
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/www/virtualhost/$1
其中/app/www/virtualhost指的是站点文件目录
例子:
<VirtualHost *:80>
DocumentRoot "/app/www/virtualhost"
ServerName www.hunk.tech
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/www/virtualhost/$1
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
<Directory "/app/www/virtualhost">
AllowOverride None
Options None
Require all granted
</Directory>
</VirtualHost>
# service httpd restart
# service php-fpm start
#ss -nlt
fcgi正在监听端口
State Recv-Q Send-Q Local Address:Port
LISTEN 0 128 127.0.0.1:9000
编辑一个php的测试文件
#vim /app/www/virtualhost/index.php
<?php
phpinfo();
?>
在mysql中创建一个用于连接的账户
mysql> create user test@‘192.168.5.102‘ identified by ‘password‘;
mysql5.7数据库下已经没有password这个字段了,password字段改成了authentication_string
并且密码策略控制着密码相关
以下为修改默认的密码策略,0=LOW,至少8个字符
mysql> set global validate_password_policy=0
测试代码如下:
# vim /app/www/virtualhost/check.php
<?php
$mysqli = new mysqli("localhost", "test", "12345678");
/* check connection */
if ($mysqli->connect_errno) {
echo "连接失败";
exit();
}
echo "连接成功";
/* close connection */
$mysqli->close();
?>
# curl www.hunk.tech/check.php
连接成功
以下代码为判断mysql和mysqli扩展是否安装
<?php
function mysqlinstalled (){
if (function_exists ("mysql_connect")){
return true;
} else {
return false;
}
}
function mysqliinstalled (){
if (function_exists ("mysqli_connect")){
return true;
} else {
return false;
}
}
if (mysqlinstalled()){
echo "<p>The mysql extension is installed.</p>";
} else {
echo "<p>The mysql extension is not installed..</p>";
}
if (mysqliinstalled()){
echo "<p>The mysqli extension is installed.</p>";
} else {
echo "<p>The mysqli extension is not installed..</p>";
}
?>
#ab -c 1000 -n 1000 192.168.5.102/check.php
Server Software: Apache/2.4.29
Server Hostname: 192.168.5.102
Server Port: 80
Document Path: /check.php
Document Length: 12 bytes
Concurrency Level: 1000
Time taken for tests: 1.381 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 183000 bytes
HTML transferred: 12000 bytes
Requests per second: 724.01 [#/sec] (mean)
Time per request: 1381.202 [ms] (mean)
Time per request: 1.381 [ms] (mean, across all concurrent requests)
Transfer rate: 129.39 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 239 424.4 2 1032
Processing: 9 154 107.5 141 796
Waiting: 4 154 107.7 140 795
Total: 52 393 445.2 149 1324
Percentage of the requests served within a certain time (ms)
50% 149
66% 165
75% 421
80% 1055
90% 1213
95% 1290
98% 1312
99% 1319
100% 1324 (longest request)
找到:[opcache],设置为
[opcache]
zend_extension=opcache.so > 加载模块,可以使用/usr/local/php/bin/php-config --extension-dir 查看扩展目录中文件是否存在
opcache.enable=1 > 开启 Zend Opcache
opcache.enable_cli=1 > 开启在命令行使用 Opcache
opcache.memory_consumption=128 > 共享内存大小,单位 MB ,根据实际情况设置
opcache.interned_strings_buffer=8 > 暂存池中字符串的占内存总量.(单位:MB)
opcache.max_accelerated_files=2000 > 最大缓存文件数量,命中率低时,可以提高该限制
opcache.revalidate_freq=60 > 查找文件更新间隔
opcache.fast_shutdown=1 > 启用内存快速回收
更详细的参数说明,请参考https://www.abcdocker.com/abcdocker/2151
# service php-fpm restart
# /usr/local/php/bin/php -m|grep -i opcach
Zend OPcache
Zend OPcache
通过phpinfo也可以看到
#ab -c 1000 -n 1000 192.168.5.102/check.php
Server Software: Apache/2.4.29
Server Hostname: 192.168.5.102
Server Port: 80
Document Path: /check.php
Document Length: 12 bytes
Concurrency Level: 1000
Time taken for tests: 1.262 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 183183 bytes
HTML transferred: 12012 bytes
Requests per second: 792.66 [#/sec] (mean)
Time per request: 1261.572 [ms] (mean)
Time per request: 1.262 [ms] (mean, across all concurrent requests)
Transfer rate: 141.80 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 163 352.8 9 1040
Processing: 24 227 195.6 192 797
Waiting: 3 226 195.7 191 797
Total: 70 390 360.5 205 1200
Percentage of the requests served within a certain time (ms)
50% 205
66% 219
75% 487
80% 818
90% 1107
95% 1151
98% 1189
99% 1196
100% 1200 (longest request)
测试受环境影响仅仅作为参考
Zend Opcache 与 eaccelerator 相冲突
Zend OPcache以PHP5.5+以上默认集成了,默认并未开启。 通过 opcode 缓存和优化提供更快的PHP执行过程。它将预编译的脚本文件存储在共享内存中供以后使用,
从而避免了从磁盘读取代码并进行编译的时间消耗。同时,它还应用了一些代码优化模式,使得代码执行更快。
别人测试的结果:
opcache 加速性能 比 xcache 优秀10%-27%(include),opcache对cpu密集运算有一定的加速效果,
opcache xcache 在实际应用中的加速效果(tp框架) 约为不加速的2-3倍
标签:Centos 6.9编译安装LAMP Zend OPcach
原文地址:http://blog.51cto.com/191226139/2083113