Nginx(四):LNMMP架构实现Web动静分离
简介:
Memcached是一款开源、高性能、分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对Database的访问来加速web应用程序。它是一个基于内存的“键值对”存储,用于存储数据库调用、API调用或页面引用结果的直接数据,如字符串、对象等。
Memcached是一款开发工具,它既不是一个代码加速器,也不是数据库中间件。其设计哲学思想主要反映在如下方面:
1、简单key/value存储:服务器不关心数据本身的意义及结构,只要是可序列化数据即可。存储项由“键、过期时间、可选的标志及数据”四个部分组成;
2、功能的实现一半依赖于客户端,一半基于服务器端:客户负责发送存储项至服务器端、从服务端获取数据以及无法连接至服务器时采用相应的动作;服务端负责接收、存储数据,并负责数据项的超时过期;
3、各服务器间彼此无视:不在服务器间进行数据同步;
4、O(1)的执行效率;
5、清理超期数据:默认情况下,Memcached是一个LRU缓存,同时,它按事先预订的时长清理超期数据;但事实上,memcached不会删除任何已缓存数据,只是在其过期之后不再为客户所见;而且,memcached也不会真正按期限清理缓存,而仅是当get命令到达时检查其时长;
实验拓扑:
工作原理:
利用nginx的高性能特点做前端反向代理服务器,分发用户请求,静态请求直接返回结果,动态请求交给后端php处理,php查询数据库返回处理结果,并将结果缓存至Memcached,当接收新请求时,php首先在Memcached查询,Memcached有结果直接返还给nginx,没结果再查询数据库,依次类推。
实验环境:
OS:CentOS-6.7-x86_64Nginx:192.168.1.4PHP-fpm:192.168.1.5Mysql:192.168.1.6Memcached:192.168.1.7
实验过程:
编译安装Nginx-1.8:
安装开发环境,并解决软件依赖关系:
[root@node0 ~]# yum groupinstall "Development Tools" "Server Platform Deveopment"[root@node0 ~]# yum install pcre-devel zlib-devel
创建nginx用户和nginx组:
[root@node0 ~]# groupadd -r nginx[root@node0 ~]# useradd -g nginx -r nginx
编译安装:
[root@node0 ~]# tar xf nginx-1.8.0.tar.gz[root@node0 ~]# cd nginx-1.8.0/[root@node0 nginx-1.8.0]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi --with-pcre[root@node0 nginx-1.8.0]# make && make install
提供服务启动脚本:
[root@node0 ~]# cat /etc/rc.d/init.d/nginx#!/bin/sh## nginx - this script starts and stops the nginx daemon## chkconfig: - 85 15# description: NGINX is an HTTP(S) server, HTTP(S) reverse \# proxy and IMAP/POP3 proxy server# processname: nginx# config: /etc/nginx/nginx.conf# config: /etc/sysconfig/nginx# pidfile: /var/run/nginx.pid# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0nginx="/usr/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginx.lockmake_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done}start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval}stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval}restart() { configtest || return $? stop sleep 1 start}reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo}force_reload() { restart}configtest() { $nginx -t -c $NGINX_CONF_FILE}rh_status() { status $prog}rh_status_q() { rh_status >/dev/null 2>&1}case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2esac
nginx主配置文件支持vim高亮:
下载nginx.vim:
[root@node0 ~]# mkdir -p ./.vim/syntax[root@node0 ~]# cp nginx.vim ./.vim/syntax/[root@node0 ~]# vim ./.vim/filetype.vimau BufRead,BufNewFile /etc/nginx/* if &ft == '' | setfiletype nginx | endif
编辑配置文件:
[root@node0 ~]# vim /etc/nginx/nginx.confworker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name www.chencer.org; add_header X-via $server_addr; location / { root /web/www; index index.php index.html index.hml; } location ~* \.(jpg|jpeg|png|gif|js|css)$ { root /web/www; } location ~ \.php$ { root /web/www; fastcgi_pass 192.168.1.5:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }}[root@node0 ~]# vim /etc/nginx/fastcgi_paramsfastcgi_param GATEWAY_INTERFACE CGI/1.1;fastcgi_param SERVER_SOFTWARE nginx;fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_param REQUEST_URI $request_uri;fastcgi_param DOCUMENT_URI $document_uri;fastcgi_param DOCUMENT_ROOT $document_root;fastcgi_param SERVER_PROTOCOL $server_protocol;fastcgi_param REMOTE_ADDR $remote_addr;fastcgi_param REMOTE_PORT $remote_port;fastcgi_param SERVER_ADDR $server_addr;fastcgi_param SERVER_PORT $server_port;fastcgi_param SERVER_NAME $server_name;
启动服务:
[root@node0 ~]# chmod +x /etc/rc.d/init.d/nginx[root@node0 ~]# chkconfig nginx --add[root@node0 ~]# chkconfig nginx on[root@node0 ~]# service nginx start
编译安装PHP-5.4;
安装编译环境,并解决软件依赖关系:
[root@node1 ~]# yum groupinstall "Server Platform Development" "Development tools" "Desktop Platform Development"[root@node1 ~]# yum install bzip2-devel libmcrypt-devel mhash-devel# 注意:libmcrypt-devel和mhash-devel来自于epel源
编译安装php:
[root@node1 ~]# tar xf php-5.4.45.tar.bz2 [root@node1 ~]# cd php-5.4.45/[root@node1 php-5.4.45]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --with-libxml-dir=/usr --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2[root@node1 php-5.4.45]# make && make install
导出头文件、库文件:
[root@node1 ~]# ln -sv /usr/local/php/include /usr/include/php"/usr/include/php" -> "/usr/local/php/include"[root@node1 ~]# vim /etc/ld.so.conf.d/php.conf/usr/local/php/lib[root@node1 ~]# ldconfig
添加并重读环境变量:
[root@node1 ~]# vim /etc/profile.d/php.shexport PATH=/usr/local/php/bin:/usr/local/php/sbin:$PATH[root@node1 ~]# source /etc/profile.d/php.sh
提供php和php-fpm配置文件:
[root@node1 ~]# cp ./php-5.4.45/php.ini-production /etc/php.ini[root@node1 ~]# cd /usr/local/php/etc/[root@node1 etc]# cp php-fpm.conf.default php-fpm.conf[root@node1 etc]# vim php-fpm.conflisten = 192.168.0.5:9000pm.max_children = 50pm.start_servers = 5pm.min_spare_servers = 2pm.max_spare_servers = 8pid = /usr/local/php/var/run/php-fpm.pid
编译安装xcache:
[root@node1 ~]# tar xf xcache-3.2.0.tar.bz2 [root@node1 ~]# cd xcache-3.2.0/[root@node1 xcache-3.2.0]# phpize[root@node1 xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config[root@node1 xcache-3.2.0]# make && make install
编译安装php-memecache扩展:
[root@node1 ~]# tar xf memcache-2.2.7.tar.gz [root@node1 ~]# cd memcache-2.2.7[root@node1 memcache-2.2.7]# phpize[root@node1 memcache-2.2.7]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache[root@node1 memcache-2.2.7]# make && make install
配置添加xcache和memechace扩展:
[root@node1 ~]# mkdir /etc/php.d[root@node1 ~]# cp ./xcache-3.2.0/xcache.ini /etc/php.d/[root@node1 ~]# vim /etc/php.d/xcache.iniextension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so[root@node1 ~]# vim /etc/php.iniextension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
为php-fpm提供服务启动脚本:
[root@node1 ~]# cp ./php-5.4.45/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm[root@node1 ~]# chmod +x /etc/rc.d/init.d/php-fpm
添加并启动服务:
[root@node1 ~]# chkconfig php-fpm --add[root@node1 ~]# chkconfig php-fpm on[root@node1 ~]# service php-fpm start
二进制格式安装Mysql-5.5:
创建数据目录和mysql用户:
[root@node2 ~]# mkdir -p /data/mydata[root@node2 ~]# groupadd -r mysql[root@node2 ~]# useradd -g mysql -r mysql[root@node2 ~]# chown -R mysql.mysql /data/
解决软件依赖关系:
[root@node2 ~]# yum install libaio
二进制安装设置:
[root@node2 ~]# tar xf mysql-5.5.47-linux2.6-x86_64.tar.gz -C /usr/local/[root@node2 ~]# cd /usr/local/[root@node2 local]# ln -sv mysql-5.5.47-linux2.6-x86_64 mysql"mysql" -> "mysql-5.5.47-linux2.6-x86_64"[root@node2 local]# cd mysql/[root@node2 mysql]# chown -R root.mysql ./*[root@node2 mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mydata/
导出头文件、库文件、man手册:
[root@node2 mysql]# ln -sv /usr/local/mysql/include /usr/include/mysql"/usr/include/mysql" -> "/usr/local/mysql/include"[root@node2 mysql]# vim /etc/ld.so.conf.d/mysql-5.5-x86_64.conf/usr/local/mysql/lib[root@node2 mysql]# ldconfig[root@node2 mysql]# vim /etc/man.configMANPATH /usr/local/mysql/man
添加并重读环境变量:
[root@node2 mysql]# vim /etc/profile.d/mysql.shexport PATH=/usr/local/mysql/bin:$PATH[root@node2 mysql]# source /etc/profile.d/mysql.sh
提供配置文件和服务启动脚本:
[root@node2 mysql]# mkdir /etc/mysql[root@node2 mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf[root@node2 mysql]# vim /etc/mysql/my.cnf[mysqld]datadir = /data/mydatathread_concurrency = 2[root@node2 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld[root@node2 mysql]# chmod +x /etc/rc.d/init.d/mysqld
启动服务:
[root@node2 mysql]# chkconfig --add mysqld[root@node2 mysql]# chkconfig mysqld on[root@node2 mysql]# service mysqld start
安装memcached:
memcached依赖于libevent API,yum安装过程中会自动解决依赖关系从base源中下载并安装libevent;
[root@node3 ~]# yum install memcached
启动服务:
[root@node3 ~]# chkconfig memcached on[root@node3 ~]# service memcached start
分别在nginx和php-fpm上部署站点文件:
[root@node0 ~]# mkdir /web[root@node0 ~]# tar xf wordpress-4.4.1-zh_CN.tar.gz [root@node0 ~]# mv wordpress /web/www[root@node0 ~]# cd /web/www/[root@node0 www]# cp wp-config-sample.php wp-config.php[root@node0 www]# vim wp-config.phpdefine('DB_NAME', 'webdb');define('DB_USER', 'webuser');define('DB_PASSWORD', 'webpass');define('DB_HOST', '192.168.1.6');[root@node0 ~]# scp -r /web/www node1:/web/
创建数据库并授权:
[root@node2 ~]# mysqlmysql> create database webdb;Query OK, 1 row affected (0.00 sec)mysql> grant all on *.* to webuser@'192.168.1.%' identified by 'webpass';Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)
站点安装:
安装memadmin状态页面:
MemAdmin是一款可视化的Memcached管理与监控工具,使用PHP开发,体积小,操作简单。
主要功能:
服务器参数监控:STATS、SETTINGS、ITEMS、SLABS、SIZES实时刷新
服务器性能监控:GET、DELETE、INCR、DECR、CAS等常用操作命中率实时监控
支持数据遍历,方便对存储内容进行监视
支持条件查询,筛选出满足条件的KEY或VALUE
数组、JSON等序列化字符反序列显示
兼容memcache协议的其他服务,如Tokyo Tyrant (遍历功能除外)
支持服务器连接池,多服务器管理切换方便简洁
nginx和php都要安装memadmin状态页面:
[root@node0 ~]# tar xf memadmin-1.0.12.tar.gz -C /web/www/
登陆添加服务器: