当先锋百科网

首页 1 2 3 4 5 6 7

memcached缓存应用

系统环境:
主机名 操作系统 ip地址 服务软件
memcached centos7.5 192.168.80.102 memcached-1.5.9.tar.gz
libevent-2.1.8-stable.tar.gz
web1 centos7.5 192.168.80.103 安装LNMP,memcache-2.2.7.tgz
客户端 windows7 网页浏览器

(在192.168.80.102上)
关闭防火墙及SElinux
systemctl stop firewalld
setenforce 0

memcached服务器:
上传源码包和插件包:
yum install lrz* -y

memcached-1.5.9.tar.gz
libevent-2.1.8-stable.tar.gz

解压
tar xf memcached-1.5.6.tar.gz -C /opt/
tar xf libevent-2.1.8-stable.tar.gz -C /opt/

yum install gcc gcc-c++ make -y
cd /opt/libevent-2.1.8-stable
./configure --prefix=/usr/local/libevent
make && make install

安装memcached:
cd /opt/memcached-1.5.6

./configure
–prefix=/usr/local/memcached
–with-libevent=/usr/local/libevent/
make && make install

ln -s /usr/local/libevent/lib/libevent-2.1.so.6.0.2 /usr/lib64/libevent-2.1.so.6
//不可缺少
cd /usr/local/memcached/bin/
./memcached -d -m 32m -p 11211 -u root //以守护进程的方式分配32MB内存指定端口指定用户账号位root运行Memcached服务
设置Memcached 服务脚本:
vi /etc/init.d/memcached
#!/bin/bash
#chkconfig: 35 99 20
#description: memcached Service Control Script
PROG="/usr/local/memcached/bin/memcached"
case “$1” in
start)
$PROG -d -m 32 -p 11211 -u root
;;
stop)
pkill -9 memcached &> /dev/null
;;
restart)
$0 stop
$0 start
;;
*)
echo “Usage: $0 {start|stop|restart|reload}”
exit 1
esac
exit 0

chmod +x /etc/init.d/memcached //添加运行权限
chkconfig --add memcached //添加到service管理器
service memcached start //启动
netstat -anpt | grep memcached

二、应用程序Memcache API(PHP服务器)——在192.168.80.103上
安装LNMP服务
1.安装nginx
systemctl stop firewalld
yum -y install
pcre-devel
zlib-devel
lrz*

useradd -M -s /sbin/nologin nginx
tar xzvf nginx-1.13.7.tar.gz -C /opt/
cd /opt/nginx-1.13.7

./configure
–prefix=/usr/local/nginx
–user=nginx
–group=nginx
–with-http_stub_status_module

make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

nginx -t //配置文件语法检查
nginx //启动服务

-------制作管理角本--------
vi /etc/init.d/nginx
#!/bin/bash
#chkconfig: 35 99 20
#description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case “$1” in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo “Usage: $0 {start|stop|restart|reload}”
exit 1
esac
exit 0

chmod +x /etc/init.d/nginx
chkconfig --add nginx

vi /usr/local/nginx/conf/nginx.conf
user nginx nginx;
error_log logs/error.log info;

2.安装MySQL
yum -y install
ncurses
ncurses-devel
bison
cmake

useradd -s /sbin/nologin mysql

tar xf mysql-boost-5.7.20.tar.gz
cd mysql-5.7.20/

cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock
-DSYSCONFDIR=/etc
-DSYSTEMD_PID_DIR=/usr/local/mysql
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1
-DMYSQL_DATADIR=/usr/local/mysql/data
-DWITH_BOOST=boost
-DWITH_SYSTEMD=1

make && make install
chown -R mysql.mysql /usr/local/mysql/ 也可mysql:mysql

vi /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

chown mysql:mysql /etc/my.cnf

echo ‘PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH’ >> /etc/profile
echo ‘export PATH’ >> /etc/profile
source /etc/profile

cd /usr/local/mysql/

bin/mysqld
–initialize-insecure
–user=mysql
–basedir=/usr/local/mysql
–datadir=/usr/local/mysql/data

cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld
netstat -anpt | grep 3306

systemctl enable mysqld

3.安装PHP
yum -y install
libjpeg
libjpeg-devel
libpng libpng-devel
freetype freetype-devel
libxml2
libxml2-devel
zlib zlib-devel
curl curl-devel
openssl openssl-devel

tar xjvf php-7.1.10.tar.bz2
cd php-7.1.10
./configure
–prefix=/usr/local/php
–with-mysql-sock=/usr/local/mysql/mysql.sock
–with-mysqli
–with-zlib
–with-curl
–with-gd
–with-jpeg-dir
–with-png-dir
–with-freetype-dir
–with-openssl
–enable-mbstring
–enable-xml
–enable-session
–enable-ftp
–enable-pdo
–enable-tokenizer
–enable-zip
–enable-fpm

make && make install

cp php.ini-development /usr/local/php/lib/php.ini
vi /usr/local/php/lib/php.ini

mysqli.default_socket = /usr/local/mysql/mysql.sock
date.timezone = Asia/Shanghai

/usr/local/php/bin/php -m //验证安装的模块

-----------配置及优化FPM模块--------
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf

cd /usr/local/php/etc/
vi php-fpm.conf

pid = run/php-fpm.pid
;user = nginx
;group = nginx

/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini
netstat -anpt | grep 9000
ln -s /usr/local/php/bin/* /usr/local/bin/
ps aux | grep -c “php-fpm” //结果

-----更新启动脚本----------
vi /etc/init.d/nginx
#!/bin/bash
#chkconfig: 35 99 20
#description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
PROG_FPM="/usr/local/php/sbin/php-fpm"
PIDF_FPM="/usr/local/php/var/run/php-fpm.pid"
case “$1” in
start)
$PROG
$PROG_FPM
;;
stop)
kill -s QUIT $(cat $PIDF)
kill -s QUIT $(cat $PIDF_FPM)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo “Usage: $0 {start|stop|restart|reload}”
exit 1
esac
exit 0

--------以下是让nginx支持PHP功能--------
vi /usr/local/nginx/conf/nginx.conf

location ~ .php$ {
root /var/www/aa;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/aa$fastcgi_script_name; //注意目录名称
include fastcgi_params;
}

vi /var/www/aa/index.php

<?php phpinfo(); ?>

在网页测试http://192.168.80.102/index.php,要注意路径问题。

客户端安装Memcache的PHP扩展功能

#安装autoconf软件包
yum install autoconf -y

#解压
tar xf memcache-2.2.7.tgz -C /opt/

#进入目录
cd /opt/memcache-2.2.7

#增加为PHP的模块后再对memcache进行配置编译
/usr/bin/phpize

#配置
./configure
–enable-memcache
–with-php-config=/usr/bin/php-config

#编译及安装
make && make install

22.编辑php.ini

vi /etc/php.ini

#732行,新增以下命令
extension_dir = “/usr/lib64/php/modules/”

#864行,新增以下命令
extension = memcache.so

23.编写测试页面,测试memcached工作是否正常

vi /var/www/aa/index.php

<?php $memcache = new Memcache(); $memcache->connect('192.168.80.102',11211); $memcache->set('key','Memcache test Successfull!',0,60); $result = $memcache->get('key'); unset($memcache); echo $result; ?>

24.重启httpd服务

service httpd restart

25.客户端进行访问测试是否成功 ,http://192.168.80.102/index.php