LNMMP:Memcache+LNMP

本次试验主要作用:

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最新

[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"
[ -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
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
#给个执行权限即可启动测试
[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

#安装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配置:

[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,将其内容替换:

[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

[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

#在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的扩展

[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
#添加如上一行;注意上述路径是编译后生成的路径

提供测试文件测试:

#提供两个文件;一个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一样没有任何区别;只是名称不一样

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

[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缓存的状态;

[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已配置完成。




如有错误;恳请指正!

你可能感兴趣的:(nginx,memcache,lnmmp)