天天看点

实现动静分离的LNMMP网站架构

一、前提说明

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):

<a href="http://s3.51cto.com/wyfs02/M00/25/3F/wKioL1NcY8bDtMeFAABQWYDe_KU977.png" target="_blank"></a>

二、安装nginx服务器(172.16.7.10)

1、解决依赖关系

   在安装前,为了避免一些不必要的麻烦,首先装几个开发包组:"Development Tools"、"Server Platform Development"、并安装"pcre-devel包"

2、添加nginx用户,实现以nginx用户运行nginx服务进程

1

2

<code># groupadd -r nginx</code>

<code># useradd -r -g nginx nginx</code>

3、编译安装

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<code># ./configure \</code>

<code>  </code><code>--prefix=</code><code>/usr/local/nginx</code> <code>\                   </code><code>#指定安装路径</code>

<code>  </code><code>--sbin-path=</code><code>/usr/local/nginx/sbin/nginx</code> <code>\     </code><code>#指定nginx程序路径</code>

<code>  </code><code>--conf-path=</code><code>/etc/nginx/nginx</code><code>.conf \           </code><code>#指定配置文件路径</code>

<code>  </code><code>--error-log-path=</code><code>/var/log/nginx/error</code><code>.log \   </code><code>#指定错误日志路径</code>

<code>  </code><code>--http-log-path=</code><code>/var/log/nginx/access</code><code>.log \   </code><code>#指定访问日志路径</code>

<code>  </code><code>--pid-path=</code><code>/var/run/nginx/nginx</code><code>.pid  \        </code><code>#指定pid文件路径</code>

<code>  </code><code>--lock-path=</code><code>/var/lock/nginx</code><code>.lock \            </code><code>#指定锁文件路径</code>

<code>  </code><code>--user=nginx \                                </code><code>#指定以nginx用户运行进程</code>

<code>  </code><code>--group=nginx \</code>

<code>  </code><code>--with-http_ssl_module \                      </code><code>#添加ssl模块</code>

<code>  </code><code>--with-http_flv_module \                      </code><code>#添加流媒体模块</code>

<code>  </code><code>--with-http_stub_status_module \              </code><code>#添加服务器状态信息模块</code>

<code>  </code><code>--with-http_gzip_static_module \              </code><code>#添加gzip压缩模块</code>

<code>  </code><code>--http-client-body-temp-path=</code><code>/var/tmp/nginx/client/</code> <code>\    </code><code>#指定客户端请求时的主体临时目录</code>

<code>  </code><code>--http-proxy-temp-path=</code><code>/var/tmp/nginx/proxy/</code> <code>\           </code><code>#指定做代理服务器时的临时目录</code>

<code>  </code><code>--http-fastcgi-temp-path=</code><code>/var/tmp/nginx/fcgi/</code> <code>\          </code><code>#指定fcgi临时目录</code>

<code>  </code><code>--http-uwsgi-temp-path=</code><code>/var/tmp/nginx/uwsgi</code> <code>\            </code><code>#指定uwsgi临时目录(反向代理python开发的页面)</code>

<code>  </code><code>--http-scgi-temp-path=</code><code>/var/tmp/nginx/scgi</code> <code>\              </code><code>#另一种反向代理用户请求的协议</code>

<code>  </code><code>--with-pcre                                              </code><code>#指定pcre</code>

<code># make &amp;&amp; make install            #安装</code>

4、为nginx提供SysV风格的脚本

   新建文件/etc/rc.d/init.d/nginx

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

<code>#!/bin/sh</code>

<code>#</code>

<code># nginx - this script starts and stops the nginx daemon</code>

<code># chkconfig:   - 85 15</code>

<code># description:  Nginx is an HTTP(S) server, HTTP(S) reverse \</code>

<code>#               proxy and IMAP/POP3 proxy server</code>

<code># processname: nginx</code>

<code># config:      /etc/nginx/nginx.conf</code>

<code># config:      /etc/sysconfig/nginx</code>

<code># pidfile:     /var/run/nginx.pid</code>

<code>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           </code> 

<code># Source function library.</code>

<code>. </code><code>/etc/rc</code><code>.d</code><code>/init</code><code>.d</code><code>/functions</code>

<code># Source networking configuration.</code>

<code>. </code><code>/etc/sysconfig/network</code>

<code># Check that networking is up.</code>

