转:http://www.linuxidc.com/Linux/2015-07/120580.htm

Nginx+php-fpm+MySQL分离部署详解

[日期:2015-07-26] 来源:Linux社区  作者:tae44 [字体:  ]
 
 

相信大家将这三者部署到同一台主机应该已经不陌生了,今天在这里,给大家演示一下如何将三者部署到三台主机上。

实验系统:CentOS 6.6_x86_64

实验前提:大部分软件使用编译安装,请提前准备好编译环境,防火墙和selinux都关闭

实验软件:nginx-1.9.3 mariadb-10.0.20 php-5.6.11 memcache-2.2.7 xcache-3.2.0

实验拓扑:

Nginx+PHP-FPM时快时慢的解决  http://www.linuxidc.com/Linux/2014-10/108011.htm

LAMP-PHP-fpm服务器配置 http://www.linuxidc.com/Linux/2014-06/103093.htm

Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL http://www.linuxidc.com/Linux/2014-05/102351.htm

高负载PHP-FPM调优 http://www.linuxidc.com/Linux/2013-07/86963.htm

Nginx php-fpm出现502解决办法和调优心得 http://www.linuxidc.com/Linux/2013-01/78566.htm

Nginx+PHP-FPM在两台服务器实现 http://www.linuxidc.com/Linux/2012-11/74001.htm

Ubuntu 10.04配置 nginx + php-fpm 模式 http://www.linuxidc.com/Linux/2012-11/74001p2.htm

Nginx&&PHP-FPM配置及优化指南(上) http://www.linuxidc.com/Linux/2012-07/65732.htm

