一、前提说明
1、实验目的
实现动静分享的LNMMP网站
2、实现功能
(1)前端nginx服务器(172.16.7.10)处理静态内容并向后转发动态内容
(2)php服务器(172.16.7.100)处理动态内容
(3)数据库为MariaDB,版本为mariadb-10.0.10,IP:172.16.7.200
(4)在动态系统中,为了减小数据库的压力,提高性能,增加memcached服务器(172.16.7.201)
3、实验拓扑
简单实验拓扑如下(因为都在一个网络内测试,所以前端nginx只给了一个IP):

二、安装nginx服务器(172.16.7.10)
1、解决依赖关系
在安装前,为了避免一些不必要的麻烦,首先装几个开发包组:"Development Tools"、"Server Platform Development"、并安装"pcre-devel包"
2、添加nginx用户,实现以nginx用户运行nginx服务进程
3、编译安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
--prefix= /usr/local/nginx \
--sbin-path= /usr/local/nginx/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= /var/tmp/nginx/client/ \
--http-proxy-temp-path= /var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path= /var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path= /var/tmp/nginx/uwsgi \
--http-scgi-temp-path= /var/tmp/nginx/scgi \
--with-pcre
|
|
4、为nginx提供SysV风格的脚本
新建文件/etc/rc.d/init.d/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
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
|
#!/bin/sh
. /etc/rc .d /init .d /functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0
nginx= "/usr/local/nginx/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() {
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
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
|
5、为服务脚本赋予执行权限
6、把nginx服务添加至服务管理器,并让其开机自动启动
7、启动服务并测试
1
2
3
|
[root@nmshuishui ~]
LISTEN 0 128 *:80 *:* users :(( "nginx" ,4724,6),( "nginx" ,4726,6))
|
|
三、安装MariaDB(172.16.7.200)
1、二进制安装包
1
|
mariadb-10.0.10-linux-x86_64. tar .gz
|
|
2、安装步骤请参考我在LAMP博文中的链接
http://nmshuishui.blog.51cto.com/1850554/1381822
四、安装php服务器(172.16.7.100)
1、安装nginx服务器
同上第二步,安装完后测试一下
2、安装php服务器
(1)解决依赖关系
事先已安装开发包组"Desktop Platform Development"
1
|
yum -y install libmcrypt libmcrypt-devel mhash mhash-devel mcrypt
|
|
(2)编译安装php-5.4.4
安装完上面那几个还有报错,根据提示又安装了这几个
1
|
[root@shuishui php-5.4.4]
|
|
接下来再次编译安装
1
|
[root@shuishui php-5.4.4]
|
(3)安装
1
|
[root@shuishui php-5.4.4]
|
|
(4)为php提供配置文件
1
|
[root@shuishui php-5.4.4]
|
|
(5)为php-fpm提供Sysv脚本,并将其添加到服务列表
1
2
3
4
|
[root@shuishui php-5.4.4]
[root@shuishui php-5.4.4]
[root@shuishui php-5.4.4]
[root@shuishui php-5.4.4]
|
|
(6)为php-fpm提供配置文件
1
|
[root@shuishui php-5.4.4]
|
(7)编辑php-fpm配置文件,配置fpm的相关选项,并启用pid
1
2
3
4
5
6
7
|
[root@shuishui ~]
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /usr/local/php/var/run/php-fpm .pid
listen = 172.16.7.100:9000
|
|
(8)启动php-fpm并验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@shuishui ~]
Starting php-fpm . done
[root@shuishui ~]
Warning: bad syntax, perhaps a bogus '-' ? See /usr/share/doc/procps-3 .2.8 /FAQ
root 60344 2.0 0.4 99684 4880 ? Ss 01:28 0:00 php-fpm: master process ( /usr/local/php/etc/php-fpm .conf)
nobody 60347 0.0 0.3 99684 3732 ? S 01:28 0:00 php-fpm: pool www
nobody 60348 0.0 0.3 99684 3732 ? S 01:28 0:00 php-fpm: pool www
nobody 60349 0.0 0.3 99684 3732 ? S 01:28 0:00 php-fpm: pool www
nobody 60350 0.0 0.3 99684 3732 ? S 01:28 0:00 php-fpm: pool www
nobody 60351 0.0 0.3 99684 3736 ? S 01:28 0:00 php-fpm: pool www
nobody 60352 0.0 0.3 99684 3736 ? S 01:28 0:00 php-fpm: pool www
nobody 60353 0.0 0.3 99684 3736 ? S 01:28 0:00 php-fpm: pool www
nobody 60354 0.0 0.3 99684 3736 ? S 01:28 0:00 php-fpm: pool www
root 60356 0.0 0.0 103244 856 pts /1 S+ 01:28 0:00 grep php-fpm
|
|
五、安装xcache,为php加速(172.16.7.100)
1、编译安装xcache
1
2
3
4
5
6
7
8
9
10
|
[root@shuishui ~]
[root@shuishui ~]
[root@shuishui xcache-3.0.1]
php php-cgi php-config phpize
[root@shuishui xcache-3.0.1]
Configuring for :
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@shuishui xcache-3.0.1]
|
结束后,会出现如下行
1
2
3
4
|
----------------------------------------------------------------------
Build complete.
Don 't forget to run ' make test '.
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
|
2、编辑php.ini,整合php和xcache
(1)首先将xcache提供的样例配置导入php.ini
1
2
|
[root@shuishui xcache-3.0.1]
[root@shuishui xcache-3.0.1]
|
|
(2)接下来修改/etc/php.d/xcache.ini,找到extension开头的行,修改为如下行:
1
|
extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache .so
|
|
3、重启php-fpm
1
2
3
|
[root@shuishui xcache-3.0.1]
Gracefully shutting down php-fpm . done
Starting php-fpm done
|
|
六、整合nginx和php5
1、编辑/etc/nginx/nginx.conf(172.16.7.10)
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
|
worker_processes 2;
error_log /var/log/nginx/error .log notice;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application /octet-stream ;
sendfile on;
keepalive_timeout 5;
gzip on;
server {
listen 80;
server_name www.shuishui.com;
add_header X-via $server_addr;
location / {
root html;
index index.php index.html index.htm;
}
location ~* \.(jpg|jpeg|png|gif|js|css)$ {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 172.16.7.100:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME scripts$fastcgi_script_name;
include fastcgi_params;
}
}
}
|
|
2、编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:(172.16.7.10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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;
|
|
3、重新载入nginx
1
2
3
4
|
[root@nmshuishui html]
nginx: the configuration file /etc/nginx/nginx .conf syntax is ok
nginx: configuration file /etc/nginx/nginx .conf test is successful
Reloading nginx: [ OK ]
|
|
4、动、静分离测试
前提:因为没有DNS服务器,所以事先要把www.shuishui.com所对应的IP:172.16.7.10写入到hosts文件中
(1)动态测试:在php服务器上的网页目录中创建index.php(172.16.7.100)
1
2
3
4
5
6
7
|
[root@shuishui html]
/usr/local/nginx/html
[root@shuishui html]
<h1>Welcome to php server(172.16.7.100)< /h1 >
<?php
phpinfo();
?>
|
|
在windows主机上访问www.shuishui.com

(2)静态测试:在前端nginx服务器(172.16.7.10)的网页目录中放入1.png图片
1
2
3
4
|
[root@nmshuishui html]
/usr/local/nginx/html
[root@nmshuishui html]
1.png 50x.html index.html index.html1 index.php
|
|
再到windows上去测试一下

由上面的动态、静态测试结果可看出,根据请求的资源类型的不同,即可实现动、静分离。因此,也知道了,网页目录需要准备两份,前端nginx服务器和后端的php服务器各需一份
七、安装Memcache服务器(172.16.7.201)
1、memcached简介
Memcached是一款开源、高性能、分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对Database的访问来加速web应用程序。它是一个基于内存的“键值对”存储,用于存储数据库调用、API调用或页面引用结果的直接数据,如字符串、对象等。
Memcached是一款开发工具,它既不是一个代码加速器,也不是数据库中间件。其设计哲学思想主要反映在如下方面:
(1)简单key/value存储:服务器不关心数据本身的意义及结构,只要是可序列化数据即可。存储项由“键、过期时间、可选的标志及数据”四个部分组成;
(2)功能的实现一半依赖于客户端,一半基于服务器端:客户负责发送存储项至服务器端、从服务端获取数据以及无法连接至服务器时采用相应的动作;服务端负责接收、存储数据,并负责数据项的超时过期;
(3)各服务器间彼此无视:不在服务器间进行数据同步;
(4)O(1)的执行效率
(5)清理超期数据:默认情况下,Memcached是一个LRU缓存,同时,它按事先预订的时长清理超期数据;但事实上,memcached不会删除任何已缓存数据,只是在其过期之后不再为客户所见;而且,memcached也不会真正按期限清理缓存,而仅是当get命令到达时检查其时长;
2、安装memcached服务器(172.16.7.201)
也可以使用yum方式安装,这里使用编译安装
(1)安装libevent
memcached依赖于libevent API,因此要事先安装
(2)安装配置memcached
(3)为memcached提供sysv脚本
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
|
#!/bin/bash
. /etc/rc .d /init .d /functions
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
|
|
(4)配置memcached成为系统服务
(5)查看memcached监听端口是否成功
1
2
3
|
[root@shuishui ~]
LISTEN 0 128 :::11211 :::* users :(( "memcached" ,3120,27))
LISTEN 0 128 *:11211 *:* users :(( "memcached" ,3120,26))
|
|
八、安装php的memcache扩展(172.16.7.100)
1、安装php的memcache扩展
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@shuishui ~]
[root@shuishui ~]
[root@shuishui memcache-2.2.7]
Configuring for :
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@shuishui memcache-2.2.7]
……
……
[root@shuishui memcache-2.2.7]
……
……
Build complete.
Don 't forget to run ' make test '.
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
|
2、编辑/etc/php.ini,在“动态模块”相关的位置添加如下一行来载入memcache扩展:
1
|
extension= /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache .so
|
|
3、重启php-fpm
1
2
3
|
[root@shuishui ~]
Gracefully shutting down php-fpm . done
Starting php-fpm done
|
|
4、在windows客户端验证memcached是否安装成功

