前言
前面的文章中说过LAMP架构包括:linux操作系统,Apache网站服务器,MySQL数据库,Perl、php或者Python编程语言,而今天要说的LNMMP 和LAMP类似,只是作为Web服务器的不再是Apache而是高性能的Nginx,同时引进Memcached加速缓存效率,用于加快访问速度。
Memcached是一款开源、高性能、分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对数据库的访问来加速Web应用程序。它是一个基于内存的“键值对”存储,用于存储数据库调用、API调用或页面引用结果的直接数据,如字符串、对象等。
实现过程
实验拓扑
实验环境
系统环境:centos6.6
web服务器:172.16.10.123 nginx-1.6.3
PHP服务器:172.16.10.110 php-5.4.26
数据库服务器:172.16.10.211 MariaDB-5.5.36
Memcached服务器:172.16.10.212 memcached-1.4.24
工作原理
利用nginx的高性能特点做前端反向代理服务器,分发用户请求,静态请求直接返回结果,动态请求交给后端php处理,php查询数据库返回处理结果,并将结果缓存至Memcached,当接收新请求时,php首先在Memcached查询,Memcached有结果直接返还给nginx,没结果再查询数据库,依次类推。
安装配置nginx
#解决依赖关系 [root@node1 ~]# yum groupinstall "Development Tools" "Server Platform Deveopment" -y [root@node1 ~]# yum install openssl-devel pcre-devel -y [root@node1 ~]# groupadd -r nginx [root@node1 ~]# useradd -r -g nginx nginx [root@node1 ~]# tar xf nginx-1.6.3.tar.gz [root@node1 ~]# cd nginx-1.6.3 [root@node1 nginx-1.6.3]# ./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/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@node1 nginx-1.6.3]# make && make install
为nginx提供SysV init脚本
[root@node1 ~]# vim /etc/rc.d/init.d/nginx #新建文件/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 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=([^ ]*).*//g' -` 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 2 esac
为脚本赋予执行权限
[root@node1 ~]# chmod +x /etc/rc.d/init.d/nginx
添加至服务管理列表,并让其开机自动启动
[root@node1 ~]# chkconfig --add nginx [root@node1 ~]# chkconfig nginx on
配置nginx
[root@node1 ~]# vim /etc/nginx/nginx.conf worker_processes 2; #worker进程的个数 error_log /var/log/nginx/error.log notice; #错误日志路径及级别 events { worker_connections 1024; #每个worker能够并发响应的最大请求数 } http { include mime.types; #支持多媒体类型 default_type Application/octet-stream; sendfile on; #由内核直接转发 #keepalive_timeout 0; keepalive_timeout 5; #持久连接5s gzip on; #开启压缩功能 server { listen 80; server_name bbs.scholar.com; add_header X-via $server_addr; #让客户端能够看到代理服务器的IP location / { root /www/bbs; index index.php index.html index.htm; } location ~* .(jpg|jpeg|png|gif|js|css)$ { #匹配静态内容 root /www/bbs; } location ~ .php$ { #匹配动态内容 root /www/bbs; fastcgi_pass 172.16.10.110:9000; #代理到的服务器 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME scripts$fastcgi_script_name; include fastcgi_params; } } } [root@node1 ~]# vim /etc/nginx/fastcgi_params fastcgi_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@node1 ~]# service nginx start
安装配置memcached
#解决依赖关系 [root@scholar ~]# yum groupinstall "Development Tools" "Server Platform Deveopment" -y #安装libevent #memcached依赖于libevent API,因此要事先安装之 [root@scholar ~]# tar xf libevent-2.0.22-stable.tar.gz [root@scholar ~]# cd libevent-2.0.22-stable [root@scholar libevent-2.0.22-stable]# ./configure --prefix=/usr/local/libevent [root@scholar libevent-2.0.22-stable]# make && make install [root@scholar ~]# echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf [root@scholar ~]# ldconfig #安装配置memcached [root@scholar ~]# tar xf memcached-1.4.24.tar.tar [root@scholar ~]# cd memcached-1.4.24 [root@scholar memcached-1.4.24]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent [root@scholar memcached-1.4.24]# make && make install
提供脚本
[root@scholar ~]# vim /etc/init.d/memcached #!/bin/bash # # Init file for memcached # # chkconfig: - 86 14 # description: Distributed memory caching daemon # # processname: memcached # config: /etc/sysconfig/memcached . /etc/rc.d/init.d/functions ## Default variables PORT="11211" USER="nobody" MAXCONN="1024" CACHESIZE="64" RETVAL=0 prog="/usr/local/memcached/bin/memcached" desc="Distributed memory caching" lockfile="/var/lock/subsys/memcached" start() { echo -n $"Starting $desc (memcached): " daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE RETVAL=$? [ $RETVAL -eq 0 ] && success && touch $lockfile || failure echo return $RETVAL } stop() { echo -n $"Shutting down $desc (memcached): " killproc $prog RETVAL=$? [ $RETVAL -eq 0 ] && success && rm -f $lockfile || failure echo return $RETVAL } restart() { stop start } reload() { echo -n $"Reloading $desc ($prog): " killproc $prog -HUP RETVAL=$? [ $RETVAL -eq 0 ] && success || failure echo return $RETVAL } case "$1" in start) start ;; stop) stop ;; restart) restart ;; condrestart) [ -e $lockfile ] && restart RETVAL=$? ;; reload) reload ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|restart|condrestart|status}" RETVAL=1 esac exit $RETVAL
授权并启动服务
[root@scholar ~]# vim /etc/init.d/memcached [root@scholar ~]# chmod +x /etc/init.d/memcached [root@scholar ~]# chkconfig --add memcached [root@scholar ~]# service memcached start
安装配置php
#解决依赖关系 [root@scholar ~]# yum groupinstall "Development tools" "Server Platform Development" -y [root@scholar ~]# yum -y groupinstall "Desktop Platform Development" [root@scholar ~]# yum -y install bzip2-devel libmcrypt-devel [root@scholar ~]# tar xf php-5.4.26.tar.bz2 [root@scholar ~]# cd php-5.4.26 [root@scholar php-5.4.26]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-openssl [root@scholar php-5.4.26]# make && make install
提供配置文件
#为php提供配置文件 [root@scholar php-5.4.26]# cp php.ini-production /etc/php.ini #为php-fpm提供脚本 [root@scholar php-5.4.26]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm [root@scholar php-5.4.26]# chmod +x /etc/rc.d/init.d/php-fpm [root@scholar php-5.4.26]# chkconfig --add php-fpm [root@scholar php-5.4.26]# chkconfig php-fpm on #为php-fpm提供配置文件 [root@scholar php-5.4.26]# cd /usr/local/php [root@scholar php]# cp etc/php-fpm.conf.default etc/php-fpm.conf [root@scholar php]# vim etc/php-fpm.conf pid =/usr/local/php/var/run/php-fpm.pid listen = 172.16.10.110:9000 pm.max_children = 25 #最大子进程数 pm.start_servers = 5 #开机预启动子进程数 pm.min_spare_servers = 2 #最小空闲子进程数 pm.max_spare_servers = 6 #最大空闲子进程数
php安装xcache拓展
[root@scholar ~]# tar xf xcache-3.1.0.tar.bz2 [root@scholar ~]# cd xcache-3.1.0 [root@scholar xcache-3.1.0]# /usr/local/php/bin/phpize [root@scholar xcache-3.1.0]# ./configure --enable-xcache --with-php-config=/usr/local/php /bin/php-config [root@scholar xcache-3.1.0]# make && make install [root@scholar xcache-3.1.0]# mkdir /etc/php.d [root@scholar xcache-3.1.0]# cp xcache.ini /etc/php.d/ [root@scholar xcache-3.1.0]# vim /etc/php.d/xcache.ini [xcache-common] ;; non-windows example: extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so
php安装memcache拓展
[root@scholar ~]# tar xf memcache-2.2.7.tgz [root@scholar ~]# cd memcache-2.2.7 [root@scholar memcache-2.2.7]# /usr/local/php/bin/phpize [root@scholar memcache-2.2.7]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache [root@scholar memcache-2.2.7]# make && make install [root@scholar memcache-2.2.7]# vim /etc/php.ini extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so [root@scholar ~]# service php-fpm start
安装配置mariadb
[root@MariaDB ~]# mkdir /mydata/data -pv [root@MariaDB ~]# groupadd -r mysql [root@MariaDB ~]# useradd -g mysql -r mysql [root@MariaDB ~]# chown -R mysql.mysql /mydata/data [root@MariaDB ~]# tar xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local [root@MariaDB ~]# cd /usr/local [root@MariaDB local]# ln -sv mariadb-5.5.36-linux-x86_64 mysql [root@MariaDB local]# chown -R root.mysql mysql
提供配置及脚本文件
[root@MariaDB local]# mkdir /etc/mysql [root@MariaDB local]# cd mysql [root@MariaDB mysql]# cp /support-files/my-large.cnf /etc/mysql/my.cnf [root@MariaDB mysql]# vim /etc/mysql/my.cnf datadir = /mydata/data [root@MariaDB mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld [root@MariaDB mysql]# chmod +x /etc/rc.d/init.d/mysqld [root@MariaDB mysql]# chkconfig --add mysqld [root@MariaDB mysql]# chkconfig mysqld on
初始化数据库
[root@MariaDB mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data [root@MariaDB ~]# service mysqld start
部署站点
[root@node1 ~]# mkdir /www/bbs -pv [root@node1 ~]# unzip wordPress/ target=_blank class=infotextkey>WordPress-3.2.1-zh_CN.zip [root@node1 ~]# cd wordpress [root@node1 wordpress]# mv * /www/bbs/ #在web和php上分别准备站点文件 #php节点 [root@scholar ~]# cd /www/bbs [root@scholar bbs]# cp wp-config-sample.php wp-config.php [root@scholar bbs]# vim wp-config.php /** WordPress 数据库的名称 */ define('DB_NAME', 'wpdb'); /** MySQL 数据库用户名 */ define('DB_USER', 'wpuser'); /** MySQL 数据库密码 */ define('DB_PASSWORD', 'wppass'); /** MySQL 主机 */ define('DB_HOST', '172.16.10.211'); /** 创建数据表时默认的文字编码 */ define('DB_CHARSET', 'utf8');
创建数据库并授权
MariaDB [(none)]> create database wpdb; Query OK, 1 row affected (0.05 sec) MariaDB [(none)]> grant all on wpdb.* to wpuser@'172.16.%.%' identified by 'wppass'; Query OK, 0 rows affected (0.06 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.03 sec)
安装memadmin
MemAdmin是一款可视化的Memcached管理与监控工具,使用PHP开发,体积小,操作简单。
主要功能:
服务器参数监控:STATS、SETTINGS、ITEMS、SLABS、SIZES实时刷新 服务器性能监控:GET、DELETE、INCR、DECR、CAS等常用操作命中率实时监控 支持数据遍历,方便对存储内容进行监视 支持条件查询,筛选出满足条件的KEY或VALUE 数组、JSON等序列化字符反序列显示 兼容memcache协议的其他服务,如Tokyo Tyrant (遍历功能除外) 支持服务器连接池,多服务器管理切换方便简洁 [root@node1 ~]# tar xf memadmin-1.0.12.tar.gz -C /www/bbs/ #web和php端都需执行此操作
登陆后添加服务器
开始管理服务器
更多细节有兴趣可自行探索
Ten end
LNMMP架构实现Web动静分离实验就说到这里了,整个部署过程跟LAMP类似,朋友们部署过程遇到问题可留言交流,nginx在反向代理时还可将缓存缓存至memcached服务器,从而提高缓存性能,这里稍微一提,就不做详解了。以上仅为个人学习整理,如有错漏,大神勿喷