一、安装nginx

  1.解决依赖关系:

    需要专门安装pcre-devel包:

  1. 1 yum -y install pcre-devel

  2.添加nginx用户:

  1. 1 useradd -r nginx

  3.解压并编译安装nginx:

  1. 1 tar xf nginx-1.9.3.tar.gz
  2. 2 cd nginx-1.9.3
  3. 3 ./configure \
  4. 4 --prefix=/usr/local/nginx \                    //安装位置
  5. 5 --sbin-path=/usr/local/nginx/sbin/nginx \            //程序文件
  6. 6 --conf-path=/etc/nginx/nginx.conf \               //配置文件安装位置
  7. 7 --error-log-path=/var/log/nginx/error.log \           //错误日志安装位置
  8. 8 --http-log-path=/var/log/nginx/access.log \           //访问日志安装位置
  9. 9 --pid-path=/var/run/nginx/nginx.pid \              //pid文件位置
  10. 10 --lock-path=/var/lock/nginx.lock \                //锁文件位置
  11. 11 --user=nginx \                            //运行进程时使用的用户身份
  12. 12 --group=nginx \                           //运行进程时使用的用户组
  13. 13 --with-http_ssl_module \                      //支持ssl模块
  14. 14 --with-http_flv_module \                      //支持flv模块
  15. 15 --with-http_stub_status_module \                 //支持stub_status模块
  16. 16 --with-http_gzip_static_module \                 //支持gzip_static模块
  17. 17 --http-client-body-temp-path=/var/tmp/nginx/client/ \    //存储HTTP客户端请求body体的临时文件位置
  18. 18 --http-proxy-temp-path=/var/tmp/nginx/proxy/ \        //存储HTTP代理的临时文件位置
  19. 19 --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \        //存储fasycgi临时文件位置
  20. 20 --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \         //存储uwsgi临时文件位置
  21. 21 --http-scgi-temp-path=/var/tmp/nginx/scgi \          //存储scgi临时文件位置
  22. 22 --with-pcre                             //支持pcre库
  23. 23 make && make install
 

  4.提供脚本文件:

  1. 1 vim /etc/init.d/nginx
  2. 2 --------------------------------
  3. 3 #!/bin/sh
  4. 4 #
  5. 5 # nginx - this script starts and stops the nginx daemon
  6. 6 #
  7. 7 # chkconfig: - 85 15
  8. 8 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
  9. 9 # proxy and IMAP/POP3 proxy server
  10. 10 # processname: nginx
  11. 11 # config: /etc/nginx/nginx.conf
  12. 12 # config: /etc/sysconfig/nginx
  13. 13 # pidfile: /var/run/nginx.pid
  14. 14
  15. 15 # Source function library.
  16. 16 . /etc/rc.d/init.d/functions
  17. 17
  18. 18 # Source networking configuration.
  19. 19 . /etc/sysconfig/network
  20. 20
  21. 21 # Check that networking is up.
  22. 22 [ "$NETWORKING" = "no" ] && exit 0
  23. 23
  24. 24 nginx="/usr/local/nginx/sbin/nginx"
  25. 25 prog=$(basename $nginx)
  26. 26
  27. 27 NGINX_CONF_FILE="/etc/nginx/nginx.conf"
  28. 28
  29. 29 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  30. 30
  31. 31 lockfile=/var/lock/subsys/nginx
  32. 32
  33. 33 make_dirs() {
  34. 34 # make required directories
  35. 35 user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
  36. 36 options=`$nginx -V 2>&1 | grep 'configure arguments:'`
  37. 37 for opt in $options; do
  38. 38 if [ `echo $opt | grep '.*-temp-path'` ]; then
  39. 39 value=`echo $opt | cut -d "=" -f 2`
  40. 40 if [ ! -d "$value" ]; then
  41. 41 # echo "creating" $value
  42. 42 mkdir -p $value && chown -R $user $value
  43. 43 fi
  44. 44 fi
  45. 45 done
  46. 46 }
  47. 47
  48. 48 start() {
  49. 49 [ -x $nginx ] || exit 5
  50. 50 [ -f $NGINX_CONF_FILE ] || exit 6
  51. 51 make_dirs
  52. 52 echo -n $"Starting $prog: "
  53. 53 daemon $nginx -c $NGINX_CONF_FILE
  54. 54 retval=$?
  55. 55 echo
  56. 56 [ $retval -eq 0 ] && touch $lockfile
  57. 57 return $retval
  58. 58 }
  59. 59
  60. 60 stop() {
  61. 61 echo -n $"Stopping $prog: "
  62. 62 killproc $prog -QUIT
  63. 63 retval=$?
  64. 64 echo
  65. 65 [ $retval -eq 0 ] && rm -f $lockfile
  66. 66 return $retval
  67. 67 }
  68. 68
  69. 69 restart() {
  70. 70 configtest || return $?
  71. 71 stop
  72. 72 sleep 1
  73. 73 start
  74. 74 }
  75. 75
  76. 76 reload() {
  77. 77 configtest || return $?
  78. 78 echo -n $"Reloading $prog: "
  79. 79 killproc $nginx -HUP
  80. 80 RETVAL=$?
  81. 81 echo
  82. 82 }
  83. 83
  84. 84 force_reload() {
  85. 85 restart
  86. 86 }
  87. 87
  88. 88 configtest() {
  89. 89 $nginx -t -c $NGINX_CONF_FILE
  90. 90 }
  91. 91
  92. 92 rh_status() {
  93. 93 status $prog
  94. 94 }
  95. 95
  96. 96 rh_status_q() {
  97. 97 rh_status >/dev/null 2>&1
  98. 98 }
  99. 99
  100. 100 case "$1" in
  101. 101 start)
  102. 102 rh_status_q && exit 0
  103. 103 $1
  104. 104 ;;
  105. 105 stop)
  106. 106 rh_status_q || exit 0
  107. 107 $1
  108. 108 ;;
  109. 109 restart|configtest)
  110. 110 $1
  111. 111 ;;
  112. 112 reload)
  113. 113 rh_status_q || exit 7
  114. 114 $1
  115. 115 ;;
  116. 116 force-reload)
  117. 117 force_reload
  118. 118 ;;
  119. 119 status)
  120. 120 rh_status
  121. 121 ;;
  122. 122 condrestart|try-restart)
  123. 123 rh_status_q || exit 0
  124. 124 ;;
  125. 125 *)
  126. 126 echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  127. 127 exit 2
  128. 128 esac
  129. 129 --------------------------------
  130. 130 chmod +x /etc/init.d/nginx
  131. 131 chkconfig --add nginx
  132. 132 chkconfig nginx on
  133. 133 service nginx start

  5.测试访问页面,nginx安装完毕:

