Lnmmp

简介

LNMMP=Linux+Nginx+MySQL+Memcache+PHP;

利用Nginx的高性能特点做前端反向代理服务器,分发用户请求,并在能够利用缓存的地方使用Memcache缓存服务,以加速缓存效率,具体架构图如下;

具体部署

代理层:Nginx

编译安装Nginx


yum install gcc openssl-devel pcre-devel zlib-devel
groupadd -r nginx
useradd -r -g nginx -M nginx
./configure --prefix=/usr/local/nginx --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 --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
vi /etc/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

配置Nginx


upstream memcached {
server 172.16.25.111:11211;
keepalive 1024;
}
upstream webserver {
server 172.16.25.112:80;
server 172.16.25.113:80;
}
upstream phpserver {
server 172.16.25.112:9000;
server 172.16.25.113:9000;
}
server {
listen 80;
server_name xxrenzhe.lnmmp.com;
access_log /var/log/nginx/lnmmp.access.log;
error_log /var/log/nginx/lnmmp.errors.log notice;
root /www/lnmmp.com;
index index.php index.html;
location / {
set $memcached_key $request_uri;
add_header X-mem-key $memcached_key;
memcached_pass memcached;
default_type test/html;
error_page 404 500 502 504 = @webnocache;
}
location @webnocache {
rewrite ^(\/)?$ /index.php last; # 配置直接访问域名或IP地址时,重定向至index.php文件
rewrite ^/.*$ /set_mem.php?$request_uri last; # 将静态访问重定向至后端set_mem.php,以实现set memcache的功能
proxy_pass http://webserver;
}
location ~* \.php(\?.*)?$ {
fastcgi_pass phpserver;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

启动服务

chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
service nginx start

缓存层:Memcache+NFS

安装配置memcache


# 安装libevent
tar xf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21
./configure --prefix=/usr/local/libevent
make && make install
echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf
ldconfig
# 安装memcache
tar xf memcached-1.4.15.tar.gz
cd memcached-1.4.15
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
make && make install
# 编辑服务脚本 # 一次启动了2个memcache实例,一个用于Nginx,一个用于后端PHP服务
vi /etc/init.d/memcached
#!/bin/bash
#
# Init file for memcached
#
# chkconfig: - 86 14
# description: Distributed memory caching daemon
#
# processname: memcached
# config: /etc/sysconfig/memcached
. /etc/rc.d/init.d/functions
## Default variables
PORT1="11211"
PORT2="11311"
USER="nobody"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
RETVAL=0
prog="/usr/local/memcached/bin/memcached"
desc="Distributed memory caching"
lockfile1="/var/lock/subsys/memcached_$PORT1"
lockfile2="/var/lock/subsys/memcached_$PORT2"
start() {
echo -n $"Starting $desc (memcached): "
daemon $prog -d -p $PORT1 -u $USER -c $MAXCONN -m $CACHESIZE -o "$OPTIONS"
daemon $prog -d -p $PORT2 -u $USER -c $MAXCONN -m $CACHESIZE -o "$OPTIONS"
RETVAL=$?
[ $RETVAL -eq 0 ] && success && touch $lockfile1 && touch $lockfile2 || failure
echo
return $RETVAL
}
stop() {
echo -n $"Shutting down $desc (memcached): "
killproc $prog
RETVAL=$?
[ $RETVAL -eq 0 ] && success && rm -f $lockfile1 && rm -f $lockfile2 | failure
echo
return $RETVAL
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $desc ($prog): "
killproc $prog -HUP
RETVAL=$?
[ $RETVAL -eq 0 ] && success || failure
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e $lockfile1 ] && [ -e $lockfile1 ] && restart
RETVAL=$?
;;
reload)
reload
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL

启动memcache服务


# 安装libevent
tar xf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21
./configure --prefix=/usr/local/libevent
make && make install
echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf
ldconfig
# 安装memcache
tar xf memcached-1.4.15.tar.gz
cd memcached-1.4.15
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
make && make install
# 编辑服务脚本 # 一次启动了2个memcache实例,一个用于Nginx,一个用于后端PHP服务
vi /etc/init.d/memcached
#!/bin/bash
#
# Init file for memcached
#
# chkconfig: - 86 14
# description: Distributed memory caching daemon
#
# processname: memcached
# config: /etc/sysconfig/memcached
. /etc/rc.d/init.d/functions
## Default variables
PORT1="11211"
PORT2="11311"
USER="nobody"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
RETVAL=0
prog="/usr/local/memcached/bin/memcached"
desc="Distributed memory caching"
lockfile1="/var/lock/subsys/memcached_$PORT1"
lockfile2="/var/lock/subsys/memcached_$PORT2"
start() {
echo -n $"Starting $desc (memcached): "
daemon $prog -d -p $PORT1 -u $USER -c $MAXCONN -m $CACHESIZE -o "$OPTIONS"
daemon $prog -d -p $PORT2 -u $USER -c $MAXCONN -m $CACHESIZE -o "$OPTIONS"
RETVAL=$?
[ $RETVAL -eq 0 ] && success && touch $lockfile1 && touch $lockfile2 || failure
echo
return $RETVAL
}
stop() {
echo -n $"Shutting down $desc (memcached): "
killproc $prog
RETVAL=$?
[ $RETVAL -eq 0 ] && success && rm -f $lockfile1 && rm -f $lockfile2 | failure
echo
return $RETVAL
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $desc ($prog): "
killproc $prog -HUP
RETVAL=$?
[ $RETVAL -eq 0 ] && success || failure
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e $lockfile1 ] && [ -e $lockfile1 ] && restart
RETVAL=$?
;;
reload)
reload
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL

