[关闭]
@yanglt7 2018-10-21T15:56:12.000000Z 字数 7056 阅读 979

【Web 集群实战】14_PHP 服务缓存加速(Opcache+Memcached+pdo_mysql+imagick)

Web集群实战


一、LNMP 基础 Web 环境准备

软件 版本
Linux CentOS 7.4.1708 64bit
Nginx 1.14.0
PHP 7.1.22
MySQL 5.5.61
  1. [root@ylt001 ~]# cat /etc/redhat-release
  2. CentOS Linux release 7.4.1708 (Core)
  3. [root@ylt001 ~]# uname -r
  4. 3.10.0-693.el7.x86_64
  5. [root@ylt001 ~]# uname -m
  6. x86_64
  7. [root@ylt001 ~]# /application/nginx/sbin/nginx -v
  8. nginx version: nginx/1.14.0
  9. [root@ylt001 ~]# /application/php/bin/php -v
  10. PHP 7.1.22 (cli) (built: Sep 25 2018 06:46:29) ( NTS )
  11. Copyright (c) 1997-2018 The PHP Group
  12. Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
  13. with Zend OPcache v7.1.22, Copyright (c) 1999-2018, by Zend Technologies
  14. [root@ylt001 ~]# mysqladmin -uroot -p version
  15. Enter password:
  16. mysqladmin Ver 8.42 Distrib 5.5.61, for linux-glibc2.12 on x86_64
  17. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  18. Oracle is a registered trademark of Oracle Corporation and/or its
  19. affiliates. Other names may be trademarks of their respective
  20. owners.
  21. Server version 5.5.61
  22. Protocol version 10
  23. Connection Localhost via UNIX socket
  24. UNIX socket /tmp/mysql.sock
  25. Uptime: 5 min 28 sec
  26. Threads: 1 Questions: 4 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.012
  1. [root@ylt001 ~]# /application/nginx/sbin/nginx
  2. [root@ylt001 ~]# /application/nginx/sbin/nginx -t
  3. [root@ylt001 ~]# /application/nginx/sbin/nginx -s reload
  4. [root@ylt001 ~]# killall php-fpm
  5. [root@ylt001 ~]# /application/php/sbin/php-fpm
  6. [root@ylt001 ~]# ps -ef|grep php-fpm
  7. [root@ylt001 ~]# /etc/init.d/mysqld start
  8. [root@ylt001 ~]# /etc/init.d/mysqld restart
  9. [root@ylt001 ~]# netstat -lntup|grep mysql

二、Perl 编译问题

  1. [root@ylt001 ~]# echo 'export LC_ALL=C'>> /etc/profile
  2. # 设置环境变量,解决后面 Perl 程序插件的编译问题
  3. [root@ylt001 ~]# tail -1 /etc/profile
  4. export LC_ALL=C
  5. [root@ylt001 ~]# source /etc/profile
  6. [root@ylt001 ~]# echo $LC_ALL
  7. C
  8. [root@ylt001 ~]# yum install -y perl-devel
  9. # 安装 Perl 相关软件依赖软件包
  10. [root@ylt001 ~]# rpm -qa perl-devel
  11. perl-devel-5.16.3-292.el7.x86_64

三、安装 PHP Opcache 缓存加速模块

  1. opcache.enable=1
  2. zend_extension=opcache.so
  3. opcache.memory_consumption=128
  4. opcache.interned_strings_buffer=8
  5. opcache.max_accelerated_files=4000
  6. opcache.revalidate_freq=60
  7. opcache.fast_shutdown=1
  8. opcache.enable_cli=1
  9. opcache.enable=1

Opcache

  1. [root@ylt001 php-memcached]# killall php-fpm
  2. [root@ylt001 php-memcached]# /application/php/sbin/php-fpm
  3. [root@ylt001 php-memcached]# ps -ef|grep php-fpm

ZendOpcache

四、安装 PHP Memcached 插件

1. 服务器端 memcached 安装

  1. [root@ylt001 ~]# yum -y install memcached
  2. [root@ylt001 ~]# cat /etc/sysconfig/memcached
  3. PORT="11211"
  4. USER="memcached"
  5. MAXCONN="1024"
  6. CACHESIZE="64"
  7. OPTIONS=""
  8. [root@ylt001 ~]# firewall-cmd --zone=public --add-port=11211/tcp --permanent
  9. [root@ylt001 ~]# systemctl enable memcached
  10. [root@ylt001 ~]# systemctl restart memcached
  1. [root@ylt001 ~]# /usr/bin/memcached -d -l 127.0.0.1 -p 11211 -m 150 -u root
  2. # -d:守护进程。当从终端窗口退出的时候 memcached 还会继续运行
  3. # -l:指定 ip 地址,这里我们指定本地 ip
  4. # -p:指定端口号,端口号为 11211
  5. # -m:分配内存,这里我分配了 150M 内存
  6. # -u:使用哪个用户运行 memcached
  7. [root@ylt001 ~]# ps -ef | grep memcached
  8. memcach+ 2043 1 0 13:40 ? 00:00:00 /usr/bin/memcached -u memcache d -p 11211 -m 64 -c 1024
  9. root 62328 62236 0 16:21 pts/0 00:00:00 grep --color=auto memcached
  10. # ps 命令是 Precess Status 的缩写,也就是列出当前系统中运行那些进程
  11. # ps -ef 就是显示所有进程,连同命令行
  12. # ps 通常与 grep 组合使用,查找特定进程