<code>[ </code><code>"$NETWORKING"</code> <code>= </code><code>"no"</code> <code>] &amp;&amp; </code><code>exit</code> <code>0</code>

<code>nginx=</code><code>"/usr/local/nginx/sbin/nginx"</code>

<code>prog=$(</code><code>basename</code> <code>$nginx)</code>

<code>NGINX_CONF_FILE=</code><code>"/etc/nginx/nginx.conf"</code>

<code>[ -f </code><code>/etc/sysconfig/nginx</code> <code>] &amp;&amp; . </code><code>/etc/sysconfig/nginx</code>

<code>lockfile=</code><code>/var/lock/subsys/nginx</code>

<code>make_dirs() {</code>

<code>   </code><code># make required directories</code>

<code>   </code><code>user=`nginx -V 2&gt;&amp;1 | </code><code>grep</code> <code>"configure arguments:"</code> <code>| </code><code>sed</code> <code>'s/[^*]*--user=\([^ ]*\).*/\1/g'</code> <code>-`</code>

<code>   </code><code>options=`$nginx -V 2&gt;&amp;1 | </code><code>grep</code> <code>'configure arguments:'</code><code>`</code>

<code>   </code><code>for</code> <code>opt </code><code>in</code> <code>$options; </code><code>do</code>

<code>       </code><code>if</code> <code>[ `</code><code>echo</code> <code>$opt | </code><code>grep</code> <code>'.*-temp-path'</code><code>` ]; </code><code>then</code>

<code>           </code><code>value=`</code><code>echo</code> <code>$opt | </code><code>cut</code> <code>-d </code><code>"="</code> <code>-f 2`</code>

<code>           </code><code>if</code> <code>[ ! -d </code><code>"$value"</code> <code>]; </code><code>then</code>

<code>               </code><code># echo "creating" $value</code>

<code>               </code><code>mkdir</code> <code>-p $value &amp;&amp; </code><code>chown</code> <code>-R $user $value</code>

<code>           </code><code>fi</code>

<code>       </code><code>fi</code>

<code>   </code><code>done</code>

<code>}</code>

<code>start() {</code>

<code>    </code><code>[ -x $nginx ] || </code><code>exit</code> <code>5</code>

<code>    </code><code>[ -f $NGINX_CONF_FILE ] || </code><code>exit</code> <code>6</code>

<code>    </code><code>make_dirs</code>

<code>    </code><code>echo</code> <code>-n $</code><code>"Starting $prog: "</code>

<code>    </code><code>daemon $nginx -c $NGINX_CONF_FILE</code>

<code>    </code><code>retval=$?</code>

<code>    </code><code>echo</code>

<code>    </code><code>[ $retval -</code><code>eq</code> <code>0 ] &amp;&amp; </code><code>touch</code> <code>$lockfile</code>

<code>    </code><code>return</code> <code>$retval</code>

<code>stop() {</code>

<code>    </code><code>echo</code> <code>-n $</code><code>"Stopping $prog: "</code>

<code>    </code><code>killproc $prog -QUIT</code>

<code>    </code><code>[ $retval -</code><code>eq</code> <code>0 ] &amp;&amp; </code><code>rm</code> <code>-f $lockfile</code>

<code>restart() {</code>

<code>    </code><code>configtest || </code><code>return</code> <code>$?</code>

<code>    </code><code>stop</code>

<code>    </code><code>sleep</code> <code>1</code>

<code>    </code><code>start</code>

<code>reload() {</code>

<code>    </code><code>echo</code> <code>-n $</code><code>"Reloading $prog: "</code>

<code>    </code><code>killproc $nginx -HUP</code>

<code>    </code><code>RETVAL=$?</code>

<code>force_reload() {</code>

<code>    </code><code>restart</code>

<code>configtest() {</code>

<code>  </code><code>$nginx -t -c $NGINX_CONF_FILE</code>

<code>rh_status() {</code>

<code>    </code><code>status $prog</code>

<code>rh_status_q() {</code>

<code>    </code><code>rh_status &gt;</code><code>/dev/null</code> <code>2&gt;&amp;1</code>

<code>case</code> <code>"$1"</code> <code>in</code>

<code>    </code><code>start)</code>

<code>        </code><code>rh_status_q &amp;&amp; </code><code>exit</code> <code>0</code>

<code>        </code><code>$1</code>

<code>        </code><code>;;</code>

<code>    </code><code>stop)</code>

<code>        </code><code>rh_status_q || </code><code>exit</code> <code>0</code>

<code>    </code><code>restart|configtest)</code>

<code>    </code><code>reload)</code>

<code>        </code><code>rh_status_q || </code><code>exit</code> <code>7</code>

<code>    </code><code>force-reload)</code>

<code>        </code><code>force_reload</code>

<code>    </code><code>status)</code>

<code>        </code><code>rh_status</code>

<code>    </code><code>condrestart|try-restart)</code>

<code>            </code><code>;;</code>

<code>    </code><code>*)</code>

