LNMMP架构实现Web动静分离
本帖最后由 dfsfsf 于 2015-6-15 08:50 编辑前言
前面的文章中说过LAMP架构包括:Linux操作系统,Apache网站服务器,MySQL数据库,Perl、PHP或者Python编程语言,而今天要说的LNMMP 和LAMP类似,只是作为Web服务器的不再是Apache而是高性能的Nginx,同时引进Memcached加速缓存效率,用于加快访问速度。Memcached是一款开源、高性能、分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对数据库的访问来加速Web应用程序。它是一个基于内存的“键值对”存储,用于存储数据库调用、API调用或页面引用结果的直接数据,如字符串、对象等。实现过程
实验拓扑
实验环境系统环境:CentOS6.6web服务器:172.16.10.123 nginx-1.6.3PHP服务器:172.16.10.110 php-5.4.26数据库服务器:172.16.10.211 MariaDB-5.5.36Memcached服务器: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
29
#解决依赖关系
# yum groupinstall "Development Tools" "Server Platform Deveopment" -y
# yum install openssl-devel pcre-devel -y
# groupadd -r nginx
# useradd -r -g nginx nginx
# tar xf nginx-1.6.3.tar.gz
# cd nginx-1.6.3
# # cd 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
# 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
# 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
# chmod +x /etc/rc.d/init.d/nginx
添加至服务管理列表,并让其开机自动启动
1
2
# chkconfig --add nginx
# 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
# vim /etc/nginx/nginx.conf
worker_processes2; #worker进程的个数
error_log/var/log/nginx/error.lognotice; #错误日志路径及级别
events {
worker_connections1024; #每个worker能够并发响应的最大请求数
}
http {
include mime.types; #支持多媒体类型
default_typeapplication/octet-stream;
sendfile on; #由内核直接转发
#keepalive_timeout0;
keepalive_timeout5; #持久连接5s
gzipon; #开启压缩功能
server {
listen 80;
server_namebbs.scholar.com;
add_header X-via $server_addr; #让客户端能够看到代理服务器的IP
location / {
root /www/bbs;
indexindex.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;
}
}
}
# vim /etc/nginx/fastcgi_params
fastcgi_paramGATEWAY_INTERFACECGI/1.1;
fastcgi_paramSERVER_SOFTWARE nginx;
fastcgi_paramQUERY_STRING $query_string;
fastcgi_paramREQUEST_METHOD $request_method;
fastcgi_paramCONTENT_TYPE $content_type;
fastcgi_paramCONTENT_LENGTH $content_length;
fastcgi_paramSCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_paramSCRIPT_NAME $fastcgi_script_name;
fastcgi_paramREQUEST_URI $request_uri;
fastcgi_paramDOCUMENT_URI $document_uri;
fastcgi_paramDOCUMENT_ROOT $document_root;
fastcgi_paramSERVER_PROTOCOL $server_protocol;
fastcgi_paramREMOTE_ADDR $remote_addr;
fastcgi_paramREMOTE_PORT $remote_port;
fastcgi_paramSERVER_ADDR $server_addr;
fastcgi_paramSERVER_PORT $server_port;
fastcgi_paramSERVER_NAME $server_name;
# service nginx start
安装配置memcached
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#解决依赖关系
# yum groupinstall "Development Tools" "Server Platform Deveopment" -y
#安装libevent
#memcached依赖于libevent API,因此要事先安装之
# tar xf libevent-2.0.22-stable.tar.gz
# cd libevent-2.0.22-stable
#./configure --prefix=/usr/local/libevent
# make && make install
# echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf
# ldconfig
#安装配置memcached
# tar xf memcached-1.4.24.tar.tar
# cd memcached-1.4.24
# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
# 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
# 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
# vim /etc/init.d/memcached
# chmod +x /etc/init.d/memcached
# chkconfig --add memcached
# service memcached start
安装配置php
1
2
3
4
5
6
7
8
9
10
11
12
#解决依赖关系
#yum groupinstall "Development tools" "Server Platform Development" -y
#yum -y groupinstall "Desktop Platform Development"
#yum -y install bzip2-devel libmcrypt-devel
# tar xf php-5.4.26.tar.bz2
# cd 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
# 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提供配置文件
# cp php.ini-production /etc/php.ini
#为php-fpm提供脚本
# cp sapi/fpm/init.d.php-fpm/etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on
#为php-fpm提供配置文件
# cd /usr/local/php
# cp etc/php-fpm.conf.default etc/php-fpm.conf
# 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
# tar xf xcache-3.1.0.tar.bz2
# cd xcache-3.1.0
# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php
/bin/php-config
# make && make install
# mkdir /etc/php.d
# cp xcache.ini /etc/php.d/
# vim /etc/php.d/xcache.ini
;; 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
# tar xf memcache-2.2.7.tgz
# cd memcache-2.2.7
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
# make && make install
# vim /etc/php.ini
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
# service php-fpm start
安装配置mariadb
1
2
3
4
5
6
7
8
# mkdir /mydata/data -pv
# groupadd -r mysql
# useradd -g mysql -r mysql
# chown -R mysql.mysql /mydata/data
# tar xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local
# ln -sv mariadb-5.5.36-linux-x86_64 mysql
# chown -R root.mysql mysql
提供配置及脚本文件
1
2
3
4
5
6
7
8
9
10
11
# mkdir /etc/mysql
# cd mysql
# cp /support-files/my-large.cnf /etc/mysql/my.cnf
# vim /etc/mysql/my.cnf
datadir = /mydata/data
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
初始化数据库
1
2
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# 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
# mkdir /www/bbs -pv
# unzip wordpress-3.2.1-zh_CN.zip
# cd wordpress
# mv * /www/bbs/
#在web和php上分别准备站点文件
#php节点
# cd /www/bbs
# cp wp-config-sample.php wp-config.php
# 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)
安装memadminMemAdmin是一款可视化的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
# tar xf memadmin-1.0.12.tar.gz -C /www/bbs/
#web和php端都需执行此操作
登陆后添加服务器
开始管理服务器
更多细节有兴趣可自行探索Ten endLNMMP架构实现Web动静分离实验就说到这里了,整个部署过程跟LAMP类似,朋友们部署过程遇到问题可留言交流,nginx在反向代理时还可将缓存缓存至memcached服务器,从而提高缓存性能,这里稍微一提,就不做详解了。以上仅为个人学习整理,如有错漏,大神勿喷~~~
页:
[1]