安装配置NFS


yum -y install nfs-utils
vi /etc/exports
/www/lnmmp.com 172.16.0.0/16(rw,no_root_squash)
exportfs -ar # 导出NFS共享目录
cd /www/lnmmp.com
unzip phpwind_v9.0_utf8.zip
mv phpwind_v9.0_utf8/upload/* .
chmod -R 777 attachment conf data html res src themes windid

Web层:Apache

Apache的安装见博客"httpd-2.4
编译安装
";

PHP-fpm的安装见博客“LAMP架构之PHP-FPM
服务器
”;

PHP加速器opcache的安装配置见博客“LAMP-PHP各加速器性能剖析”;

配置PHP支持memcache


tar xf memcache-2.2.5.tgz
cd memcache-2.2.5
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
make && make install
# 上述安装完后会有类似这样的提示:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
vi /usr/local/php/lib/php.ini # 配置memcache模块位置及php会话保存至memcache中
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
session.save_handler = memcache
session.save_path = "tcp://172.16.25.111:11311"
# 自定义set memcache的程序
vi /www/lnmmp.com/set_mem.php
<?php
$fn = dirname(__FILE__).$_SERVER[‘QUERY_STRING‘];
if (file_exists($fn)) {
$data = file_get_contents($fn);
$mem = new Memcache();
$mem->connect("172.16.25.111",11311) or die("Could not connect");
$mem->set($_SERVER[‘QUERY_STRING‘],$data,0,600) or die("Failed to save data at the memcached server");
header(‘Content-Length: ‘.filesize($fn)."\r\n");
header(‘Content-Type: text/html‘."\r\n");
header(‘X-cache: MISS‘."\r\n");
print "$data";
} else {
header(‘Location: http://xxrenzhe.lnmmp.com‘."\r\n");
header(‘Content-Length: ‘.filesize($fn)."\r\n");
header(‘X-cache: ‘.$fn."\r\n");
}
?>

Apache配置


vi /etc/httpd/httpd.conf
# DocumentRoot "/usr/local/apache/htdocs" # 注释掉此行
Include /etc/httpd/extra/httpd-vhosts.conf # 取消此行注释
vi /etc/httpd/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/www/lnmmp.com"
ServerName xxrenzhe.lnmmp.com
ErrorLog "logs/lnmmp.com-error_log"
CustomLog "logs/lnmmp.com-access_log"common
<Directory "/www/lnmmp.com">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

NFS目录挂载

mount -t nfs 172.16.25.111:/www/lnmmp.com /www/lnmmp.com

服务启动

service httpd start
service php-fpm start 

数据库层:Ameoba+MariaDB

MariaDB的安装详见博客“MySQL初识-架构-安装-初始化-连接-管理工具-数据文件”;

MariaDB的主从复制架构配置见博客“Maria10实现主从复制架构及SSL复制”;

Ameoba安装见“http://docs.hexnova.com/amoeba/index.html”;

Ameoba实现读写分离见“http://docs.hexnova.com/amoeba/rw-splitting.html

测试验证

Nginx利用memcache缓存小静态文件测试效果

Nginx利用memcache缓存静态文件测试效果

PHP利用memcache保存session数据测试效果

Lnmmp,布布扣,bubuko.com

时间: 2024-10-14 08:58:10

Lnmmp的相关文章

LNMMP平台搭建

拓扑 1) Nginx及的编译及安装 参考Nginx基本使用一文,同时在编译时一定要加入 2) 编译安装MariaDB #添加用户 useradd -r mysql #安装并提供配置文件和启动脚本 tar xvf mariadb-10.0.10-linux-x86_64.tar.gz -C /usr/local/ cd /usr/local/ ln -s mariadb-10.0.10-linux-x86_64/ mysql mkdir /etc/mysql scp support-files/

Web服务之LNMMP架构及动静分离实现

一.LNMMP LNMMP环境是Linux + Nginx + Memcached + MySQL + PhP,即LNMP + memcached. Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.Memcached基于一个存储键/值对的hashmap.其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通

LNMMP架构实现Web动静分离

前言 前面的文章中说过LAMP架构包括:Linux操作系统,Apache网站服务器,MySQL数据库,Perl.PHP或者Python编程语言,而今天要说的LNMMP 和LAMP类似,只是作为Web服务器的不再是Apache而是高性能的Nginx,同时引进Memcached加速缓存效率,用于加快访问速度. Memcached是一款开源.高性能.分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对数据库的访问来加速Web应用程序.它是一个基于内存的"键值对"存储,用于

高性能Web服务之lnmmp架构应用

Memcached是一款开源.高性能.分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对Database的访问来加速web应用程序.它是一个基于内存的"键值对"存储,用于存储数据库调用.API调用或页面引用结果的直接数据,如字符串.对象等. memcached是以LiveJournal旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件.现在已成为mixi.hatena.Facebook.Vox.LiveJournal等

Nginx结合memcached实现LNMMP平台搭建

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器.由俄罗斯的程序设计师Igor Sysoev所开发,其特点是占有内存少,并发能力强,它的主要功用中还有一项是作为缓存服务器,在某些场景下,如后端的服务器是数据库服务器时,Nginx与其他就需要配合其他专门用于缓存数据库中数据的软件结合起来可以更好的完成缓存数据的功能,比如memcached,此时的Nginx作为代理服务器使用. 实验:完成LNNMP平台

实现高可用LNMMP

实验平台:RHEL5.8 RHEL6.4 实验环境:node1 ---- node9 分别对应  10.32.9.51 ---- 10.32.9.59 实验步骤: 一.软件安装 node6  node7 使用rpm安装keepalived.x86_64 0:1.2.7 node6  node7 编译安装nginx-1.4.7 ./configure   --prefix=/usr   --sbin-path=/usr/sbin/nginx   --conf-path=/etc/nginx/ngi

Centos7下LNMMP环境搭建

实验环境: Centos7_64 实验目的:在Centos7下实现源码安装nginx+mariadb+memcache+php均为最新版本 --2015.10.10 一.解决依赖关系yum install -y openssl-devel pcre-devel cmake 二.安装nginx groupadd -r nginx useradd -r -g nginx nginx tar -zxvf nginx-1.9.5.tar.gz cd nginx-1.9.5 ./configure --p

实现LNMMP

实验平台:RHEL5.8  RHEL6.4 实验环境:node1 -- node9的内网IP分别对应10.32.9.51 -- 10.32.9.59 24位掩码           node6 node7对应一个虚拟外网IP 100.100.100.1/32 实现功能: 1.利用iscsi协议.集群文件系统gfs2.clvm集群逻辑卷.cman集群传输层协议为web服务器提供共享存储. 2.node1 node2 node3 提供wordpress个人博客服务 3.memcached缓存php应

Varnish详解与实战

一.简介: Varnish 是一款高性能且开源的反向代理服务器和 HTTP 加速器,其采用全新的软件体系机构,和现在的硬件体系紧密配合,与传统的 squid 相比,varnish 具有稳定,且效率更高,资源占用更少等优点. Varnish的官网为https://www.varnish-cache.org,rpm,rpm包的下载位置为:http://repo.varnish-cache.org. 二.varnish结构特点: 1.varnish结构 三大部分,客户端,varnish处理,varni