<code>        </code><code>echo</code> <code>$</code><code>"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"</code>

<code>        </code><code>exit</code> <code>2</code>

<code>esac</code>

5、为服务脚本赋予执行权限

<code># chmod +x /etc/rc.d/init.d/nginx</code>

6、把nginx服务添加至服务管理器,并让其开机自动启动

<code># chkconfig --add nginx</code>

<code># chkconfig nginx on</code>

7、启动服务并测试

<code># service nginx start</code>

<code>[root@nmshuishui ~]</code><code># ss -antlp | grep nginx</code>

<code>LISTEN     0      128                       *:80                       *:*      </code><code>users</code><code>:((</code><code>"nginx"</code><code>,4724,6),(</code><code>"nginx"</code><code>,4726,6))</code>

三、安装MariaDB(172.16.7.200)

1、二进制安装包

<code>mariadb-10.0.10-linux-x86_64.</code><code>tar</code><code>.gz</code>

2、安装步骤请参考我在LAMP博文中的链接

<a href="http://nmshuishui.blog.51cto.com/1850554/1381822" target="_blank">   http://nmshuishui.blog.51cto.com/1850554/1381822</a>

四、安装php服务器(172.16.7.100)

1、安装nginx服务器

   同上第二步,安装完后测试一下

2、安装php服务器

(1)解决依赖关系

   事先已安装开发包组"Desktop Platform Development"

<code>yum -y </code><code>install</code> <code>libmcrypt libmcrypt-devel mhash mhash-devel mcrypt</code>

(2)编译安装php-5.4.4

   安装完上面那几个还有报错,根据提示又安装了这几个

<code>[root@shuishui php-5.4.4]</code><code># yum -y install libcurl-devel bzip2-devel libmcrypt-devel</code>

   接下来再次编译安装

<code>[root@shuishui php-5.4.4]</code><code>#  ./configure --prefix=/usr/local/php --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml  --with-mhash --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd</code>

(3)安装

<code>[root@shuishui php-5.4.4]</code><code># make &amp;&amp; make install</code>

(4)为php提供配置文件

<code>[root@shuishui php-5.4.4]</code><code># cp php.ini-production /etc/php.ini</code>

(5)为php-fpm提供Sysv脚本,并将其添加到服务列表

<code>[root@shuishui php-5.4.4]</code><code># cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm</code>

<code>[root@shuishui php-5.4.4]</code><code># chmod +x /etc/rc.d/init.d/php-fpm</code>

<code>[root@shuishui php-5.4.4]</code><code># chkconfig --add php-fpm</code>

<code>[root@shuishui php-5.4.4]</code><code># chkconfig php-fpm on</code>

(6)为php-fpm提供配置文件

<code>[root@shuishui php-5.4.4]</code><code># cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf</code>

(7)编辑php-fpm配置文件,配置fpm的相关选项,并启用pid

<code>[root@shuishui ~]</code><code># vim /usr/local/php/etc/php-fpm.conf</code>

<code>pm.max_children = 150</code>

<code>pm.start_servers = 8</code>

<code>pm.min_spare_servers = 5</code>

<code>pm.max_spare_servers = 10</code>

<code>pid = </code><code>/usr/local/php/var/run/php-fpm</code><code>.pid</code>

<code>listen = 172.16.7.100:9000</code>

(8)启动php-fpm并验证

<code>[root@shuishui ~]</code><code># service php-fpm start</code>

<code>Starting php-fpm . </code><code>done</code>

<code>[root@shuishui ~]</code><code># ps -aux | grep php-fpm</code>

<code>Warning: bad syntax, perhaps a bogus </code><code>'-'</code><code>? See </code><code>/usr/share/doc/procps-3</code><code>.2.8</code><code>/FAQ</code>

<code>root     60344  2.0  0.4  99684  4880 ?        Ss   01:28   0:00 php-fpm: master process (</code><code>/usr/local/php/etc/php-fpm</code><code>.conf)        </code>

