LEMP架构实现
本实验用到的架构图,如下所示:简单介绍本次实验架构的数据流向:首先client向A服务器发起网页请求,A接到请求,首先查看memcached是否有请求的内容,如果有就返回给client,如果memcached中没有,则A查询B服务器中client的请求响应缓存到memcached中一份,同时再响应给客户端,如果在一定时长内,client再次发起的同样的请求,A服务器直接将缓存响应给client,简单理解。
环境安装A服务器安装memcachedyum install -y memcached查看安装后的内容为:
# 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
# cat /etc/sysconfig/memcachedPORT="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 2esac
后续操作
# 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##############最修改的结果如下所示################## grep -v '^$' php-fpm.conf |grep -v '^;'|grep -v '^[[:space:]].*'pid = /usr/local/php/var/run/php-fpm.piderror_log = /var/log/php-fpm.loguser = nobodygroup = nobodylisten = 127.0.0.1:9000pm = dynamicpm.max_children = 128pm.start_servers = 5pm.min_spare_servers = 3pm.max_spare_servers = 5pm.max_requests = 500pm.status_path = /statusping.path = /pingping.response = pongrlimit_files = 10240启动服务# service php-fpm startnginx与php5的整合 编辑/etc/nginx/nginx.conf,启用如下选项 只显示修改后的内容方式
# grep -v '^[[:space:]].*#' nginx.conf |grep -v '^$'worker_processes1;#默认启动的进程线events { worker_connections1024;#连接并发数}http { include mime.types; #支持的文件类型 default_typeapplication/octet-stream; sendfile on; #支持直接从内核空间返回响应给客户 keepalive_timeout65; #长连接时长 fastcgi_cache_path /tmp levels=1:2 keys_zone=fcgi:50m max_size=1g inactive=12h; ##缓存文件存放路径,目录层次(目录结构),定义键值域名称 最大缓存空间1G,非活动时间12小时 server { listen 80; #监听端口 server_namewww.mytest.com; #虚拟主机 location / { URL上下言语 root /mydata/htdocshare; #htdoc在本地系统的路径 indexindex.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_paramSCRIPT_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_indexindex.php; #索引页 fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name; include fastcgi_params; }
}} 编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容,此文件作用就是fastcgi中的变量与nginx中的变量能够相互转换。
fastcgi_paramGATEWAY_INTERFACECGI/1.1;fastcgi_paramSERVER_SOFTWARE nginx;fastcgi_paramQUERY_STRING $query_string;fastcgi_paramREQUEST_METHOD $request_method;fastcgi_paramCONTENT_TYPE $content_type;fastcgi_paramCONTENT_LENGTH $content_length;fastcgi_paramSCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_paramSCRIPT_NAME $fastcgi_script_name;fastcgi_paramREQUEST_URI $request_uri;fastcgi_paramDOCUMENT_URI $document_uri;fastcgi_paramDOCUMENT_ROOT $document_root;fastcgi_paramSERVER_PROTOCOL $server_protocol;fastcgi_paramREMOTE_ADDR $remote_addr;fastcgi_paramREMOTE_PORT $remote_port;fastcgi_paramSERVER_ADDR $server_addr;fastcgi_paramSERVER_PORT $server_port;fastcgi_paramSERVER_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.soMariaDB安装
#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=ondatadir = /mydata/data为mysql增加服务启动脚本# cd /usr/local/mysql# cp support-files/mysql.server/etc/rc.d/init.d/mysqld# chkconfig --add mysqld# chkconfig mysqld on启动mysqlservice 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]