2. php-memcached 拓展安装

2.1 libmemcached 拓展安装

  1. [root@ylt001 tools]# wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
  2. [root@ylt001 tools]# tar -zxvf libmemcached-1.0.18.tar.gz
  3. [root@ylt001 tools]# mv libmemcached-1.0.18 /application/
  4. [root@ylt001 tools]# cd /application/libmemcached-1.0.18
  5. [root@ylt001 libmemcached-1.0.18]# ./configure --prefix=/application/libmemcached-1.0.18
  6. [root@ylt001 libmemcached-1.0.18]# make && make install
  7. [root@ylt001 libmemcached-1.0.18]# ln -s /application/libmemcached-1.0.18 /application/libmemcached

2.1 php-memcached 拓展安装

  1. [root@ylt001 tools]# git clone https://github.com/php-memcached-dev/php-memcached
  2. [root@ylt001 tools]# cd php-memcached/
  3. [root@ylt001 php-memcached]# git checkout php7
  4. Branch php7 set up to track remote branch php7 from origin.
  5. Switched to a new branch 'php7'
  6. [root@ylt001 php-memcached]# /application/php/bin/phpize
  7. Configuring for:
  8. PHP Api Version: 20160303
  9. Zend Module Api No: 20160303
  10. Zend Extension Api No: 320160303
  11. [root@ylt001 php-memcached]# ./configure --with-php-config=/application/php/bin/php-config --with-libmemcached-dir=/application/libmemcached-1.0.18 --disable-memcached-sasl
  12. [root@ylt001 php-memcached]# make && make install
  13. [root@ylt001 php-memcached]# mv ../php-memcached /application/
  1. extension=memcached.so
  1. [root@ylt001 php-memcached]# killall php-fpm
  2. [root@ylt001 php-memcached]# /application/php/sbin/php-fpm
  3. [root@ylt001 php-memcached]# ps -ef|grep php-fpm

memcached

五、安装 PDO_MYSQL

  1. [root@ylt001 php-memcached]# killall php-fpm
  2. [root@ylt001 php-memcached]# /application/php/sbin/php-fpm
  3. [root@ylt001 php-memcached]# ps -ef|grep php-fpm

pdo_mysql

六、安装 imagick

  1. [root@ylt001 tools]# wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick-6.9.10-12.tar.gz
  2. [root@ylt001 tools]# tar zxvf ImageMagick-6.9.10-12.tar.gz
  3. [root@ylt001 tools]# mv ImageMagick-6.9.10-12 /application/
  4. [root@ylt001 tools]# cd /application/ImageMagick-6.9.10-12
  5. [root@ylt001 tools]# ./configure --prefix=/application/ImageMagick-6.9.10-12
  6. [root@ylt001 tools]# make && make install
  7. [root@ylt001 tools]# ln -s /application/ImageMagick-6.9.10-12 /application/ImageMagick
  1. [root@ylt001 tools]# wget http://pecl.php.net/get/imagick-3.4.3.tgz
  2. [root@ylt001 tools]# tar zxvf imagick-3.4.3.tgz
  3. [root@ylt001 tools]# mv imagick-3.4.3 /application/
  4. [root@ylt001 tools]# cd /application/imagick-3.4.3
  5. [root@ylt001 imagick-3.4.3]# /application/php/bin/phpize
  6. [root@ylt001 imagick-3.4.3]# ./configure --with-php-config=/application/php/bin/php-config --with-imagick=/application/ImageMagick
  7. [root@ylt001 imagick-3.4.3]# make && make install
  8. [root@ylt001 imagick-3.4.3]# ln -s /application/imagick-3.4.3 /application/imagick
  1. extension=imagick.so
  1. [root@ylt001 php-memcached]# killall php-fpm
  2. [root@ylt001 php-memcached]# /application/php/sbin/php-fpm
  3. [root@ylt001 php-memcached]# ps -ef|grep php-fpm

七、检查效果

  1. [root@ylt001 tools]# cd /application/php-7.1.22/lib/php/extensions/no-debug-non-zts-20160303/
  2. [root@ylt001 no-debug-non-zts-20160303]# ll
  3. total 6668
  4. -rwxr-xr-x 1 root root 1185176 Sep 28 14:55 imagick.so
  5. -rwxr-xr-x 1 root root 383312 Sep 28 14:16 memcached.so
  6. -rwxr-xr-x 1 root root 205632 Sep 26 11:12 mysql.so
  7. -rwxr-xr-x 1 root root 3406756 Sep 26 04:05 opcache.a
  8. -rwxr-xr-x 1 root root 1636664 Sep 26 04:05 opcache.so
  9. [root@ylt001 no-debug-non-zts-20160303]# cd /application/php/lib/
  10. [root@ylt001 lib]# tail -5 php.ini
  11. ; Local Variables:
  12. ; tab-width: 4
  13. extension=memcached.so
  14. extension=imagick.so
  15. ; End:
  16. [root@ylt001 lib]# grep opcache php.ini
  17. [opcache]
  18. opcache.enable=1
  19. zend_extension=opcache.so
  20. opcache.memory_consumption=128
  21. opcache.interned_strings_buffer=8
  22. opcache.max_accelerated_files=4000
  23. opcache.revalidate_freq=60
  24. opcache.fast_shutdown=1
  25. opcache.enable_cli=1
  26. opcache.enable=1
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注