<code>nobody   60347  0.0  0.3  99684  3732 ?        S    01:28   0:00 php-fpm: pool www                                                </code>

<code>nobody   60348  0.0  0.3  99684  3732 ?        S    01:28   0:00 php-fpm: pool www                                                </code>

<code>nobody   60349  0.0  0.3  99684  3732 ?        S    01:28   0:00 php-fpm: pool www                                                </code>

<code>nobody   60350  0.0  0.3  99684  3732 ?        S    01:28   0:00 php-fpm: pool www                                                </code>

<code>nobody   60351  0.0  0.3  99684  3736 ?        S    01:28   0:00 php-fpm: pool www                                                </code>

<code>nobody   60352  0.0  0.3  99684  3736 ?        S    01:28   0:00 php-fpm: pool www                                                </code>

<code>nobody   60353  0.0  0.3  99684  3736 ?        S    01:28   0:00 php-fpm: pool www                                                </code>

<code>nobody   60354  0.0  0.3  99684  3736 ?        S    01:28   0:00 php-fpm: pool www                                                </code>

<code>root     60356  0.0  0.0 103244   856 pts</code><code>/1</code>    <code>S+   01:28   0:00 </code><code>grep</code> <code>php-fpm</code>

五、安装xcache,为php加速(172.16.7.100)

1、编译安装xcache

<code>[root@shuishui ~]</code><code># tar xf xcache-3.0.1.tar.bz2</code>

<code>[root@shuishui ~]</code><code># cd xcache-3.0.1</code>

<code>[root@shuishui xcache-3.0.1]</code><code># /usr/local/php/bin/php</code>

<code>php         php-cgi     php-config  phpize</code>

<code>[root@shuishui xcache-3.0.1]</code><code># /usr/local/php/bin/phpize</code>

<code>Configuring </code><code>for</code><code>:</code>

<code>PHP Api Version:         20100412</code>

<code>Zend Module Api No:      20100525</code>

<code>Zend Extension Api No:   220100525</code>

<code>[root@shuishui xcache-3.0.1]</code><code># ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config</code>

结束后,会出现如下行

<code>----------------------------------------------------------------------</code>

<code>Build complete.</code>

<code>Don</code><code>'t forget to run '</code><code>make</code> <code>test</code><code>'.</code>

<code>Installing shared extensions:     </code><code>/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/</code>

2、编辑php.ini,整合php和xcache

(1)首先将xcache提供的样例配置导入php.ini

<code>[root@shuishui xcache-3.0.1]</code><code># mkdir /etc/php.d</code>

<code>[root@shuishui xcache-3.0.1]</code><code># cp xcache.ini /etc/php.d/  #xcache.ini文件在xcache的源码目录中</code>

(2)接下来修改/etc/php.d/xcache.ini,找到extension开头的行,修改为如下行:

<code>extension = </code><code>/usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache</code><code>.so</code>

3、重启php-fpm

<code>[root@shuishui xcache-3.0.1]</code><code># service php-fpm restart</code>

<code>Gracefully shutting down php-fpm . </code><code>done</code>

<code>Starting php-fpm  </code><code>done</code>

六、整合nginx和php5

1、编辑/etc/nginx/nginx.conf(172.16.7.10)

<code>worker_processes  2; </code><code>#worker进程的个数</code>

<code>error_log  </code><code>/var/log/nginx/error</code><code>.log  notice;    </code><code>#错误日志路径及级别</code>

