LNMMP:Memcache+LNMP

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

本次试验主要作用:

1、利用Nginx实现LNMP动静分离;

2、利用Memcache对php查询做缓存;

大致规划:

主机 IP 描述
nginx 192.168.0.111 Nginx作为代理服务器实现动静分离
php 192.168.0.112 处理动态请求
httpd 192.168.0.113 处理静态请求
mariadb 192.168.0.114 数据库存储
memcache 192.168.0.115 对php查询做缓存

一、安装配置Nginx

下载安装包:http://nginx.org/ 目前稳定版本是1.4最新

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
[root@node1 ~] # ls
anaconda - ks.cfg  install.log  install.log.syslog  nginx - 1.4 . 7   nginx - 1.4 . 7.tar .gz
[root@node1 ~] # cd nginx-1.4.7
[root@node1 nginx - 1.4 . 7 ] # ls
CHANGES  CHANGES.ru  LICENSE  Makefile  README  auto  conf  configure  contrib  html  man  objs  src
[root@node1 nginx - 1.4 . 7 ] # ./configure \
>    - - prefix = / usr \
>    - - 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 \     #注意这里用的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
[root@node1 nginx - 1.4 . 7 ] # make && make install
#这些编译安装过程如果有报错提示依赖其他安装包只需按照提示安装即可
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
#提供一个启动脚本
[root@node1 nginx - 1.4 . 7 ] # vim /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"
- / etc / sysconfig / nginx ] && .  / etc / sysconfig / nginx
lockfile = / var / lock / subsys / nginx
make_dirs() {
    # make required directories
    user = `nginx  - 2 >& 1  | grep  "configure arguments:"  | sed  's/[^*]*--user=\([^ ]*\).*/\1/g'  - `
    options = `$nginx  - 2 >& 1  | grep  'configure arguments:' `
    for  opt  in  $options; do
        if  [ `echo $opt | grep  '.*-temp-path' ` ]; then
            value = `echo $opt | cut  - "="  - 2 `
            if  [ !  - "$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  - - 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 nginx - 1.4 . 7 ] # service nginx start
Starting nginx:                                            [  OK  ]
[root@node1 nginx - 1.4 . 7 ] # ss -tunl | grep 80
tcp    LISTEN      0       128                     * : 80                     * : *

在node2上安装php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#安装php
[root@node2 ~] # tar xf php-5.4.26.tar.bz2
[root@node2 ~] # cd php-5.4.26
[root@node2 php - 5.4 . 26 ] # ls
acinclude.m4      install - sh           README.EXTENSIONS                 run - tests.php
aclocal.m4        LICENSE              README.EXT_SKEL                   sapi
build             ltmain.sh            README.GIT - RULES                  scripts
#php的安装在之前的LAMP中已有介绍;这里就不详细说明
#注意:这里的php监听的地址要改为自己的ip地址;不能使用本地地址
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
[root@node2 ~] # service fpmd start
Starting php - fpm  done
[root@node2 ~] # ss -tunl | grep 9000
tcp    LISTEN      0       128             192.168 . 0.112 : 9000                   * : *

详情参考:http://chenpipi.blog.51cto.com/8563610/1381835

二、配置Nginx整合到php

Nginx的模块和变量相当多;这里无法一一介绍清楚;直接给出官方连接;

详细可以查阅:http://nginx.org/en/docs/     http://wiki.nginx.org/Modules

配置Nginx:

node1配置:

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
[root@node1 html] # vim /etc/nginx/nginx.conf
#user  nobody;
worker_processes   4 ;    worker进程数
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
     worker_connections   1024 ;    每个进程处理请求数
}
http {
     include       mime.types;
     default_type  application / octet - stream;
     #proxy_cache_path   /cache/nginx levels=1:2 keys_zone=web:10m inactive=12h max_size=1g;
     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
     #                  '$status $body_bytes_sent "$http_referer" '
     #                  '"$http_user_agent" "$http_x_forwarded_for"';
     #access_log  logs/access.log  main;
     sendfile        on;
     #tcp_nopush     on;
     #keepalive_timeout  0;
     keepalive_timeout   5 ;        持久连接  5s
     #gzip  on;
     server {                配置虚拟服务器
         listen        80 ;
         root         / var / www / html;     #站点路径
         server_name  www.soul.com;
         #charset koi8-r;
         #access_log  logs/host.access.log  main;
         location  /  {             #属性,正常请求全部代理到node3上
             index  index.php index.html index.htm;
             proxy_pass  http: / / 192.168 . 0.113 ;
         }
         #error_page  404              /404.html;
         # redirect server error pages to the static page /50x.html
         #
         error_page    500  502  503  504   / 50x .html;
         location  =  / 50x .html {
             root   html;
         }
         location ~ \.php$ {             #这里就是整合php;以.php结尾的文件
             fastcgi_pass    192.168 . 0.112 : 9000 ;     #以fastcgi协议代理到node2的9000端口
             fastcgi_index  index.php;          #如果没有参数;则以index.php为参数
             fastcgi_param  SCRIPT_FILENAME   / scripts$fastcgi_script_name;         
             include        fastcgi_params;     #fastcgi的配置参数文件
         }
}

修改/etc/nginx/fastcgi_params,将其内容替换:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[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;

配置node3

1
2
3
4
5
[root@node3 ~] # rpm -q httpd
httpd - 2.2 . 15 - 29.el6 .centos.x86_64
[root@node3 ~] # service httpd start
Starting httpd:                                            [  OK  ]
[root@node3 ~] #

配置完成后;对nginx和fpmd进行重启并提供主页面进行测试;这里现在就不测试

三、配置memcache

下面开始配置memcach

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#在node5上直接yum安装memcached
# yum -y install memcached
[root@node5 ~] # rpm -ql memcached    安装完成查看生成的文件
/ etc / rc.d / init.d / memcached 
/ etc / sysconfig / memcached     #配置文件
/ usr / bin / memcached
/ usr / bin / memcached - tool 
[root@node5 ~] # vim /etc/sysconfig/memcached
PORT = "11211"
USER = "memcached"
MAXCONN = "1024"
CACHESIZE = "64"
OPTIONS = ""     #其实很简单;不需要任何配置;直接启动
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
[root@node5 ~] # ss -tunl | grep 11211
udp    UNCONN      0       0                       * : 11211                  * : *  
udp    UNCONN      0       0                      ::: 11211                 ::: *  
tcp    LISTEN      0       128                    ::: 11211                 ::: *  
tcp    LISTEN      0       128                     * : 11211                  * : *

在node2上安装php的memcache的扩展

1
2
3
4
5
6
7
8
9
10
11
12
[root@node2 ~] # rm -rf memcache-2.2.7
[root@node2 ~] # tar xf memcache-2.2.7.tgz
[root@node2 ~] # cd memcache-2.2.7
[root@node2 memcache - 2.2 . 7 ] # ls
config9.m4  CREDITS      memcache_consistent_hash.c  memcache_queue.c    memcache_standard_hash.c
config.m4   example.php  memcache.dsp                memcache_queue.h    php_memcache.h
config.w32  memcache.c   memcache.php                memcache_session.c  README
[root@node2 memcache - 2.2 . 7 ] # /usr/local/php/bin/phpize
[root@node2 memcache - 2.2 . 7 ] # ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
[root@node2 memcache - 2.2 . 7 ] # vim /etc/php.ini
extension  =  / usr / local / php / lib / php / extensions / no - debug - non - zts - 20100525 / memcache.so
#添加如上一行;注意上述路径是编译后生成的路径

提供测试文件测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#提供两个文件;一个html;一个php
#该文件放在node2,node3各一份;
[root@node2 ~] # vim /var/www/html/index.html  
<h1>This  is  test page< / h1>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
[root@node2 ~] # vim /var/www/html/index.php 
<?php     #这里测试memcache是否正常连接
$mem  =  new Memcache;
$mem - >connect( "192.168.0.115" 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." ;
?>
<?php
         phpinfo();         #php测试页面
?>

wKioL1Ncx-3BeBcXAAFOl56b9Is845.jpg

上述页面显示的memcache连是正常的;且也显示该网页是来自node2.soul.com这台主机的

测试正常访问

wKiom1NchAWTmT3sAADIhf5jVtM633.jpg

测试html页面正常返回。此处也给php安装了xcache;

wKiom1NcyGqRRHE2AAEWp5oKct8810.jpg

安装过程就不再赘述;前面的博客都有详解。

到此;前面的nginx配置已完成;下面配置mariadb。

四、安装mariadb

MariaDB的安装与mysql一样没有任何区别;只是名称不一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
anaconda - ks.cfg                                   install.log
drbd - 8.4 . 3 - 33.el6 .x86_64.rpm                      install.log.syslog
drbd - kmdl - 2.6 . 32 - 431.el6 - 8.4 . 3 - 33.el6 .x86_64.rpm  mariadb - 10.0 . 10 - linux - x86_64.tar.gz
[root@node4 ~] #
[root@node4 ~] # tar xf mariadb-10.0.10-linux-x86_64.tar.gz -C /usr/local/
#剩下步骤就是依次做个软连接到mysql
#更改权限;建议使用LVM作为存储
#存储目录权限也需要更改
#初始化
#复制启动脚本和配置文件;更改下配置文件;启动
[root@node4 ~] # mysql
Welcome to the MariaDB monitor.  Commands end with ;  or  \g.
Your MariaDB connection  id  is  5
Server version:  10.0 . 10 - MariaDB - log MariaDB Server
Copyright (c)  2000 2014 , Oracle, SkySQL Ab  and  others.
Type  'help;'  or  '\h'  for  help Type  '\c'  to clear the current  input  statement.
MariaDB [(none)]> use mysql
Database changed
MariaDB [mysql]> grant  all  privileges  on  * . *   to  'root' @ '192.168.0.%'  identified by  '123456' ;
MariaDB [mysql]> flush privileges;
#授权php主机可以连接
#到此mysql配置也结束

可以测试php是否可以连接mariadb

1
2
3
4
5
6
7
8
9
10
[root@node2 ~] # vim /var/www/html/index.php
#添加如下信息
<?php
         $link  =  mysql_connect( '192.168.0.114' , 'root' , '123456' );
         if ($link)
                 echo  "Success..." ;
         else
                 echo  "Failure..." ;
         mysql_close;
?>

访问测试

wKioL1Nczsaih1BtAAFgcazAB3Y134.jpg



五、安装论坛测试

在node2,node3上各放一个论坛程序

然后访问安装

wKiom1Nc6gKzzPYEAAFov6Bkuzc326.jpg

测试安装正常。现在来看下memcache缓存的状态;

1
2
3
4
[root@node2 ~] # cd /var/www/html/
[root@node2 html] # ls
Discuz_X3. 1_SC_UTF8 . zip   index.html  index.php  memadmin  readme  upload  utility
[root@node2 html] #

wKiom1Nc63jAFqniAAKXbHLdV-s885.jpg

查看memcache的详细信息;此处都有统计。

到此;memcache+lnmp已配置完成。



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

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
6月前
|
存储 缓存 监控
memcache的安装及使用详解
memcache的安装及使用详解
|
网络协议 Linux Memcache
linux安装memcache
linux安装memcache
82 1
linux安装memcache
|
Linux PHP Memcache
linux安装php memcache扩展
linux安装php memcache扩展
355 0
|
移动开发 应用服务中间件 PHP
|
安全 Memcache
|
关系型数据库 MySQL 应用服务中间件
|
算法 Memcache 网络协议
|
存储 测试技术 PHP
|
Web App开发 关系型数据库 应用服务中间件