刚才的x-cache也安装成功了
5、对memcached功能进行测试,在网站目录中建立测试页面test.php,添加如下内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@shuishui html]
/usr/local/nginx/html
[root@shuishui html]
<?php
$mem = new Memcache;
$mem->connect( "172.16.7.201" , 11211) or die( "Could not connect" );
$version = $mem->getVersion();
echo "Server's version: " .$version. "<br/>\n" ;
$mem-> set ( 'hellokey' , 'Hello World' , 0, 600) or die( "Failed to save data at the memcached server" );
echo "Store data in the cache (data will expire in 600 seconds)<br/>\n" ;
$get_result = $mem->get( 'hellokey' );
echo "$get_result is from memcached server." ;
?>
~
|
在windows服务器上测试,表明memcache已经能够正常工作

由上图可以看出,memcached(172.16.7.201)已经可以正常工作了,到这里,基于动、静分离的LNMMP就已经实现了;下一步就是安装一个博客系统并监控其性能
九、安装wordpress和数据库授权(前端nginx和后端的php都需要安装wordpress)
1、在mysql数据库(172.16.7.200)中授权访问网段
1
2
3
4
5
|
MariaDB [(none)]> grant all on *.* to 'wordpress' @ '172.16.%.%' identified by 'wordpress' ;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
|
|
2、创建wordpress数据库,否则不让安装
1
|
MariaDB [(none)]> create database wordpress;
|
|
3、wordpress安装准备
1
2
3
4
5
6
7
|
[root@nmshuishui ~]
[root@nmshuishui html]
[root@nmshuishui html]
[root@nmshuishui wordpress]
[root@nmshuishui wordpress]
[root@nmshuishui wordpress]
|
|
修改如下几项即可

4、博客系统安装成功

十、安装memadmin-master查看memcached的状态信息
1、解压(前端nginx和后端的php都需要解压安装memadmin-master)
2、连接memadmin-master

3、查看memcached状态信息

本文转自 nmshuishui 51CTO博客,原文链接:http://blog.51cto.com/nmshuishui/1403720,如需转载请自行联系原作者