memcached

1. memcached

内存数据库   NOSQL     redis
  
  
  

2. 部署Nginx

cat>/etc/yum.repos.d/nginx.repo<<"EOF"
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF

#2.安装Nginx

[root@web01 ~]# yum install -y nginx

#3.配置Nginx进程运行用户

[root@web01 ~]# groupadd -g666 www
[root@web01 ~]# useradd -u666 -g666 www
[root@web01 ~]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf

#4.启动Nginx,并将Nginx加入开机自启

[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

3. 部署PHP

[root@web01 ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 
[root@web01 ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

[root@web01 ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached

[root@web01 ~]# sed  -i  '/^user/cuser = www'  /etc/php-fpm.d/www.conf

[root@web01 ~]# sed  -i  '/^group/cgroup = www'  /etc/php-fpm.d/www.conf

[root@web01 ~]# systemctl  start  php-fpm

4. 部署网站

[root@web01 ~]# cat  /etc/nginx/conf.d/php.conf 
server {
	listen 80;
	server_name  php.qls.com;
	location / {
		root  /code/phpmyadmin;
		index index.php index.html;
	}	
	location ~ .php$ {
	       root           /code/phpmyadmin;
	       fastcgi_pass   127.0.0.1:9000;
	       fastcgi_index  index.php;
	       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
	       include        fastcgi_params;
   }
}

#配置站点目录

[root@web01 ~]# wget  https://files.phpmyadmin.net/phpMyAdmin/5.0.0-rc1/phpMyAdmin-5.0.0-rc1-all-languages.zip

[root@web01 ~]# mkdir  /code

[root@web01 ~]# unzip phpMyAdmin-5.0.0-rc1-all-languages.zip

[root@web01 ~]# mv  phpMyAdmin-5.0.0-rc1-all-languages  /code/phpmyadmin

[root@web01 ~]# chown  -R  www.www  /code/phpmyadmin
[root@web01 ~]# ll /code/
total 8
drwxr-xr-x 12 www www 4096 Nov 22 03:07 phpmyadmin

#重启生效

[root@web01 ~]# systemctl  restart  nginx

[root@web01 ~]# ll /var/lib/php/
total 0
drwxrwx--- 2 root apache 6 Oct 26 18:50 session
drwxrwx--- 2 root apache 6 Oct 26 18:50 wsdlcache
[root@web01 ~]# chown  -R www.www /var/lib/php/session/

5. 安装部署数据库

[root@db01 ~]# yum  install  -y  mariadb  mariadb-server

[root@db01 ~]# systemctl  start mariadb

[root@db01 ~]# mysqladmin   password  '123'

[root@db01 ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 5
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> grant all privileges on *.* to qls@'10.0.0.%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 

#编辑网站配置文件

[root@web01 phpmyadmin]# cd   /code/phpmyadmin/

[root@web01 phpmyadmin]# cp  config.sample.inc.php  config.inc.php 

[root@web01 phpmyadmin]# grep  'host'  config.inc.php
$cfg['Servers'][$i]['host'] = '10.0.0.51';



6. 配置负载均衡

[root@lb01 ~]# cat /etc/nginx/conf.d/php.conf 
upstream php {
	server 10.0.0.7:80;
	server 10.0.0.8:80;
}

server {

	listen 80;
	server_name php.qls.com;
	location / {

	proxy_pass  http://php;
	proxy_set_header Host $host;
	}
}


7. 安装部署Memcached

#服务端部署


#安装依赖库
[root@db01 ~]# yum install  -y libevent libevent-devel


[root@db01 ~]# yum install  -y  memcached

[root@db01 ~]# systemctl  start  memcached
[root@db01 ~]# netstat  -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      8117/mysqld         
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      8308/memcached      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      6973/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7122/master         
tcp6       0      0 :::11211                :::*                    LISTEN      8308/memcached      
tcp6       0      0 :::22                   :::*                    LISTEN      6973/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      7122/master       


#客户端部署

#下载插件
[root@web01 ~]# git clone https://github.com/websupport-sk/pecl-memcache.git


[root@web01 ~]# cd  pecl-memcache/

#生成扩展库
[root@web01 pecl-memcache]# phpize
Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303

#下载依赖
[root@web01 pecl-memcache]# yum  install -y  zlib  zlib-devel

#预编译
[root@web01 pecl-memcache]# ./configure

#编译
[root@web01 pecl-memcache]# make

#安装
[root@web01 pecl-memcache]# make  install 
Installing shared extensions:     /usr/lib64/php/modules/

[root@web01 pecl-memcache]# vim  /etc/php.ini
extension_dir = "/usr/lib64/php/modules/"
extension =  memcache.so

#创建一个php的信息说明

[root@web01 ~]# vim  /code/phpmyadmin/info.php
<?php
        phpinfo();
?>

[root@web01 ~]# systemctl  restart  php-fpm

[root@web01 ~]# chown  www.www /code/phpmyadmin/php.info


#测试php与memcached的连通性

[root@web01 ~]# cat>/code/phpmyadmin/test_memcache.php<<"EOF"
> <?php
> $memcache = new Memcache;
> $memcache->connect('172.16.1.51', 11211) or die ("Could not connect server");
> $memcache->set('key', 'Memcache connect OK');
> $get = $memcache->get('key');
> echo $get;
> ?>
> EOF
[root@web01 ~]# 
[root@web01 ~]# chown  www.www /code/phpmyadmin/test_memcache.php
[root@web01 ~]# 
[root@web01 ~]# php /code/phpmyadmin/test_memcache.php
Memcache connect OK		#成功


#配置memcached缓存路径
[root@web01 ~]# vim  /etc/php.ini 
1231 session.save_handler = memcache
1264 session.save_path = "tcp://10.0.0.51:11211"

#注释下面两行内容
[root@web01 ~]# vim  /etc/php-fpm.d/www.conf
;php_value[session.save_handler] = files
;php_value[session.save_path]    = /var/lib/php/session

[root@web01 ~]# systemctl  restart php-fpm

#登录memcached
[root@db01 ~]# telnet  127.0.0.1  11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats items				#查看所有的key
STAT items:1:number 1
STAT items:1:age 475
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0

stats cachedump 16 0		#查看指定的key的信息
ITEM 210444c3903ec58b63f1f4c915d7878b [2451 b; 1576086803 s]
END

原文地址:https://www.cnblogs.com/223zhp/p/12061400.html