二、安装mysql

  1.添加mysql用户和创建数据目录:

  1. 1 useradd -r mysql
  2. 2 mkdir -pv /mydata/data
  3. 3 chown -R mysql:mysql /mydata/data

  2.解压并初始化mysql:

  1. 1 tar xf mariadb-10.0.20-linux-x86_64.tar.gz -C /usr/local/
  2. 2 cd /usr/local/
  3. 3 ln -sv mariadb-10.0.20-linux-x86_64 mysql
  4. 4 cd mysql/
  5. 5 chown -R root:mysql .
  6. 6 scripts/mysql_install_db --user=mysql --datadir=/mydata/data/

  3.提供配置文件:

  1. 1 cp support-files/my-large.cnf /etc/my.cnf
  2. 2 vim /etc/my.cnf
  3. 3 修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如:thread_concurrency = 2
  4. 4 另外还需要添加如下行指定mysql数据文件的存放位置:datadir = /mydata/data

  4.提供脚本文件:

  1. 1 cp support-files/mysql.server /etc/init.d/mysqld
  2. 2 chkconfig --add mysqld
  3. 3 chkconfig mysqld on
  4. 4 service mysqld start

  使用mysql目录的下的bin/mysql去连接mysql,出现如下画面表示连接成功:

  5.输出mysql的man手册至man命令的查找路径:   

    编辑/etc/man.config,添加如下行即可:MANPATH  /usr/local/mysql/man

  6.输出mysql的头文件至系统头文件路径/usr/include:
    这可以通过简单的创建链接实现: 
  1. 1 ln -sv /usr/local/mysql/include /usr/include/mysql

  7.输出mysql的库文件给系统库查找路径:

  1. 1 echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
  2. 2 ldconfig

三、安装PHP

  1.解决依赖关系:

  1. 1 yum -y install libxml2-devel bzip2-devel libcurl-devel libmcrypt-devel

  2.编译安装php:

  1. 1 ./configure --prefix=/usr/local/php \      //安装位置
  2. 2 --with-mysql \                   //支持mysql
  3. 3 --with-pdo-mysql \                //支持pdo模块
  4. 4 --with-mysqli \                  //支持mysqli��块         
  5. 5 --with-openssl \                  //支持openssl模块
  6. 6 --enable-fpm \                   //支持fpm模式
  7. 7 --enable-sockets \                //启用socket支持
  8. 8 --enable-sysvshm \                //启用系统共享内存支持
  9. 9 --enable-mbstring \                //使多字节字符串的支持
  10. 10 --with-freetype-dir \              //设置FreeType安装前缀路径
  11. 11 --with-jpeg-dir \                //设置libjpeg安装前缀路径
  12. 12 --with-png-dir \                //设置libpng安装前缀路径
  13. 13 --with-zlib-dir \                //设置libz安装前缀路径
  14. 14 --with-libxml-dir=/usr \            //设置libxml2安装路径
  15. 15 --enable-xml \                 
    16 --with-mhash \                 //支持mhash
  16. 17 --with-mcrypt \                 //支持mcrypt
  17. 18 --with-config-file-path=/etc \        //配置文件路径
  18. 19 --with-config-file-scan-dir=/etc/php.d \ //配置文件扫描路径
  19. 20 --with-bz2 \               //支持BZip2
  20. 21 --with-curl                   //支持curl
  21. 22 make && make install

  3.提供配置文件:

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

  4.为php-fpm提供脚本:

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

  5.提供php-fpm配置文件并编辑:

  1. 1 cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  2. 2 ------------------------------------------------------
  3. 3 pm.max_children = 150
  4. 4 pm.start_servers = 8
  5. 5 pm.min_spare_servers = 5
  6. 6 pm.max_spare_servers = 10
  7. 7 pid = /usr/local/php/var/run/php-fpm.pid
  6.启动php-fpm服务:
  1. 1 service php-fpm start

