secce 发表于 2018-12-12 06:31:16

memcache缓存服务器(nginx php memcache mysql)

memcache缓存服务器(nginx php memcache mysql)
  环境:
  192.168.1.23nginx+php
  192.168.1.28memcache
  192.168.1.27mysql
  

  一、安装 nginx (192.168.1.23)
  1、解压 zlib 和pcre 不需要编译,只需要解压就行。
  # tar zxf zlib-1.2.8.tar.gz
  # tar zxf pcre-8.39.tar.gz
  2、安装依赖包
  #yum -y install gcc gcc-c++ make libtool openssl openssl-devel
  3、解压源码包
  ①下载 nginx 的源码包: http://nginx.org/download
  # tar zxf nginx-1.14.0.tar.gz
  # cd nginx-1.14.0/
  # groupadd www
  # useradd -g www www -s /sbin/nologin
  #./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
  # make && make install
  # ln -s /usr/local/nginx1.14/sbin/nginx /usr/local/sbin/
  # 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
  # nginx
  # netstat -anpt | grep nginx
  4、关闭防火墙或者开启端口
  # systemctl stop firewalld.service
http://s1.运维网.com/images/20180524/1527164004269263.png
  5、安装 php
  ①安装 libmcrypt
  # tar zxf libmcrypt-2.5.7.tar.gz
  # cd libmcrypt-2.5.7/
  #./configure --prefix=/usr/local/libmcrypt && make && make install
  ②安装依赖包
  # yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel
  # tar zxf php-5.6.27.tar.gz
  # cd 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
  #make && make install
  # cp php.ini-production /etc/php.ini
  ③修改/etc/php.ini 文件,将 short_open_tag 修改为 on
  #vim /etc/php.ini
   ;short_open_tag   改成short_open_tag=0       //第151行(支持 php 短标签 )
  ④创建 php-fpm 服务启动脚本:
  #cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  #chmod +x /etc/init.d/php-fpm
  # chkconfig --add php-fpm
  #chkconfig php-fpm on
  ⑤提供 php-fpm 配置文件并编辑
  # cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
  #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 服务
  #service php-fpm start
  Starting php-fpmdone
  # 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
  # tar zxf libevent-2.0.22-stable.tar.gz
  # cd libevent-2.0.22-stable/
  # ./configure
  # make && make install
  ①安装 memcached
  # tar zxf memcached-1.4.33.tar.gz
  # cd memcached-1.4.33/
  # ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local
  # make && make install
  ②检测是否成功安装
  #ls /usr/local/memcached/bin/memcached
  /usr/local/memcached/bin/memcached
  ③为系统环境变量 LD_LIBRARY_PATH 增加新的目录
  # vim ~/.bash_profile
  添加:
  MEMCACHED_HOME=/usr/local/memcached
  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MEMCACHED_HOME/lib
  # systemctl stop firewalld.service
  #/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
  #netstat -anpt |grep memcached
  tcp      0      0 192.168.1.28:11211      0.0.0.0:*               LISTEN      64362/memcached
  ④刷新用户环境变量:
  # source ~/.bash_profile
  ⑤编写 memcached 服务启停脚本
  #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
  #chmod +x /etc/init.d/memcached
  #chkconfig --add memcached
  #chkconfig memcached on
  2、配置 nginx.conf 文件(在 nginx 主机操作)
  ①修改配置文件
  # 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 服务
  # nginx -sreload
  # netstat -anpt | grep nginx
  tcp      0      0 0.0.0.0:80            0.0.0.0:*               LISTEN      15512/nginx: worker
  ③生成一个 php 测试页
  # cd /usr/local/nginx1.14/html/
  # vim test1.php
  添加:
  
  3、memcache 客户端(在 nginx、php 服务器操作)
  ①安装memcache客户端
  # tar zxf memcache-3.0.8.tgz
  # cd memcache-3.0.8/
  # /usr/local/php5.6/bin/phpize
  #./configure --enable-memcache --with-php-config=/usr/local/php5.6/bin/php-config
  #make && make install
  ②安装完后会有类似这样的提示:
  Installing shared extensions:
  /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/
  ③把这个记住,然后修改 php.ini
  # vim /etc/php.ini
  添加:
  extension=/usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/memcache.so
  ④重启 php-fpm 服务
  # service php-fpm restart
  Gracefully shutting down php-fpm . done
  Starting php-fpmdone
  4、测试:
  ①检查 php 扩展是否正确安装查询结果中是否有 memcache 项
  ②创建 phpinfo()页面,查询 session 项下面的 Registered save handlers 值中是否有 memcache
  项
  ③浏览器访问 test1.php
http://s1.运维网.com/images/20180524/1527164398637051.png
  ④测试代码:
  # cd /usr/local/nginx1.14/html/
  # vim test2.php
  添加:
  
页: [1]
查看完整版本: memcache缓存服务器(nginx php memcache mysql)