设为首页 收藏本站
查看: 467|回复: 0

[经验分享] memcache缓存服务器(nginx php memcache mysql)

[复制链接]

尚未签到

发表于 2018-12-12 06:31:16 | 显示全部楼层 |阅读模式
memcache缓存服务器(nginx php memcache mysql)

  环境:
  192.168.1.23  nginx+php
  192.168.1.28  memcache
  192.168.1.27  mysql
  

  一、安装 nginx (192.168.1.23)
  1、解压 zlib 和pcre 不需要编译,只需要解压就行。
  [root@localhost ~]# tar zxf zlib-1.2.8.tar.gz
  [root@localhost ~]# tar zxf pcre-8.39.tar.gz
  2、安装依赖包
  [root@localhost ~]#yum -y install gcc gcc-c++ make libtool openssl openssl-devel
  3、解压源码包
  ①下载 nginx 的源码包: http://nginx.org/download
  [root@localhost ~]# tar zxf nginx-1.14.0.tar.gz
  [root@localhost ~]# cd nginx-1.14.0/
  [root@localhost nginx-1.14.0]# groupadd www
  [root@localhost nginx-1.14.0]# useradd -g www www -s /sbin/nologin
  [root@localhost nginx-1.14.0]#./configure --prefix=/usr/local/nginx1.14 \
  --with-http_dav_module --with-http_stub_status_module \
  --with-http_addition_module --with-http_sub_module \
  --with-http_flv_module --with-http_mp4_module \
  --with-pcre=/root/pcre-8.39 --with-zlib=/root/zlib-1.2.8 \
  --with-http_ssl_module --with-http_gzip_static_module --user=www \
  --group=www
  [root@localhost nginx-1.14.0]# make && make install
  [root@localhost nginx-1.14.0]# ln -s /usr/local/nginx1.14/sbin/nginx /usr/local/sbin/
  [root@localhost nginx-1.14.0]# nginx -t
  nginx: the configuration file /usr/local/nginx1.14/conf/nginx.conf syntax is ok
  nginx: configuration file /usr/local/nginx1.14/conf/nginx.conf test is successful
  [root@localhost nginx-1.14.0]# nginx
  [root@localhost nginx-1.14.0]# netstat -anpt | grep nginx
  4、关闭防火墙或者开启端口
  [root@localhost nginx-1.14.0]# systemctl stop firewalld.service

  5、安装 php
  ①安装 libmcrypt
  [root@localhost ~]# tar zxf libmcrypt-2.5.7.tar.gz
  [root@localhost ~]# cd libmcrypt-2.5.7/
  [root@localhost libmcrypt-2.5.7]#  ./configure --prefix=/usr/local/libmcrypt && make && make install
  ②安装依赖包
  [root@localhost libmcrypt-2.5.7]# yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel
  [root@localhost ~]# tar zxf php-5.6.27.tar.gz
  [root@localhost ~]# cd php-5.6.27/
  [root@localhost php-5.6.27]#./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd \
  --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets \
  --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib \
  --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt \
  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 \
  --enable-maintainer-zts
  [root@localhost php-5.6.27]#make && make install
  [root@localhost php-5.6.27]# cp php.ini-production /etc/php.ini
  ③修改/etc/php.ini 文件,将 short_open_tag 修改为 on
  [root@localhost php-5.6.27]#vim /etc/php.ini
   ;short_open_tag   改成  short_open_tag=0         //第151行(支持 php 短标签 )
  ④创建 php-fpm 服务启动脚本:
  [root@localhost php-5.6.27]#  cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  [root@localhost php-5.6.27]#  chmod +x /etc/init.d/php-fpm
  [root@localhost php-5.6.27]# chkconfig --add php-fpm
  [root@localhost php-5.6.27]#  chkconfig php-fpm on
  ⑤提供 php-fpm 配置文件并编辑
  [root@localhost php-5.6.27]# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
  [root@localhost php-5.6.27]#vim /usr/local/php5.6/etc/php-fpm.conf
  第25行去掉;    pid = run/php-fpm.pid
  第164行         listen =127.0.0.1:9000
  第235行         pm.max_children = 300
  第240行         pm.start_servers = 10
  第245行  pm.min_spare_servers = 10
  第250行         pm.max_spare_servers =50
  ⑥启动 php-fpm 服务
  [root@localhost php-5.6.27]#  service php-fpm start
  Starting php-fpm  done
  [root@localhost php-5.6.27]# netstat -anpt | grep php-fpm
  tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      52269/php-fpm: mast
  

  

  二、安装MySQL省略(192.168.1.27)
  

  

  三、安装 memcached 服务端 (192.168.1.28)
  1、首先先安装 memcached 依赖库 libevent
  [root@localhost ~]# tar zxf libevent-2.0.22-stable.tar.gz
  [root@localhost ~]# cd libevent-2.0.22-stable/
  [root@localhost libevent-2.0.22-stable]# ./configure
  [root@localhost libevent-2.0.22-stable]# make && make install
  ①安装 memcached
  [root@localhost ~]# tar zxf memcached-1.4.33.tar.gz
  [root@localhost ~]# cd memcached-1.4.33/
  [root@localhost memcached-1.4.33]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local
  [root@localhost memcached-1.4.33]# make && make install
  ②检测是否成功安装
  [root@localhost memcached-1.4.33]#  ls /usr/local/memcached/bin/memcached
  /usr/local/memcached/bin/memcached
  ③为系统环境变量 LD_LIBRARY_PATH 增加新的目录
  [root@localhost memcached-1.4.33]# vim ~/.bash_profile
  添加:
  MEMCACHED_HOME=/usr/local/memcached
  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MEMCACHED_HOME/lib
  [root@localhost ~]# systemctl stop firewalld.service
  [root@localhost ~]#  /usr/local/memcached/bin/memcached -d -m 2048 -l 192.168.1.28 -p 11211 -u root -c 10240 -P /usr/local/memcached/memcached.pid
  [root@localhost ~]#  netstat -anpt |grep memcached
  tcp        0      0 192.168.1.28:11211      0.0.0.0:*               LISTEN      64362/memcached
  ④刷新用户环境变量:
  [root@localhost ~]# source ~/.bash_profile
  ⑤编写 memcached 服务启停脚本
  [root@localhost memcached-1.4.33]#vi /etc/init.d/memcached
  添加:
  #!/bin/sh
  #
  # pidfile: /usr/local/memcached/memcached.pid
  # memcached_home: /usr/local/memcached
  # chkconfig: 35 21 79
  # description: Start and stop memcached Service
  # Source function library
  . /etc/rc.d/init.d/functions
  RETVAL=0
  prog="memcached"
  basedir=/usr/local/memcached
  cmd=${basedir}/bin/memcached
  pidfile="$basedir/${prog}.pid"
  #interface to listen on (default: INADDR_ANY, all addresses)
  ipaddr="192.168.1.28"
  #listen port
  port=11211
  #username for memcached
  username="root"
  #max memory for memcached,default is 64M
  max_memory=2048
  #max connections for memcached
  max_simul_conn=10240
  start() {
  echo -n $"Starting service: $prog"
  $cmd -d -m $max_memory -u $username -l $ipaddr -p $port -c $max_simul_conn -P $pidfile
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
  }
  stop() {
  echo -n $"Stopping service: $prog "
  run_user=$(whoami)
  pidlist=$(ps -ef | grep $run_user | grep memcached | grep -v grep | awk '{print($2)}')
  for pid in $pidlist
  do
  kill -9 $pidif [ $? -ne 0 ]; then
  return 1
  fi
  done
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
  }
  # See how we were called.
  case "$1" in
  start)
  start
  ;;
  stop)
  stop
  ;;
  restart)
  stop
  start
  ;;
  *)
  echo "Usage: $0 {start|stop|restart|status}"
  exit 1
  esac
  exit $RETVAL
  [root@localhost ~]#  chmod +x /etc/init.d/memcached
  [root@localhost ~]#  chkconfig --add memcached
  [root@localhost ~]#  chkconfig memcached on
  2、配置 nginx.conf 文件(在 nginx 主机操作)
  ①修改配置文件
  [root@localhost ~]# vi /usr/local/nginx1.14/conf/nginx.conf
  全删掉然后添加:
  user www www;
  worker_processes 2;
  worker_cpu_affinity 01 10;
  error_log logs/error.log;
  #error_log logs/error.log notice;
  #error_log logs/error.log info;pid logs/nginx.pid;
  events {
  use epoll;
  worker_connections 65535;
  multi_accept on;
  }
  http {
  include mime.types;
  default_type application/octet-stream;
  #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  # '$status $body_bytes_sent "$http_referer" '
  # '"$http_user_agent" "$http_x_forwarded_for"';
  #access_log logs/access.log main;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 65;
  tcp_nodelay on;
  client_header_buffer_size 4k;
  open_file_cache max=102400 inactive=20s;
  open_file_cache_valid 30s;
  open_file_cache_min_uses 1;
  client_header_timeout 15;
  client_body_timeout 15;
  reset_timedout_connection on;
  send_timeout 15;
  server_tokens off;
  client_max_body_size 10m;
  fastcgi_connect_timeout 600;
  fastcgi_send_timeout 600;
  fastcgi_read_timeout 600;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  fastcgi_temp_path /usr/local/nginx1.14/nginx_tmp;
  fastcgi_intercept_errors on;
  fastcgi_cache_path /usr/local/nginx1.14/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;
  gzip on;
  gzip_min_length 2k;
  gzip_buffers 4 32k;
  gzip_http_version 1.1;
  gzip_comp_level 6;
  gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
  gzip_vary on;
  gzip_proxied any;
  server {
  listen 80;
  server_name www.benet.com;
  #charset koi8-r;
  #access_log logs/host.access.log main;
  location ~* ^.+\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {
  valid_referers none blocked www.benet.com benet.com;
  if ($invalid_referer) {
  #return 302 http://www.benet.com/img/nolink.jpg;
  return 404;
  break;
  }
  access_log off;
  }
  location / {
  root html;
  index index.php index.html index.htm;
  }
  location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)$ {
  expires 30d;
  #log_not_found off;
  access_log off;
  }
  location ~* \.(js|css)$ {
  expires 7d;
  log_not_found off;
  access_log off;
  }location = /(favicon.ico|roboots.txt) {
  access_log off;
  log_not_found off;
  }
  location /status {
  stub_status on;
  }
  location ~ .*\.(php|php5)?$ {
  root html;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  include fastcgi.conf;
  fastcgi_cache cache_fastcgi;
  fastcgi_cache_valid 200 302 1h;
  fastcgi_cache_valid 301 1d;
  fastcgi_cache_valid any 1m;
  fastcgi_cache_min_uses 1;
  fastcgi_cache_use_stale error timeout invalid_header http_500;
  fastcgi_cache_key http://$host$request_uri;
  }
  #error_page 404 /404.html;
  # redirect server error pages to the static page /50x.html
  #
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
  root html;
  }
  }
  }
  ②重启 nginx 服务
  [root@localhost php-5.6.27]# nginx -s  reload
  [root@localhost php-5.6.27]# netstat -anpt | grep nginx
  tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15512/nginx: worker
  ③生成一个 php 测试页
  [root@localhost ~]# cd /usr/local/nginx1.14/html/
  [root@localhost html]# vim test1.php
  添加:
  
  3、memcache 客户端(在 nginx、php 服务器操作)
  ①安装memcache客户端
  [root@localhost ~]# tar zxf memcache-3.0.8.tgz
  [root@localhost ~]# cd memcache-3.0.8/
  [root@localhost memcache-3.0.8]# /usr/local/php5.6/bin/phpize
  [root@localhost memcache-3.0.8]#./configure --enable-memcache --with-php-config=/usr/local/php5.6/bin/php-config
  [root@localhost memcache-3.0.8]#  make && make install
  ②安装完后会有类似这样的提示:
  Installing shared extensions:
  /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/
  ③把这个记住,然后修改 php.ini
  [root@localhost memcache-3.0.8]# vim /etc/php.ini
  添加:
  extension=/usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/memcache.so
  ④重启 php-fpm 服务
  [root@localhost memcache-3.0.8]# service php-fpm restart
  Gracefully shutting down php-fpm . done
  Starting php-fpm  done
  4、测试:
  ①检查 php 扩展是否正确安装  查询结果中是否有 memcache 项
  ②创建 phpinfo()页面,查询 session 项下面的 Registered save handlers 值中是否有 memcache
  
  ③浏览器访问 test1.php

  ④测试代码:
  [root@localhost memcache-3.0.8]# cd /usr/local/nginx1.14/html/
  [root@localhost html]# vim test2.php
  添加:
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-650243-1-1.html 上篇帖子: centos6.9 php7.1.16安装 下篇帖子: PHP代码分析溯源(第1题)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表