四、整合nginx与PHP

  1.nginx服务器建立网页文件存放目录/www,并修改其权限:

  1. 1 mkdir /www
  2. 2 chown -R nginx:nginx /www

  2.修改nginx配置文件:

  1. 1 vim /etc/nginx/nginx.conf
  2. 2 --------------------------------------
  3. 3 location / {
  4. 4 root /www;
  5. 5 index index.php index.html index.htm;
  6. 6 }
  7. 7
  8. 8 location ~ \.php$ {
  9. 9 root /www;
  10. 10 fastcgi_pass 192.168.19.92:9000;
  11. 11 fastcgi_index index.php;
  12. 12 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  13. 13 include fastcgi_params;
  14. 14 }
 

  3.修改fastcgi_params文件为:

  1. 1 fastcgi_param GATEWAY_INTERFACE CGI/1.1;
  2. 2 fastcgi_param SERVER_SOFTWARE nginx;
  3. 3 fastcgi_param QUERY_STRING $query_string;
  4. 4 fastcgi_param REQUEST_METHOD $request_method;
  5. 5 fastcgi_param CONTENT_TYPE $content_type;
  6. 6 fastcgi_param CONTENT_LENGTH $content_length;
  7. 7 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  8. 8 fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  9. 9 fastcgi_param REQUEST_URI $request_uri;
  10. 10 fastcgi_param DOCUMENT_URI $document_uri;
  11. 11 fastcgi_param DOCUMENT_ROOT $document_root;
  12. 12 fastcgi_param SERVER_PROTOCOL $server_protocol;
  13. 13 fastcgi_param REMOTE_ADDR $remote_addr;
  14. 14 fastcgi_param REMOTE_PORT $remote_port;
  15. 15 fastcgi_param SERVER_ADDR $server_addr;
  16. 16 fastcgi_param SERVER_PORT $server_port;
  17. 17 fastcgi_param SERVER_NAME $server_name;
  4.在PHP服务器上,建立nginx用户,要保证和nginx服务器上的nginx用户id号、组id号一致:
  5.修改php-fpm配置文件,并重启:
  1. 1 vim /usr/local/php/etc/php-fpm.conf
  2. 2 ---------------------------------------------
  3. 3 listen = 192.168.19.92:9000     //监听物理网卡地址,供其它机器调用
  4. 4 user = nginx //php-fpm以nginx用户运行
  5. 5 group = nginx
  6. 6 ---------------------------------------------
  7. 7 service php-fpm restart

  6.在PHP服务器上创建/www目录,并开启nfs服务:

  1. 1 mkdir /www
  2. 2 chown -R nginx:nginx /www
  3. 3 vim /etc/exports
  4. 4 ---------------------------------------------
  5. 5 /www 192.168.19.0/24(rw,no_root_squash)
  6. 6 ---------------------------------------------
  7. 7 service nfs start

  7.nginx服务器挂载nfs文件,并测试php,测试成功后删除index.php:

  1. 1 mount -t nfs 192.168.19.92:/www /www
  2. 2 vim /www/index.php
  3. 3 ---------------------------------------
  4. 4 <?php
  5. 5 phpinfo();
  6. 6 ?>
  7. 7 --------------------------------------
  8. 8 service nginx restart

