设为首页 收藏本站
查看: 1544|回复: 0

LEMP架构实现

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-5-6 11:01:50 | 显示全部楼层 |阅读模式
本实验用到的架构图,如下所示:

QQ截图20140506105834.png
简单介绍本次实验架构的数据流向:
首先client向A服务器发起网页请求,A接到请求,首先查看memcached是否有请求的内容,如果有就返回给client,如果memcached中没有,则A查询B服务器中client的请求响应缓存到memcached中一份,同时再响应给客户端,如果在一定时长内,client再次发起的同样的请求,A服务器直接将缓存响应给client,简单理解。
环境安装
A服务器安装memcached
yum install -y memcached
查看安装后的内容为:


[iyunv@node5 ~]# rpm -ql memcached
/etc/rc.d/init.d/memcached  ##sysV风格服务启动脚本
/etc/sysconfig/memcached   ##软件默认配置文件
/usr/bin/memcached    ##可执行文件(后面简单介绍使用方法)
/usr/bin/memcached-tool ##可执行文件(后面简单介绍使用方法)
/usr/share/doc/memcached-1.4.4
/usr/share/doc/memcached-1.4.4/AUTHORS
/usr/share/doc/memcached-1.4.4/CONTRIBUTORS
/usr/share/doc/memcached-1.4.4/COPYING
/usr/share/doc/memcached-1.4.4/ChangeLog
/usr/share/doc/memcached-1.4.4/NEWS
/usr/share/doc/memcached-1.4.4/README
/usr/share/doc/memcached-1.4.4/protocol.txt
/usr/share/doc/memcached-1.4.4/readme.txt
/usr/share/doc/memcached-1.4.4/threads.txt
/usr/share/man/man1/memcached.1.gz
/var/run/memcached  #运行时信息存放目录,pid文件
查看/etc/sysconfig/memcached


[iyunv@node5 ~]# cat /etc/sysconfig/memcached
PORT="11211" #memcache默认端口
USER="memcached" #默认用户
MAXCONN="1024" #最大缓存的链接数,通常要此参数修改
CACHESIZE="64" #缓存所占用的内存空间,单位为Mb,通常要修改
OPTIONS=""
B服务器的软件安装
   nginx软件安装具体版本为nginx-1.4.7  


# groupadd -r nginx
# useradd -r -g nginx nginx
# ./configure \
  --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
# make && make install
    增加nginx启动脚本


#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

后续操作


# chmod +x /etc/rc.d/init.d/nginx  #加执行权限
# chkconfig --add nginx #加到服务列表
# chkconfig nginx on  #开机自启
# service nginx start 测试启动
安装php-fpm,让php工作于(简单)服务器模式,具体版本php-5.4.26


#yum groupinstall -y "Development Tools" "Server Platform Developments" "Server Platform Development"  #解决php安装过程中的包依赖
#tar -xf php-5.4.26.tar.bz2
#cd php-5.4.26
#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm  --with-mysqli=/usr/local/mysql/bin/mysql_config --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

提供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提供的配置文件
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
调整php-fpm相关的参数,如下所示:
#cd /usr/local/php/etc
##############最修改的结果如下所示#################
[iyunv@node3 etc]# grep -v '^$' php-fpm.conf |grep -v '^;'|grep -v '^[[:space:]].*'
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /var/log/php-fpm.log
[www]
user = nobody
group = nobody
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 128
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 5
pm.max_requests = 500
pm.status_path = /status
ping.path = /ping
ping.response = pong
rlimit_files = 10240
启动服务
# service php-fpm start
nginx与php5的整合
   编辑/etc/nginx/nginx.conf,启用如下选项
       只显示修改后的内容
方式


[iyunv@node3 nginx]# grep -v '^[[:space:]].*#' nginx.conf |grep -v '^$'
worker_processes  1;  #默认启动的进程线
events {
    worker_connections  1024;#连接并发数
}
http {
    include       mime.types; #支持的文件类型
    default_type  application/octet-stream;
    sendfile        on; #支持直接从内核空间返回响应给客户
    keepalive_timeout  65; #长连接时长
    fastcgi_cache_path /tmp levels=1:2 keys_zone=fcgi:50m max_size=1g inactive=12h;
    ##缓存文件存放路径,目录层次(目录结构),定义键值域名称 最大缓存空间1G,非活动时间12小时
    server {
        listen       80; #监听端口
        server_name  www.mytest.com; #虚拟主机
        location / {   URL上下言语
            root   /mydata/htdocshare; #htdoc在本地系统的路径
            index  index.php index.html index.htm; 默认首页索引文件
        }
        error_page   500 502 503 504  /50x.html; #不同出错代码所做出的网页跳转后显示的页面
        location = /50x.html { #定义具体的出错页面路径
            root   html;
        }
        location ~* /(status|ping) { #完全匹配字符
            root           /mydata/htdocshare; #匹配后跳转路径及处理方式
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_param  SCRIPT_FILENAME  /$fastcgi_script_name;
            auth_basic "admin area"; #认证提示框信息
            auth_basic_user_file /etc/nginx/.htpswd; #引用存放用户名称的文件
        include        fastcgi_params; #引用此文件中变量,此文件内容可以简单理解为nginx中的变量与fastcgi中的变量的一一对应关系(名称),从nginx传递过来的变量,fastcgi可以理解并认识
    }
        location ~ \.php$ { #用户访问php页面时所做出的处理方式,转给fastcgit程序处理
            root           /mydata/htdocshare;
            fastcgi_pass   127.0.0.1:9000; #php-fpm监听的路径与端口
            fastcgi_index  index.php; #索引页
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

    }
}
   编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容,此文件作用就是fastcgi中的变量与nginx中的变量能够相互转换。



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;
安装xcache为PH加速


# 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


安装完出现类似如下行:
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20100525/


首先将xcache提供的样例配置导入php.ini
#
# cp /software/xcache-3.1.0/xcache.ini /etc/php.d
接下来编辑xcache.ini 文件增加以下内容
zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
MariaDB安装


#useradd -r mysql
#mkdir /mydata/data -pv
#chown -R mysql.mysql /mydata/data
#tar -xf mariadb-10.0.10-linux-x86_64.tar.gz -C /usr/local
#cd /usr/local
#ln -sv mariadb-10.0.10-linux-x86_64 mysql
#cd mysql
#chown -R root.mysql ./*
#scripts/mysql_install_db --datadir=/mydata/data --user=mysql
# cd /usr/local/mysql
# cp support-files/my-large.cnf  /etc/my.cnf
##编辑my.cnf,增加以下内容
innodb_file_per_table=on
datadir = /mydata/data
为mysql增加服务启动脚本
# cd /usr/local/mysql
# cp support-files/mysql.server  /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
启动mysql
service mysqld start

新建测试页面来测试缓存服务器是否起作用


<?php
$mem = new Memcache;
$mem->connect("172.16.251.24", 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 s from memcached server.";      

?>


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-18758-1-1.html 上篇帖子: Ubuntu 搭建LAMP环境 下篇帖子: LAMP架构之NFS
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表