当前位置: > Linux服务器 > 环境配置 >

LNMMP架构及动静分离实现

时间:2016-07-14 13:02来源:linux.it.net.cn 作者:IT
目录
LNMMP  
工程拓扑
安装nginx服务器
安装MySQL服务器
安装php服务器
安装php加速器xcache
配置nginx
安装Memcached服务器
安装php的memcached扩展
安装memadmin-master查看memcached状态信息

 

1 LNMMP

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

2工程拓扑

Web服务之LNMMP架构及动静分离实现,by 5lulu.com

 

3 安装nginx服务器

  1. 1)部署开发环境
  2. # yum -y install “Development tools” “Server Platform Development”
  3. 2)解决依赖 pcre-devel openssl-devel
  4. # yum -y install pcre-devel openssl-devel
  5. 3) 设置用户
  6. # groupadd -r nginx
  7. # useradd -r -g nginx nginx

如果是通过yum安装nginx,可以忽略下面

  1. 4)编译安装nginx-1.4.7
  2. # tar xf nginx-1.4.7.tar.gz
  3. # cd nginx-1.4.7
  4. # ./configure –prefix=/usr –sbin-path=/usr/sbin/nginx
  5. –conf-path=/etc/nginx/nginx.conf –error-log-path= /var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log
  6. –pid-path=/var/run/nginx/nginx.pid –lock-path=/var/lock/nginx.lock –user=nginx –group=nginx –with-http_ssl_module
  7. –with-http_flv_module –with-http_stub_status_module
  8. –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/
  9. –http-uwsgi-temp-path=/var/tmp/nginx/uwsgi –http-scgi-temp-path=
  10. /var/tmp/nginx/scgi –with-pcre
  11. # make && make install
  12. 5)检测配置文件语法
  13. # /usr/sbin/nginx -t
  14. 6) 提供启动脚本
  15. # vim /etc/rc.d/init.d/nginx
  16. 内容如下
  17. # nginx – this script starts and stops the nginx daemon
  18. #
  19. # chkconfig: – 85 15
  20. # description: Nginx is an HTTP(S) server, HTTP(S) reverse
  21. # proxy and IMAP/POP3 proxy server
  22. # processname: nginx
  23. # config: /etc/nginx/nginx.conf
  24. # config: /etc/sysconfig/nginx
  25. # pidfile: /var/run/nginx.pid
  26. # Source function library.
  27. . /etc/rc.d/init.d/functions
  28. # Source networking configuration.
  29. . /etc/sysconfig/network
  30. # Check that networking is up.
  31. [ “$NETWORKING” = “no” ] && exit 0
  32. nginx=”/usr/sbin/nginx”
  33. prog=$(basename $nginx)
  34. NGINX_CONF_FILE=”/etc/nginx/nginx.conf”
  35. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  36. lockfile=/var/lock/subsys/nginx
  37. make_dirs() {
  38. # make required directories
  39. user=`nginx -V 2>&1 | grep “configure arguments:” | sed ‘s/[^*]*–user=([^ ]*).*/1/g’ -`
  40. options=`$nginx -V 2>&1 | grep ‘configure arguments:’`
  41. for opt in $options; do
  42. if [ `echo $opt | grep ‘.*-temp-path’` ]; then
  43. value=`echo $opt | cut -d “=” -f 2`
  44. if [ ! -d “$value” ]; then
  45. # echo “creating” $value
  46. mkdir -p $value && chown -R $user $value
  47. fi
  48. fi
  49. done
  50. }
  51. start() {
  52. [ -x $nginx ] || exit 5
  53. [ -f $NGINX_CONF_FILE ] || exit 6
  54. make_dirs
  55. echo -n $”Starting $prog: “
  56. daemon $nginx -c $NGINX_CONF_FILE
  57. retval=$?
  58. echo
  59. [ $retval -eq 0 ] && touch $lockfile
  60. return $retval
  61. }
  62. stop() {
  63. echo -n $”Stopping $prog: “
  64. killproc $prog -QUIT
  65. retval=$?
  66. echo
  67. [ $retval -eq 0 ] && rm -f $lockfile
  68. return $retval
  69. }
  70. restart() {
  71. configtest || return $?
  72. stop
  73. sleep 1
  74. start
  75. }
  76. reload() {
  77. configtest || return $?
  78. echo -n $”Reloading $prog: “
  79. killproc $nginx -HUP
  80. RETVAL=$?
  81. echo
  82. }
  83. force_reload() {
  84. restart
  85. }
  86. configtest() {
  87. $nginx -t -c $NGINX_CONF_FILE
  88. }
  89. rh_status() {
  90. status $prog
  91. }
  92. rh_status_q() {
  93. rh_status >/dev/null 2>&1
  94. }
  95. case “$1” in
  96. start)
  97. rh_status_q && exit 0
  98. $1
  99. ;;
  100. stop)
  101. rh_status_q || exit 0
  102. $1
  103. ;;
  104. restart|configtest)
  105. $1
  106. ;;
  107. reload)
  108. rh_status_q || exit 7
  109. $1
  110. ;;
  111. force-reload)
  112. force_reload
  113. ;;
  114. status)
  115. rh_status
  116. ;;
  117. condrestart|try-restart)
  118. rh_status_q || exit 0
  119. ;;
  120. *)
  121. echo $”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”
  122. exit 2
  123. esac
  124. 7)为服务脚本赋予执行权限
  125. # chmod +x /etc/rc.d/init.d/nginx
  126. 8)添加到系统服务并开机启动
  127. # chkconfig –add nginx
  128. # chkconfig nginx on
  129. # chkconfig –list nigx
  130. 9) 设置nginx配置文件的语法高亮
  131. # mkdir ./vim/syntax -pv
  132. # cd .vim/syntax
  133. # wget http://www.vim.org/scripts/download_script.php?src_id=19394
  134. # cd .vim
  135. # vim filetype.vim 内容如下
  136. au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/* if &ft == ” | setfiletype nginx | endif
  137. 10)启动服务
  138. # service nginx start
  139. # ss -tnalp | grep nginx

 

4 安装MySQL服务器

1.安装

  1. # tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local
  2. # ln -sv /usr/local/mysql-5.5.33-linux2.6-x86_64 mysql 创建软连接,易于操作

2.为数据库创建数据目录

  1. #mkdri -pv /mydata/data

3.新建用户以安全方式运行进程

  1. #groupadd -r mysql //创建系统组mysql
  2. #useradd -r -s /sbin/nologin -g mysql mysql -M -D /mydata/data mysql
  3. //创建系统用户mysql
  4. #chown -R mysql:mysql /mydata/data
  5. //设置目录属主属组

4.初始化mysql

  1. # cd /usr/local/mysql
  2. # scripts/mysql_install_db –datadir=/mydata/data –user=mysql
  3. //初始化数据库
  4. # chown -R root .
  5. //设置当前目录所有文件属主为root

5.提供脚本

  1. #cd /usr/local/mysql
  2. #cp support-files/mysql.server /etc/rc.d/init.d/mysqld
  3. //设置脚本mysqld
  4. #chmod +x /etc/rc.d/init.d/mysqld
  5. //给脚本执行权限
  6. # chkconfig –add mysqld
  7. //添加开机启动
  8. # chkconfig mysqld on

6.提供配文件

  1. #cd /usr/local/mysql
  2. #cp support-files/my-large.cnf /etc/my.cnf
  3. #vim /etc/my.cnf
  4. thread_concurrency = 2
  5. //修改,并发线程数,bithread_concurrency的值为CPU个数乘以2
  6. datadir = /mydata/data
  7. #添加,mysql数据文件的存放路径:

7.其他配置

  1. # vim /etc/profile.d/mysqld.sh
  2. exportPATH=/usr/local/mysql/bin:$PATH
  3. # source /etc/profile.d/mysqld.sh
  4. #vim /etc/man.config
  5. MANPATH /usr/local/mysql/man//添加此行
  6. # ln -sv /usr/local/mysql/include /usr/include/mysql
  7. //输出mysql的头文件至系统头文件路径/usr/include
  8. # echo ‘/usr/local/mysql/lib’ > /etc/ld.so.conf.d/mysql.conf
  9. //输出mysql的库文件给系统库
  10. #ldconfig //重载系统库:

8.启动服务

  1. # service mysqld start
  2. # ss -tnl | grep 3306

9.用户初始化

  1. #mysql
  2. mysql> use mysql
  3. mysql> selecthost,user,password from user;
  4. mysql> DELETE FROM user WHERE user = ”; //删除空用户
  5. mysql> DELETE FROM user WHERE user = ‘::1’; //删除ipv6用户
  6. mysql> UPDATE user SET password = PASSWORD(‘Hoolee’) WHERE password = ”;
  7. //为root用户设置密码
  8. mysql> FLUSH PRIVILEGES;

 

5安装php服务器

1.解决开发环境和依赖关系

  1. # yum -y install bzip2-devel
  2. # yum -y install libmcrypt-devel
  3. # yum -y groupinstall “Desktop Platform Development”

2.安装php

  1. # tar xf php-5.4.26.tar.bz2
  2. # cd /usr/src/php-5.4.26/
  3. # ./configure–prefix=/usr/local/php –with-openssl
  4. –with-mysql=mysqlnd –with-mysqli=mysqlnd –with-pdo-mysql=mysqlnd
  5. –enable-mbstring –with-freetype-dir –with-jpeg-dir
  6. –with-png-dir –with-zlib–with-libxml-dir=/usr –enable-xml
  7. –enable-sockets –with-apxs2=/usr/local/apache2/bin/apxs
  8. –with-mcrypt –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d
  9. –with-bz2 –enable-maintainer-zts
  10. # make && make install

3.提供配置文件

  1. # cp php.ini-production /etc/php.ini

4.为php-fpm提供脚本,并将其添加到服务列表

  1. # cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
  2. # chmod +x /etc/rc.d/init.d/php-fpm
  3. # chkconfig –add php-fpm
  4. # chkconfig php-fpm on

5.为php-fpm提供配置文件

  1. # cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

6.编辑php-fpm配置文件

  1. # vim /usr/local/php/etc/php-fpm.conf
  2. pm.max_children = 150
  3. pm.start_servers = 8
  4. pm.min_spare_servers = 5
  5. pm.max_spare_servers = 10
  6. pid = /usr/local/php/var/run/php-fpm.pid
  7. listen = 172.16.1.11:9000

7.启动php-fpm

  1. # service php-fpm start
  2. # ps -aux | grep php-fpm

 

6安装php加速器xcache

1.安装xcache

  1. # tar xf xcache-3.0.3.tar.gz
  2. # cd xcache-3.0.3
  3. # /usr/local/php/bin/phpize
  4. //phpize是用来安装php扩展模块的,通过phpize可以建立php的
  5. 外挂模块,若你想在原来编译好的php中加入memcached或者
  6. ImageMagick等扩展模块,就需要使用phpize
  7. # # ./configure –enable=xcache –with-php-config=/usr/local/php/bin/
  8. php-config
  9. # make && make install
  10. //显示xcache模块路径:/usr/local/php/lib/php/extensions/no-debug-zts-20100525/

2.编辑配置文件,整合php + xcache

  1. # vim xcache.ini
  2. //添加模块路径
  3. extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
  4. # cp xcache.ini /etc/php.d/

3.重启php-fpm

  1. # service php-fpm restart

 

7配置nginx

1.编辑/etc/nginx/nginx.conf,实现动静分离

  1. worker_processes 2; #worker进程的个数
  2. error_log /var/log/nginx/error.log notice; #错误日志路径及级别
  3. events {
  4. worker_connections 1024; #每个worker能够并发响应的最大请求数
  5. }
  6. http {
  7. include mime.types; #支持多媒体类型
  8. default_type application/octet-stream;
  9. sendfile on; #由内核直接转发
  10. #keepalive_timeout 0;
  11. keepalive_timeout 5; #持久连接5s
  12. gzip on; #开启压缩功能
  13. server {
  14. listen 80;
  15. server_name www.hoo.com;
  16. add_header X-via $server_addr; #让客户端能够看到代理服务器的IP
  17. location / {
  18. root html;
  19. index index.php index.html index.htm;
  20. }
  21. location ~* .(jpg|jpeg|png|gif|js|css)$ { #匹配静态内容
  22. root html; #默认目录在/usr/local/nginx/html
  23. }
  24. location ~ .php$ { #匹配动态php文件
  25. root html;
  26. fastcgi_pass 172.16.7.11:9000; #代理到的服务器
  27. fastcgi_index index.php;
  28. fastcgi_param SCRIPT_FILENAME scripts$fastcgi_script_name;
  29. include fastcgi_params;
  30. }
  31. }
  32. }

2.编辑/etc/nginx/fastcgi_params

  1. fastcgi_param GATEWAY_INTERFACE CGI/1.1;
  2. fastcgi_param SERVER_SOFTWARE nginx;
  3. fastcgi_param QUERY_STRING $query_string;
  4. fastcgi_param REQUEST_METHOD $request_method;
  5. fastcgi_param CONTENT_TYPE $content_type;
  6. fastcgi_param CONTENT_LENGTH $content_length;
  7. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  8. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  9. fastcgi_param REQUEST_URI $request_uri;
  10. fastcgi_param DOCUMENT_URI $document_uri;
  11. fastcgi_param DOCUMENT_ROOT $document_root;
  12. fastcgi_param SERVER_PROTOCOL $server_protocol;
  13. fastcgi_param REMOTE_ADDR $remote_addr;
  14. fastcgi_param REMOTE_PORT $remote_port;
  15. fastcgi_param SERVER_ADDR $server_addr;
  16. fastcgi_param SERVER_PORT $server_port;
  17. fastcgi_param SERVER_NAME $server_name;

3.重载nginx

  1. # service nginx reload

 

8安装Memcached服务器

1.memcached特性

Memcached是一款开发工具,它既不是一个代码加速器,也不是数据库中间件。其设计哲学思想主要反映在如下方面:

(1)简单key/value存储:服务器不关心数据本身的意义及结构,只要是可序列化数据即可。存储项由“键、过期时间、可选的标志及数据”四个部分组成;

(2)功能的实现一半依赖于客户端,一半基于服务器端:客户负责发送存储项至服务器端、从服务端获取数据以及无法连接至服务器时采用相应的动作;服务端负责接收、存储数据,并负责数据项的超时过期;

(3)各服务器间彼此无视:不在服务器间进行数据同步;

(4)O(1)的执行效率

(5)清理超期数据:默认情况下,Memcached是一个LRU缓存,同时,它按事先预订的时长清理超期数 据;但事实上,memcached不会删除任何已缓存数据,只是在其过期之后不再为客户所见;而且,memcached也不会真正按期限清理缓存,而仅是 当get命令到达时检查其时长;

2.安装memcached

a).部署开发环境,解决依赖关系

  1. # yum groupinstall “Development Tools” “Server Platform Deveopment” -y
  2. # yum install -y libevent-devel

   b).编译安装memcached

  1. # tar xf memcached-1.4.15.tar.gz
  2. # cd memcached-1.4.15
  3. # ./configure –prefix=/usr/local/memcached –with-libevent=/usr/local/libevent
  4. # make && make install

c).为memcached提供启动脚本

  1. #!/bin/bash
  2. #
  3. # Init file for memcached
  4. #
  5. # chkconfig: – 86 14
  6. # description: Distributed memory caching daemon
  7. #
  8. # processname: memcached
  9. # config: /etc/sysconfig/memcached
  10. . /etc/rc.d/init.d/functions
  11. ## Default variables
  12. PORT=”11211″
  13. USER=”nobody”
  14. MAXCONN=”1024″
  15. CACHESIZE=”64″
  16. OPTIONS=””
  17. RETVAL=0
  18. prog=”/usr/local/memcached/bin/memcached”
  19. desc=”Distributed memory caching”
  20. lockfile=”/var/lock/subsys/memcached”
  21. start() {
  22. echo -n $”Starting $desc (memcached): “
  23. daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE -o “$OPTIONS”
  24. RETVAL=$?
  25. [ $RETVAL -eq 0 ] && success && touch $lockfile || failure
  26. echo
  27. return $RETVAL
  28. }
  29. stop() {
  30. echo -n $”Shutting down $desc (memcached): “
  31. killproc $prog
  32. RETVAL=$?
  33. [ $RETVAL -eq 0 ] && success && rm -f $lockfile || failure
  34. echo
  35. return $RETVAL
  36. }
  37. restart() {
  38. stop
  39. start
  40. }
  41. reload() {
  42. echo -n $”Reloading $desc ($prog): “
  43. killproc $prog -HUP
  44. RETVAL=$?
  45. [ $RETVAL -eq 0 ] && success || failure
  46. echo
  47. return $RETVAL
  48. }
  49. case “$1” in
  50. start)
  51. start
  52. ;;
  53. stop)
  54. stop
  55. ;;
  56. restart)
  57. restart
  58. ;;
  59. condrestart)
  60. [ -e $lockfile ] && restart
  61. RETVAL=$?
  62. ;;
  63. reload)
  64. reload
  65. ;;
  66. status)
  67. status $prog
  68. RETVAL=$?
  69. ;;
  70. *)
  71. echo $”Usage: $0 {start|stop|restart|condrestart|status}”
  72. RETVAL=1
  73. esac
  74. exit $RETVAL

d).添加memcached至服务列表

  1. # chmod +x /etc/init.d/memcached
  2. # chkconfig –add memcached
  3. # service memcached start

   e).检查是否运行

  1. # ss -tnlp | grep 11211

 

9安装php的memcached扩展

1.安装memcached扩展

  1. # tar xf memcache-2.2.7.tgz
  2. # cd memcache-2.2.7
  3. # /usr/local/php/bin/phpize
  4. # ./configure –with-php-config=/usr/local/php/bin/php-config –enable-memcache
  5. # make && make install

2.编辑配置文件,整合php + memcached

  1. # vim xcache.ini
  2. //添加模块路径
  3. extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/memcache.so

3.重启php-fpm

  1. # service php-fpm restart

4.对memcached功能进行测试,在网站目录中建立测试页面test.php

  1. //php服务器
  2. # vim test.php
  3. <?php
  4. $mem = new Memcache;
  5. $mem->connect(“172.16.1.13”, 11211) or die(“Could not connect”);
  6. $version = $mem->getVersion();
  7. echo “Server’s version: “.$version.”<br/>n”;
  8. $mem->set(‘hellokey’, ‘Hello World’, 0, 600) or die(“Failed to save data at the memcached server”);
  9. echo “Store data in the cache (data will expire in 600 seconds)<br/>n”;
  10. $get_result = $mem->get(‘hellokey’);
  11. echo “$get_result is from memcached server.”;
  12. ?>
  13. ~

测试访问即可。

5.安装wordpress测试访问即可

 

10安装memadmin-master查看memcached状态信息

1.解压即可使用

  1. //解压至php服务器/usr/local/nginx/html/
  2. # unzip memadmin-master.zip

2.测试访问

www.hoo.com/memadmin-master
本文链接 http://tec.5lulu.com/detail/109krn14mpntt8rca.html



(责任编辑:IT)
------分隔线----------------------------