北城书生 的BLOG
前言
前 面的文章中说过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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #解决依赖关系 [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脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | [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=\([^ ]*\).*/\1/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 |
为脚本赋予执行权限
1 | [root@node1 ~] # chmod +x /etc/rc.d/init.d/nginx |
添加至服务管理列表,并让其开机自动启动
1 2 | [root@node1 ~] # chkconfig --add nginx [root@node1 ~] # chkconfig nginx on |
配置nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | [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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #解决依赖关系 [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 |
提供脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | [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" OPTIONS= "" 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 -o "$OPTIONS" 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 |
授权并启动服务
1 2 3 4 | [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
1 2 3 4 5 6 7 8 9 10 11 12 | #解决依赖关系 [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 |
提供配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #为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拓展
1 2 3 4 5 6 7 8 9 10 11 12 13 | [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拓展
1 2 3 4 5 6 7 8 9 10 | [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
1 2 3 4 5 6 7 8 | [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 |
提供配置及脚本文件
1 2 3 4 5 6 7 8 9 10 11 | [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 |
初始化数据库
1 2 | [root@MariaDB mysql] # scripts/mysql_install_db --user=mysql --datadir=/mydata/data [root@MariaDB ~] # service mysqld start |
部署站点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | [root@node1 ~] # mkdir /www/bbs -pv [root@node1 ~] # unzip 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' ); |
创建数据库并授权
1 2 3 4 5 6 7 8 | 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开发,体积小,操作简单。
主要功能:
1 2 3 4 5 6 7 | 服务器参数监控:STATS、SETTINGS、ITEMS、SLABS、SIZES实时刷新 服务器性能监控:GET、DELETE、INCR、DECR、CAS等常用操作命中率实时监控 支持数据遍历,方便对存储内容进行监视 支持条件查询,筛选出满足条件的KEY或VALUE 数组、JSON等序列化字符反序列显示 兼容memcache协议的其他服务,如Tokyo Tyrant (遍历功能除外) 支持服务器连接池,多服务器管理切换方便简洁 |
1 2 3 | [root@node1 ~] # tar xf memadmin-1.0.12.tar.gz -C /www/bbs/ #web和php端都需执行此操作 |
登陆后添加服务器
开始管理服务器
更多细节有兴趣可自行探索
Ten end
LNMMP 架构实现Web动静分离实验就说到这里了,整个部署过程跟LAMP类似,朋友们部署过程遇到问题可留言交流,nginx在反向代理时还可将缓存缓存至 memcached服务器,从而提高缓存性能,这里稍微一提,就不做详解了。以上仅为个人学习整理,如有错漏,大神勿喷~~~
本文出自 “” 博客,请务必保留此出处