【转】Nginx+php-fpm+MySQL分离部署详解的更多相关文章

  1. nginx+php-fpm+mysql分离部署详解

    相信大家将这三者部署到同一台主机应该已经不陌生了,今天在这里,给大家演示一下如何将三者部署到三台主机上. 实验系统:CentOS 6.6_x86_64 实验前提:大部分软件使用编译安装,请提前准备好编 ...

  2. MySQL高可用方案-PXC(Percona XtraDB Cluster)环境部署详解

    MySQL高可用方案-PXC(Percona XtraDB Cluster)环境部署详解 Percona XtraDB Cluster简称PXC.Percona Xtradb Cluster的实现是在 ...

  3. legend3---Windows 7/8/10 系统下Laravel框架的开发环境安装及部署详解(Vagrant + Homestead)

    legend3---Windows 7/8/10 系统下Laravel框架的开发环境安装及部署详解(Vagrant + Homestead) 一.总结 一句话总结: 1.安装的话就是下载好git,va ...

  4. Windows下Nginx Virtual Host多站点配置详解

    Windows下Nginx Virtual Host多站点配置详解 此教程适用于Windows系统已经配置好Nginx+Php+Mysql环境的同学. 如果您还未搭建WNMP环境,请查看 window ...

  5. Nginx+Tomcat的服务器端环境配置详解

    这篇文章主要介绍了Nginx+Tomcat的服务器端环境配置详解,包括Nginx与Tomcat的监控开启方法,需要的朋友可以参考下 Nginx+tomcat是目前主流的Javaweb架构,如何让ngi ...

  6. MySQL 联合索引详解

    MySQL 联合索引详解   联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分.例如索引是key index (a,b,c ...

  7. centos7.2环境elasticsearch-5.0.1+kibana-5.0.1+zookeeper3.4.6+kafka_2.9.2-0.8.2.1部署详解

    centos7.2环境elasticsearch-5.0.1+kibana-5.0.1+zookeeper3.4.6+kafka_2.9.2-0.8.2.1部署详解 环境准备: 操作系统:centos ...

  8. MySQL关闭过程详解和安全关闭MySQL的方法

    MySQL关闭过程详解和安全关闭MySQL的方法 www.hongkevip.com 时间: -- : 阅读: 整理: 红客VIP 分享到: 红客VIP(http://www.hongkevip.co ...

  9. Nginx 常用全局变量 及Rewrite规则详解

    每次都很容易忘记Nginx的变量,下面列出来了一些常用 $remote_addr //获取客户端ip $binary_remote_addr //客户端ip(二进制) $remote_port //客 ...

随机推荐

  1. NS_ASSUME_NONNULL_BEGIN,NS_ASSUME_NONNULL_END

    Nonnull区域设置(Audited Regions) 如果需要每个属性或每个方法都去指定nonnull和nullable,是一件非常繁琐的事.苹果为了减轻我们的工作量,专门提供了两个宏:NS_AS ...

  2. jquery动画遮罩

    以前一直以为遮罩都是鼠标移上去,改变透明度实现的,后来看到过这样的一个遮罩动画,然后今天自己写了一个,因为弹出的遮罩是圆形的,所以从美观上来说,这个遮罩效果更适合于方形图片. <div clas ...

  3. CSharp Similarities and Differences

    This document lists some basic differences between Nemerle and C# in a terse form. If you know Java ...

  4. 详解.NET异步

    在说到异步前,先来理一下几个容易混淆的概念,并行.多线程.异步. 并行,一般指并行计算,是说同一时刻有多条指令同时被执行,这些指令可能执行于同一CPU的多核上,或者多个CPU上,或者多个物理主机甚至多 ...

  5. Spring的DI(Ioc) - 注入集合类型

    1: 首先给service添加集合类型的属性,并提供getter, setter package cn.gbx.serviceimpl; import java.util.ArrayList; imp ...

  6. c++ string的实现。

    第三次做了.只是做个复习.偶然发现之前的版本有内存泄露.基本功还是不过关.这次应该没有内存泄漏了.虽然是个简单版本. 1)了解堆,栈,值copy. 2)几个常用的c的字符函数和c中的char 如何表示 ...

  7. ubuntu安装jdk-6u45-linux-x64.bin___ZC_20160423

    for : Android4.4源码编译 环境 : ubuntu12.04_desktop_amd64 1. 1.1.jdk-6u45-linux-x64.bin 放置于 /home 1.2.命令&q ...

  8. GooglePlay_下载apk

    关键字:"APK Downloader" 方式: (1)."APK Downloader"网站在线下载(无需我们的GooglePlay账户信息,也就无需Goog ...

  9. 利用ajax.dll类库文件实现无刷新

    使用这种方法前需要配置相应的环境 1.引用ajax.dll文件 2.在web.config添加如下: <httpHandlers>   <add path="ajax/*. ...

  10. django的安装和搭建

    一.先下载pyton,配置下python的环境变量,这个很重要,然后下载django,解压到与python同一个根目录底下,进入django目录,运行python setup.py install安装 ...