知识屋:更实用的电脑技术知识网站
所在位置:首页 > 网络安全 > 安全资讯

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

发布时间:2014-05-21 09:27:52作者:知识屋

一、LNMMP

LNMMP环境是Linux + Nginx + Memcached + MySQL + PhP,即LNMP + memcached。

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

二、工程拓扑

Web服务之LNMMP架构及动静分离实现——dnjsb.com


 

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

四、安装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;

 

五、安装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

 

六、安装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

七、配置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

八、安装Memcached服务器[!--empirenews.page--]分页标题[/!--empirenews.page--]

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

九、安装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测试访问即可

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

1.解压即可使用


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

2.测试访问

www.hoo.com/memadmin-master

(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