<code>events {</code>

<code>    </code><code>worker_connections  1024;    </code><code>#每个worker能够并发响应的最大请求数</code>

<code>http {</code>

<code>    </code><code>include       mime.types;    </code><code>#支持多媒体类型</code>

<code>    </code><code>default_type  application</code><code>/octet-stream</code><code>;</code>

<code>    </code><code>sendfile        on;    </code><code>#由内核直接转发</code>

<code>    </code><code>#keepalive_timeout  0;</code>

<code>    </code><code>keepalive_timeout  5;    </code><code>#持久连接5s</code>

<code>    </code><code>gzip</code>  <code>on;    </code><code>#开启压缩功能</code>

<code>    </code><code>server {</code>

<code>        </code><code>listen       80;</code>

<code>        </code><code>server_name  www.shuishui.com;</code>

<code>        </code><code>add_header X-via $server_addr;    </code><code>#让客户端能够看到代理服务器的IP</code>

<code>        </code><code>location / {</code>

<code>            </code><code>root   html;</code>

<code>            </code><code>index  index.php index.html index.htm;</code>

<code>        </code><code>}</code>

<code>        </code><code>location ~* \.(jpg|jpeg|png|gif|js|css)$ {    </code><code>#匹配以括号中结尾的格式</code>

<code>            </code><code>root   html;    </code><code>#默认目录在/usr/local/nginx/html</code>

<code>        </code><code>location ~ \.php$ {</code>

<code>            </code><code>root                html;</code>

<code>            </code><code>fastcgi_pass        172.16.7.100:9000;    </code><code>#代理到的服务器</code>

<code>            </code><code>fastcgi_index       index.php;</code>

<code>            </code><code>fastcgi_param       SCRIPT_FILENAME scripts$fastcgi_script_name;</code>

<code>            </code><code>include             fastcgi_params;</code>

<code>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     </code> 

<code>    </code><code>}</code>

2、编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:(172.16.7.10)

<code>fastcgi_param  GATEWAY_INTERFACE  CGI</code><code>/1</code><code>.1;</code>

<code>fastcgi_param  SERVER_SOFTWARE    nginx;</code>

<code>fastcgi_param  QUERY_STRING       $query_string;</code>

<code>fastcgi_param  REQUEST_METHOD     $request_method;</code>

<code>fastcgi_param  CONTENT_TYPE       $content_type;</code>

<code>fastcgi_param  CONTENT_LENGTH     $content_length;</code>

<code>fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;</code>

<code>fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;</code>

<code>fastcgi_param  REQUEST_URI        $request_uri;</code>

<code>fastcgi_param  DOCUMENT_URI       $document_uri;</code>

<code>fastcgi_param  DOCUMENT_ROOT      $document_root;</code>

<code>fastcgi_param  SERVER_PROTOCOL    $server_protocol;</code>

<code>fastcgi_param  REMOTE_ADDR        $remote_addr;</code>

<code>fastcgi_param  REMOTE_PORT        $remote_port;</code>

<code>fastcgi_param  SERVER_ADDR        $server_addr;</code>

<code>fastcgi_param  SERVER_PORT        $server_port;</code>

<code>fastcgi_param  SERVER_NAME        $server_name;</code>

3、重新载入nginx

<code>[root@nmshuishui html]</code><code># service nginx reload</code>

<code>nginx: the configuration </code><code>file</code> <code>/etc/nginx/nginx</code><code>.conf syntax is ok</code>

<code>nginx: configuration </code><code>file</code> <code>/etc/nginx/nginx</code><code>.conf </code><code>test</code> <code>is successful</code>

<code>Reloading nginx:                                           [  OK  ]</code>

4、动、静分离测试

   前提:因为没有DNS服务器,所以事先要把www.shuishui.com所对应的IP:172.16.7.10写入到hosts文件中

(1)动态测试:在php服务器上的网页目录中创建index.php(172.16.7.100)

<code>[root@shuishui html]</code><code># pwd</code>

<code>/usr/local/nginx/html</code>

<code>[root@shuishui html]</code><code># vim index.php</code>

<code>&lt;h1&gt;Welcome to php server(172.16.7.100)&lt;</code><code>/h1</code><code>&gt;</code>

<code>&lt;?php</code>

<code>        </code><code>phpinfo();</code>

<code>?&gt;</code>

<a href="http://s3.51cto.com/wyfs02/M01/25/41/wKioL1Ncv3_wOqAPAAECnEwqRXs864.png" target="_blank"></a>

(2)静态测试:在前端nginx服务器(172.16.7.10)的网页目录中放入1.png图片

<code>[root@nmshuishui html]</code><code># pwd</code>

<code>[root@nmshuishui html]</code><code># ls</code>

<code>1.png  50x.html  index.html  index.html1  index.php</code>

再到windows上去测试一下

<a href="http://s3.51cto.com/wyfs02/M01/25/41/wKiom1NcwFiCSuZLAAzGMWfm5SU285.png" target="_blank"></a>

由上面的动态、静态测试结果可看出,根据请求的资源类型的不同,即可实现动、静分离。因此,也知道了,网页目录需要准备两份,前端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,因此要事先安装

<code># yum groupinstall "Development Tools" "Server Platform Deveopment" -y</code>

<code>#yum install -y libevent-devel</code>

(2)安装配置memcached

<code># tar xf memcached-1.4.15.tar.gz</code>

<code># cd memcached-1.4.15</code>

<code># ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent</code>

<code># make &amp;&amp; make install</code>

(3)为memcached提供sysv脚本

<code>#!/bin/bash</code>

<code># Init file for memcached</code>

<code># chkconfig: - 86 14</code>

<code># description: Distributed memory caching daemon</code>

<code># processname: memcached</code>

<code># config: /etc/sysconfig/memcached</code>

<code>## Default variables</code>

<code>PORT=</code><code>"11211"</code>

<code>USER=</code><code>"nobody"</code>

<code>MAXCONN=</code><code>"1024"</code>

<code>CACHESIZE=</code><code>"64"</code>

<code>OPTIONS=</code><code>""</code>

<code>RETVAL=0</code>

<code>prog=</code><code>"/usr/local/memcached/bin/memcached"</code>

<code>desc=</code><code>"Distributed memory caching"</code>

<code>lockfile=</code><code>"/var/lock/subsys/memcached"</code>

<code>        </code><code>echo</code> <code>-n $</code><code>"Starting $desc (memcached): "</code>

<code>        </code><code>daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE -o </code><code>"$OPTIONS"</code>

<code>        </code><code>RETVAL=$?</code>

<code>        </code><code>[ $RETVAL -</code><code>eq</code> <code>0 ] &amp;&amp; success &amp;&amp; </code><code>touch</code> <code>$lockfile || failure</code>

<code>        </code><code>echo</code>

<code>        </code><code>return</code> <code>$RETVAL</code>

<code>        </code><code>echo</code> <code>-n $</code><code>"Shutting down $desc (memcached): "</code>

<code>        </code><code>killproc $prog</code>

<code>        </code><code>[ $RETVAL -</code><code>eq</code> <code>0 ] &amp;&amp; success &amp;&amp; </code><code>rm</code> <code>-f $lockfile || failure</code>

<code>        </code><code>stop</code>

<code>        </code><code>start</code>

<code>        </code><code>echo</code> <code>-n $</code><code>"Reloading $desc ($prog): "</code>

<code>        </code><code>killproc $prog -HUP</code>

<code>        </code><code>[ $RETVAL -</code><code>eq</code> <code>0 ] &amp;&amp; success || failure</code>

<code>  </code><code>start)</code>

<code>  </code><code>stop)</code>

<code>  </code><code>restart)</code>

<code>        </code><code>restart</code>

<code>  </code><code>condrestart)</code>

<code>        </code><code>[ -e $lockfile ] &amp;&amp; restart</code>

<code>  </code><code>reload)</code>

<code>        </code><code>reload</code>

<code>  </code><code>status)</code>

<code>        </code><code>status $prog</code>

<code>   </code><code>*)</code>

<code>        </code><code>echo</code> <code>$</code><code>"Usage: $0 {start|stop|restart|condrestart|status}"</code>

<code>        </code><code>RETVAL=1</code>

<code>exit</code> <code>$RETVAL</code>

(4)配置memcached成为系统服务

<code># chmod +x /etc/init.d/memcached</code>

<code># chkconfig --add memcached</code>

<code># service memcached start</code>

(5)查看memcached监听端口是否成功

<code>[root@shuishui ~]</code><code># ss -tnlp | grep 11211</code>

<code>LISTEN     0      128                      :::11211                   :::*      </code><code>users</code><code>:((</code><code>"memcached"</code><code>,3120,27))</code>

<code>LISTEN     0      128                       *:11211                    *:*      </code><code>users</code><code>:((</code><code>"memcached"</code><code>,3120,26))</code>

八、安装php的memcache扩展(172.16.7.100)

1、安装php的memcache扩展

<code>[root@shuishui ~]</code><code># tar xf memcache-2.2.7.tgz</code>

<code>[root@shuishui ~]</code><code># cd memcache-2.2.7</code>

<code>[root@shuishui memcache-2.2.7]</code><code># /usr/local/php/bin/phpize</code>

<code>[root@shuishui memcache-2.2.7]</code><code># ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache</code>

<code>    </code><code>……</code>

<code>[root@shuishui memcache-2.2.7]</code><code># make &amp;&amp; make install</code>

2、编辑/etc/php.ini,在“动态模块”相关的位置添加如下一行来载入memcache扩展:

<code>extension=</code><code>/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache</code><code>.so</code>

<code>[root@shuishui ~]</code><code># service php-fpm restart</code>

4、在windows客户端验证memcached是否安装成功

<a href="http://s3.51cto.com/wyfs02/M02/25/42/wKiom1Nc2PGQicAIAACKPpThYO0512.png" target="_blank"></a>

刚才的x-cache也安装成功了

5、对memcached功能进行测试,在网站目录中建立测试页面test.php,添加如下内容

<code>[root@shuishui html]</code><code># pwd    #php服务器(172.16.7.100)</code>

<code>[root@shuishui html]</code><code># vim test.php</code>

<code>$mem = new Memcache;</code>

<code>$mem-&gt;connect(</code><code>"172.16.7.201"</code><code>, 11211)  or die(</code><code>"Could not connect"</code><code>);</code>

<code>$version = $mem-&gt;getVersion();</code>

<code>echo</code> <code>"Server's version: "</code><code>.$version.</code><code>"&lt;br/&gt;\n"</code><code>;</code>

<code>$mem-&gt;</code><code>set</code><code>(</code><code>'hellokey'</code><code>, </code><code>'Hello World'</code><code>, 0, 600) or die(</code><code>"Failed to save data at the memcached server"</code><code>);</code>

<code>echo</code> <code>"Store data in the cache (data will expire in 600 seconds)&lt;br/&gt;\n"</code><code>;</code>

<code>$get_result = $mem-&gt;get(</code><code>'hellokey'</code><code>);</code>

<code>echo</code> <code>"$get_result is from memcached server."</code><code>;</code>

<code>~</code>

在windows服务器上测试,表明memcache已经能够正常工作

<a href="http://s3.51cto.com/wyfs02/M00/25/42/wKioL1Nc3-XAv81VAABW7likxFE673.png" target="_blank"></a>

由上图可以看出,memcached(172.16.7.201)已经可以正常工作了,到这里,基于动、静分离的LNMMP就已经实现了;下一步就是安装一个博客系统并监控其性能

九、安装wordpress和数据库授权(前端nginx和后端的php都需要安装wordpress)

1、在mysql数据库(172.16.7.200)中授权访问网段

<code>MariaDB [(none)]&gt; grant all on *.* to </code><code>'wordpress'</code><code>@</code><code>'172.16.%.%'</code> <code>identified by </code><code>'wordpress'</code><code>;</code>

<code>Query OK, 0 rows affected (0.00 sec)</code>

<code>MariaDB [(none)]&gt;</code>

<code>MariaDB [(none)]&gt; flush privileges;</code>

2、创建wordpress数据库,否则不让安装

<code>MariaDB [(none)]&gt; create database wordpress;</code>

3、wordpress安装准备

<code>[root@nmshuishui ~]</code><code># cd /usr/local/nginx/html/</code>

<code>[root@nmshuishui html]</code><code># unzip wordpress-3.2.1-zh_CN.zip</code>

<code>[root@nmshuishui html]</code><code># cd wordpress</code>

<code>[root@nmshuishui wordpress]</code><code># cp wp-config-sample.php wp-config.php</code>

<code>[root@nmshuishui wordpress]</code><code># vim wp-config.php  #需要在这里填入所需的数据库信息,这个还和Discuz不一样</code>

<code>[root@nmshuishui wordpress]</code><code># scp wp-config.php [email protected]:/usr/local/nginx/html/wordpress</code>

<code>#前端nginx和后端的php都需要安装wordpress,所以配置文件一保持一致</code>

修改如下几项即可

<a href="http://s3.51cto.com/wyfs02/M00/25/43/wKiom1NdEmSQI4YIAABTBpVE_jo515.png" target="_blank"></a>

4、博客系统安装成功

<a href="http://s3.51cto.com/wyfs02/M02/25/44/wKioL1NdEmTRAG9xAAcTTN1NrVY336.png" target="_blank"></a>

十、安装memadmin-master查看memcached的状态信息

1、解压(前端nginx和后端的php都需要解压安装memadmin-master)

<code>[root@shuishui html]</code><code># unzip memadmin-master.zip</code>

2、连接memadmin-master

<a href="http://s3.51cto.com/wyfs02/M01/25/43/wKiom1NdEuSBYL_FAACdA2CHhlE638.png" target="_blank"></a>

3、查看memcached状态信息

<a href="http://s3.51cto.com/wyfs02/M01/25/43/wKiom1NdEv_jaz08AADXpXL1Sq8689.png" target="_blank"></a>

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