LNMP架构(一)

标签: LNMP

LNMP架构介绍

  • 和LAMP不同的是,提供web服务的是Nginx

  • 并且php是作为一个独立服务存在的,这个服务叫做php-fpm

  • Nginx直接处理静态请求,动态请求会转发给php-fpm

 

MySQL安装

先把旧的mysql卸载,卸载步骤:

  [[email protected] ~]# ps aux |grep mysql  ##检查mysql服务是否开启,开启需要关闭
  root      1627  0.0  0.0 112720   968 pts/0    R+   21:22   0:00 grep --color=auto mysql
  [[email protected] ~]# rm -rf /usr/local/mysql  ##删除mysql的目录
  [[email protected] ~]# rm -rf /etc/init.d/mysqld  ##删除mysqld的启动脚本
  [[email protected] ~]# vim /etc/my.cnf  ##可以保留[[email protected] ~]# ps aux |grep mysql  ##检查mysql服务是否开启,开启需要关闭
  root      1627  0.0  0.0 112720   968 pts/0    R+   21:22   0:00 grep --color=auto mysql
  [[email protected] ~]# rm -rf /usr/local/mysql  ##删除mysql的目录
  [[email protected] ~]# rm -rf /etc/init.d/mysqld  ##删除mysqld的启动脚本
  [[email protected] ~]# vim /etc/my.cnf  ##可以保留

安装新的mysql步骤:

   cd /usr/local/src
   wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 
   tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
   mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
   cd /usr/local/mysql
   useradd mysql
   mkdir /data/
   ##初始化,就是mysql启动需要的目录全部生成。
   ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
   cp support-files/my-default.cnf  /etc/my.cnf 
   cp support-files/mysql.server /etc/init.d/mysqld
   ##定义basedir和datadir
   vi /etc/init.d/mysqld
   /etc/init.d/mysqld start
   [[email protected] mysql]# ps aux |grep mysql  ##确认mysql服务启动
  root      1789  0.0  0.0 113308  1612 pts/0    S    21:38   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/zyshanlinux-001.pid
  mysql     1924  2.0 24.0 973096 451200 pts/0   Sl   21:38   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/zyshanlinux-001.err --pid-file=/data/mysql/zyshanlinux-001.pid --socket=/tmp/mysql.sock
  root      1948  0.0  0.0 112720   972 pts/0    R+   21:39   0:00 grep --color=auto mysql
  [[email protected] mysql]# chkconfig --add mysqld  ##把mysqld加入到服务列表
  [[email protected] mysql]# chkconfig mysqld on  ##开机启动
  [[email protected] mysql]# service mysqld stop  ##加入列表后可以这样停止
  Shutting down MySQL.. SUCCESS! 
  [[email protected] mysql]# service mysqld start   ##加入列表后可以这样启动
  Starting MySQL. SUCCESS! 
  [[email protected] mysql]# ps aux |grep mysql  ##确实是开启状态
  root      2048  0.0  0.0  11816  1604 pts/0    S    21:43   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/zyshanlinux-001.pid
  mysql     2184  4.0 23.9 973096 449532 pts/0   Sl   21:43   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/zyshanlinux-001.err --pid-file=/data/mysql/zyshanlinux-001.pid --socket=/tmp/mysql.sock
  root      2208  0.0  0.0 112720   968 pts/0    R+   21:43   0:00 grep --color=auto mysql cd /usr/local/src
   wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 
   tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
   mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
   cd /usr/local/mysql
   useradd mysql
   mkdir /data/
   ##初始化,就是mysql启动需要的目录全部生成。
   ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
   cp support-files/my-default.cnf  /etc/my.cnf 
   cp support-files/mysql.server /etc/init.d/mysqld
   ##定义basedir和datadir
   vi /etc/init.d/mysqld
   /etc/init.d/mysqld start
   [[email protected] mysql]# ps aux |grep mysql  ##确认mysql服务启动
  root      1789  0.0  0.0 113308  1612 pts/0    S    21:38   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/zyshanlinux-001.pid
  mysql     1924  2.0 24.0 973096 451200 pts/0   Sl   21:38   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/zyshanlinux-001.err --pid-file=/data/mysql/zyshanlinux-001.pid --socket=/tmp/mysql.sock
  root      1948  0.0  0.0 112720   972 pts/0    R+   21:39   0:00 grep --color=auto mysql
  [[email protected] mysql]# chkconfig --add mysqld  ##把mysqld加入到服务列表
  [[email protected] mysql]# chkconfig mysqld on  ##开机启动
  [[email protected] mysql]# service mysqld stop  ##加入列表后可以这样停止
  Shutting down MySQL.. SUCCESS! 
  [[email protected] mysql]# service mysqld start   ##加入列表后可以这样启动
  Starting MySQL. SUCCESS! 
  [[email protected] mysql]# ps aux |grep mysql  ##确实是开启状态
  root      2048  0.0  0.0  11816  1604 pts/0    S    21:43   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/zyshanlinux-001.pid
  mysql     2184  4.0 23.9 973096 449532 pts/0   Sl   21:43   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/zyshanlinux-001.err --pid-file=/data/mysql/zyshanlinux-001.pid --socket=/tmp/mysql.sock
  root      2208  0.0  0.0 112720   968 pts/0    R+   21:43   0:00 grep --color=auto mysql

 

PHP安装

删除旧的编译文件命令:make clean 就变成刚解压的状态

安装步骤:

  和LAMP安装PHP方法有差别,需要开启php-fpm服务
   [[email protected]]# cd /usr/local/src/
   [[email protected] php-5.6.30]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
   [[email protected] php-5.6.30]# tar zxf php-5.6.30.tar.gz
   [[email protected] php-5.6.30]# groupadd php-fpm;useradd -g php-fpm php-fpm  ##创建php-fpm用户
   [[email protected] php-5.6.30]# cd php-5.6.30
   ./configure --prefix=/usr/local/php-fpm(指定路径) --with-config-file-path=/usr/local/php-fpm/etc (指定配置文件所在路径)--enable-fpm(加上这句才能启动该服务) --with-fpm-user=php-fpm(指定用户) --with-fpm-group=php-fpm(指定用户组) --with-mysql=/usr/local/mysql(指定mysql的路径) --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl  --with-openssl和LAMP安装PHP方法有差别,需要开启php-fpm服务
   [[email protected]]# cd /usr/local/src/
   [[email protected] php-5.6.30]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
   [[email protected] php-5.6.30]# tar zxf php-5.6.30.tar.gz
   [[email protected] php-5.6.30]# groupadd php-fpm start;useradd -g php-fpm php-fpm  ##创建php-fpm组和用户
   [[email protected] php-5.6.30]# cd php-5.6.30
   ./configure --prefix=/usr/local/php-fpm(指定路径) --with-config-file-path=/usr/local/php-fpm/etc (指定配置文件所在路径)--enable-fpm(加上这句才能启动该服务) --with-fpm-user=php-fpm(指定用户) --with-fpm-group=php-fpm(指定用户组) --with-mysql=/usr/local/mysql(指定mysql的路径) --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl  --with-openssl

排障:

  checking for cURL in default path... not found
  configure: error: Please reinstall the libcurl distribution -
      easy.h should be in <curl-dir>/include/curl/
  [[email protected] php-5.6.30]# 
  [[email protected] php-5.6.30]# yum install libcurl-develchecking for cURL in default path... not found
  configure: error: Please reinstall the libcurl distribution -
      easy.h should be in <curl-dir>/include/curl/
  [[email protected] php-5.6.30]# 
  [[email protected] php-5.6.30]# yum install libcurl-devel

初始化成功:

  +--------------------------------------------------------------------+
  | License:                                                           |
  | This software is subject to the PHP License, available in this     |
  | distribution in the file LICENSE.  By continuing this installation |
  | process, you are bound by the terms of this license agreement.     |
  | If you do not agree with the terms of this license, you must abort |
  | the installation process at this point.                            |
  +--------------------------------------------------------------------+
  ​
  Thank you for using PHP.
  ​
  config.status: creating php5.spec
  config.status: creating main/build-defs.h
  config.status: creating scripts/phpize
  config.status: creating scripts/man1/phpize.1
  config.status: creating scripts/php-config
  config.status: creating scripts/man1/php-config.1
  config.status: creating sapi/cli/php.1
  config.status: creating sapi/fpm/php-fpm.conf
  config.status: creating sapi/fpm/init.d.php-fpm
  config.status: creating sapi/fpm/php-fpm.service
  config.status: creating sapi/fpm/php-fpm.8
  config.status: creating sapi/fpm/status.html
  config.status: creating sapi/cgi/php-cgi.1
  config.status: creating ext/phar/phar.1
  config.status: creating ext/phar/phar.phar.1
  config.status: creating main/php_config.h
  config.status: executing default commands+--------------------------------------------------------------------+
  | License:                                                           |
  | This software is subject to the PHP License, available in this     |
  | distribution in the file LICENSE.  By continuing this installation |
  | process, you are bound by the terms of this license agreement.     |
  | If you do not agree with the terms of this license, you must abort |
  | the installation process at this point.                            |
  +--------------------------------------------------------------------+
  ​
  Thank you for using PHP.
  ​
  config.status: creating php5.spec
  config.status: creating main/build-defs.h
  config.status: creating scripts/phpize
  config.status: creating scripts/man1/phpize.1
  config.status: creating scripts/php-config
  config.status: creating scripts/man1/php-config.1
  config.status: creating sapi/cli/php.1
  config.status: creating sapi/fpm/php-fpm.conf
  config.status: creating sapi/fpm/init.d.php-fpm
  config.status: creating sapi/fpm/php-fpm.service
  config.status: creating sapi/fpm/php-fpm.8
  config.status: creating sapi/fpm/status.html
  config.status: creating sapi/cgi/php-cgi.1
  config.status: creating ext/phar/phar.1
  config.status: creating ext/phar/phar.phar.1
  config.status: creating main/php_config.h
  config.status: executing default commands

下面这步需要等待一段时间,完成

  [[email protected] php-5.6.30]# make && make install
  ...
  Installing PEAR environment:      /usr/local/php-fpm/lib/php/
  [PEAR] Archive_Tar    - installed: 1.4.0
  [PEAR] Console_Getopt - installed: 1.4.1
  [PEAR] Structures_Graph- installed: 1.1.1
  [PEAR] XML_Util       - installed: 1.3.0
  [PEAR] PEAR           - installed: 1.10.1
  Wrote PEAR system config file at: /usr/local/php-fpm/etc/pear.conf
  You may want to add: /usr/local/php-fpm/lib/php to your php.ini include_path
  /usr/local/src/php-5.6.30/build/shtool install -c ext/phar/phar.phar /usr/local/php-fpm/bin
  ln -s -f phar.phar /usr/local/php-fpm/bin/phar
  Installing PDO headers:           /usr/local/php-fpm/include/php/ext/pdo/[[email protected] php-5.6.30]# make && make install
  ...
  Installing PEAR environment:      /usr/local/php-fpm/lib/php/
  [PEAR] Archive_Tar    - installed: 1.4.0
  [PEAR] Console_Getopt - installed: 1.4.1
  [PEAR] Structures_Graph- installed: 1.1.1
  [PEAR] XML_Util       - installed: 1.3.0
  [PEAR] PEAR           - installed: 1.10.1
  Wrote PEAR system config file at: /usr/local/php-fpm/etc/pear.conf
  You may want to add: /usr/local/php-fpm/lib/php to your php.ini include_path
  /usr/local/src/php-5.6.30/build/shtool install -c ext/phar/phar.phar /usr/local/php-fpm/bin
  ln -s -f phar.phar /usr/local/php-fpm/bin/phar
  Installing PDO headers:           /usr/local/php-fpm/include/php/ext/pdo/

对比php和php-fpm的区别

  [[email protected] php-5.6.30]# ls /usr/local/php-fpm
  bin  etc  include  lib  php  sbin  var
  [[email protected] php-5.6.30]# ls /usr/local/php
  bin  etc  include  lib  php
  [[email protected] php-5.6.30]# ls /usr/local/php-fpm/sbin  ##php-fpm服务所在
  php-fpm
  [[email protected] php-5.6.30]# ls /usr/local/php-fpm/var/
  log  run
  [[email protected] php-5.6.30]# ls /usr/local/php-fpm/var/log/  ##存放日志
  [[email protected] php-5.6.30]# ls /usr/local/php-fpm/var/run/  ##存放PID
  [[email protected] php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -m
  [[email protected] php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -i
  [[email protected] php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t ##测试配置文件的语法
  [[email protected] php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
  [[email protected] php-5.6.30]# cd /usr/local/php-fpm/etc/
  [[email protected] etc]# ls
  pear.conf  php-fpm.conf.default  php.ini
  [[email protected] etc]# vim php-fpm.conf[[email protected] php-5.6.30]# ls /usr/local/php-fpm
  bin  etc  include  lib  php  sbin  var
  [[email protected] php-5.6.30]# ls /usr/local/php
  bin  etc  include  lib  php
  [[email protected] php-5.6.30]# ls /usr/local/php-fpm/sbin  ##php-fpm服务所在
  php-fpm
  [[email protected] php-5.6.30]# ls /usr/local/php-fpm/var/
  log  run
  [[email protected] php-5.6.30]# ls /usr/local/php-fpm/var/log/  ##存放日志
  [[email protected] php-5.6.30]# ls /usr/local/php-fpm/var/run/  ##存放PID
  [[email protected] php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -m
  [[email protected] php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -i
  [[email protected] php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t ##测试配置文件的语法
  [[email protected] php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
  [[email protected] php-5.6.30]# cd /usr/local/php-fpm/etc/
  [[email protected] etc]# ls
  pear.conf  php-fpm.conf.default  php.ini
  [[email protected] etc]# vim php-fpm.conf

配置php-fpm.conf

  [global]  ##定义全局参数
  pid = /usr/local/php-fpm/var/run/php-fpm.pid
  error_log = /usr/local/php-fpm/var/log/php-fpm.log
  [www]  ##模块的名字
  listen = /tmp/php-fcgi.sock  ##监听的地址
  ##可以写成这样 listen = 127.0.0.1:9000 适合本地使用,即php和Nginx在同一台机器里
  listen.mode = 666  ##定义sock的权限是666
  user = php-fpm  ##用户
  group = php-fpm  ##组
  pm = dynamic
  pm.max_children = 50
  pm.start_servers = 20
  pm.min_spare_servers = 5
  pm.max_spare_servers = 35
  pm.max_requests = 500
  rlimit_files = 1024
  ​
  [[email protected] php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t ##测试配置文件的语法[global]  ##定义全局参数
  pid = /usr/local/php-fpm/var/run/php-fpm.pid
  error_log = /usr/local/php-fpm/var/log/php-fpm.log
  [www]  ##模块的名字
  listen = /tmp/php-fcgi.sock  ##监听的地址
  ##可以写成这样 listen = 127.0.0.1:9000 适合本地使用,即php和Nginx在同一台机器里
  listen.mode = 666  ##定义sock的权限是666
  user = php-fpm  ##用户
  group = php-fpm  ##组
  pm = dynamic
  pm.max_children = 50
  pm.start_servers = 20
  pm.min_spare_servers = 5
  pm.max_spare_servers = 35
  pm.max_requests = 500
  rlimit_files = 1024
  ​
  [[email protected] php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t ##测试配置文件的语法

拷贝启动脚本:

  [[email protected] php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm[[email protected] php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

加入服务列表,启动php-fpm服务

  [[email protected] php-5.6.30]# chmod 755 /etc/init.d/php-fpm
  [[email protected] php-5.6.30]# chkconfig --add php-fpm
  [[email protected] php-5.6.30]# chkconfig php-fpm on
  [[email protected] php-5.6.30]# service php-fpm start
  Starting php-fpm  done
  [[email protected] php-5.6.30]# ps aux |grep php-fpm
  root     53160  0.0  0.2 123652  4948 ?        Ss   22:35   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
  php-fpm  53161  0.0  0.2 123652  4708 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53162  0.0  0.2 123652  4708 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53163  0.0  0.2 123652  4708 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53164  0.0  0.2 123652  4708 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53165  0.0  0.2 123652  4712 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53166  0.0  0.2 123652  4712 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53167  0.0  0.2 123652  4712 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53168  0.0  0.2 123652  4712 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53169  0.0  0.2 123652  4712 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53170  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53171  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53172  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53173  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53174  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53175  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53176  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53177  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53178  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53179  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53180  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  root     53182  0.0  0.0 112720   972 pts/0    R+   22:38   0:00 grep --color=auto php-fpm[[email protected] php-5.6.30]# chmod 755 /etc/init.d/php-fpm
  [[email protected] php-5.6.30]# chkconfig --add php-fpm
  [[email protected] php-5.6.30]# chkconfig php-fpm on
  [[email protected] php-5.6.30]# service php-fpm start
  Starting php-fpm  done
  [[email protected] php-5.6.30]# ps aux |grep php-fpm
  root     53160  0.0  0.2 123652  4948 ?        Ss   22:35   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
  php-fpm  53161  0.0  0.2 123652  4708 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53162  0.0  0.2 123652  4708 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53163  0.0  0.2 123652  4708 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53164  0.0  0.2 123652  4708 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53165  0.0  0.2 123652  4712 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53166  0.0  0.2 123652  4712 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53167  0.0  0.2 123652  4712 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53168  0.0  0.2 123652  4712 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53169  0.0  0.2 123652  4712 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53170  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53171  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53172  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53173  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53174  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53175  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53176  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53177  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53178  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53179  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  php-fpm  53180  0.0  0.2 123652  4716 ?        S    22:35   0:00 php-fpm: pool www
  root     53182  0.0  0.0 112720   972 pts/0    R+   22:38   0:00 grep --color=auto php-fpm

 

Nginx介绍

  Nginx官网 nginx.org,最新版1.13,最新稳定版1.12 (俄国人开发的)
  Nginx应用场景:web服务、反向代理、负载均衡
  Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并
  Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考http://jinnianshilongnian.iteye.com/blog/2280928Nginx官网 nginx.org,最新版1.13,最新稳定版1.12 (俄国人开发的)
  Nginx应用场景:web服务、反向代理、负载均衡
  Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并
  Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考http://jinnianshilongnian.iteye.com/blog/2280928

 

Nginx安装

安装步骤:

  [[email protected]]# cd /usr/local/src
  [[email protected] src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
  [[email protected] src]# tar zxf nginx-1.12.1.tar.gz
  [[email protected] src]# cd nginx-1.12.1/
  [[email protected] nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
  [[email protected] nginx-1.12.1]# make &&  make install[[email protected]]# cd /usr/local/src
  [[email protected] src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
  [[email protected] src]# tar zxf nginx-1.12.1.tar.gz
  [[email protected] src]# cd nginx-1.12.1/
  [[email protected] nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
  [[email protected] nginx-1.12.1]# make &&  make install

nginx各目录情况

  [[email protected] nginx-1.12.1]# ls /usr/local/nginx
  conf  html  logs  sbin
  [[email protected] nginx-1.12.1]# ls /usr/local/nginx/conf/
  fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
  fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
  fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf
  [[email protected] nginx-1.12.1]# ls /usr/local/nginx/html/
  50x.html  index.html
  [[email protected] nginx-1.12.1]# ls /usr/local/nginx/logs/
  [[email protected] nginx-1.12.1]# ls /usr/local/nginx/sbin/
  nginx
  [[email protected] nginx-1.12.1]# ls /usr/local/nginx/sbin/nginx
  /usr/local/nginx/sbin/nginx
  [[email protected] nginx-1.12.1]# /usr/local/nginx/sbin/nginx -t
  nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[[email protected] nginx-1.12.1]# ls /usr/local/nginx
  conf  html  logs  sbin
  [[email protected]inux-001 nginx-1.12.1]# ls /usr/local/nginx/conf/
  fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
  fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
  fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf
  [[email protected] nginx-1.12.1]# ls /usr/local/nginx/html/
  50x.html  index.html
  [[email protected] nginx-1.12.1]# ls /usr/local/nginx/logs/
  [[email protected] nginx-1.12.1]# ls /usr/local/nginx/sbin/
  nginx
  [[email protected] nginx-1.12.1]# ls /usr/local/nginx/sbin/nginx
  /usr/local/nginx/sbin/nginx
  [[email protected] nginx-1.12.1]# /usr/local/nginx/sbin/nginx -t
  nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

配置 /etc/init.d/nginx

[[email protected] nginx-1.12.1]# vim /etc/init.d/nginx

  #!/bin/bash
  # chkconfig: - 30 21
  # description: http service.
  # Source Function Library
  . /etc/init.d/functions
  # Nginx Settings
  ​
  NGINX_SBIN="/usr/local/nginx/sbin/nginx"
  NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
  NGINX_PID="/usr/local/nginx/logs/nginx.pid"
  RETVAL=0
  prog="Nginx"
  ​
  start() 
  {
      echo -n $"Starting $prog: "
      mkdir -p /dev/shm/nginx_temp
      daemon $NGINX_SBIN -c $NGINX_CONF
      RETVAL=$?
      echo
      return $RETVAL
  }
  ​
  stop() 
  {
      echo -n $"Stopping $prog: "
      killproc -p $NGINX_PID $NGINX_SBIN -TERM
      rm -rf /dev/shm/nginx_temp
      RETVAL=$?
      echo
      return $RETVAL
  }
  ​
  reload()
  {
      echo -n $"Reloading $prog: "
      killproc -p $NGINX_PID $NGINX_SBIN -HUP
      RETVAL=$?
      echo
      return $RETVAL
  }
  ​
  restart()
  {
      stop
      start
  }
  ​
  configtest()
  {
      $NGINX_SBIN -c $NGINX_CONF -t
      return 0
  }
  ​
  case "$1" in
    start)
          start
          ;;
    stop)
          stop
          ;;
    reload)
          reload
          ;;
    restart)
          restart
          ;;
    configtest)
          configtest
          ;;
    *)
          echo $"Usage: $0 {start|stop|reload|restart|configtest}"
          RETVAL=1
  esac
  ​
  exit $RETVAL#!/bin/bash
  # chkconfig: - 30 21
  # description: http service.
  # Source Function Library
  . /etc/init.d/functions
  # Nginx Settings
  ​
  NGINX_SBIN="/usr/local/nginx/sbin/nginx"
  NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
  NGINX_PID="/usr/local/nginx/logs/nginx.pid"
  RETVAL=0
  prog="Nginx"
  ​
  start() 
  {
      echo -n $"Starting $prog: "
      mkdir -p /dev/shm/nginx_temp
      daemon $NGINX_SBIN -c $NGINX_CONF
      RETVAL=$?
      echo
      return $RETVAL
  }
  ​
  stop() 
  {
      echo -n $"Stopping $prog: "
      killproc -p $NGINX_PID $NGINX_SBIN -TERM
      rm -rf /dev/shm/nginx_temp
      RETVAL=$?
      echo
      return $RETVAL
  }
  ​
  reload()
  {
      echo -n $"Reloading $prog: "
      killproc -p $NGINX_PID $NGINX_SBIN -HUP
      RETVAL=$?
      echo
      return $RETVAL
  }
  ​
  restart()
  {
      stop
      start
  }
  ​
  configtest()
  {
      $NGINX_SBIN -c $NGINX_CONF -t
      return 0
  }
  ​
  case "$1" in
    start)
          start
          ;;
    stop)
          stop
          ;;
    reload)
          reload
          ;;
    restart)
          restart
          ;;
    configtest)
          configtest
          ;;
    *)
          echo $"Usage: $0 {start|stop|reload|restart|configtest}"
          RETVAL=1
  esac
  ​
  exit $RETVAL

加入服务列表,启动Nginx服务

  [[email protected] nginx-1.12.1]# chmod 755 /etc/init.d/nginx
  [[email protected] nginx-1.12.1]# chkconfig --add nginx 
  [[email protected] nginx-1.12.1]# chkconfig nginx on 
  [[email protected] nginx-1.12.1]# cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak[[email protected] nginx-1.12.1]# chmod 755 /etc/init.d/nginx
  [[email protected] nginx-1.12.1]# chkconfig --add nginx 
  [[email protected] nginx-1.12.1]# chkconfig nginx on 
  [[email protected] nginx-1.12.1]# cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak

跳转,备份

  [[email protected] nginx-1.12.1]# cd /usr/local/nginx/conf/
  [[email protected] conf]# mv nginx.conf nginx.conf.bak[[email protected] nginx-1.12.1]# cd /usr/local/nginx/conf/
  [[email protected] conf]# mv nginx.conf nginx.conf.bak

编辑nginx.conf [[email protected] conf]# vim nginx.conf

  user nobody nobody;
  worker_processes 2;  ##定义子进程有几个
  error_log /usr/local/nginx/logs/nginx_error.log crit;
  pid /usr/local/nginx/logs/nginx.pid;
  worker_rlimit_nofile 51200;  ##定义最多打开多少个
  ​
  events
  {
      use epoll;
      worker_connections 6000;
  }
  ​
  http
  {
      include mime.types;
      default_type application/octet-stream;
      server_names_hash_bucket_size 3526;
      server_names_hash_max_size 4096;
      log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
      ' $host "$request_uri" $status'
      ' "$http_referer" "$http_user_agent"';
      sendfile on;
      tcp_nopush on;
      keepalive_timeout 30;
      client_header_timeout 3m;
      client_body_timeout 3m;
      send_timeout 3m;
      connection_pool_size 256;
      client_header_buffer_size 1k;
      large_client_header_buffers 8 4k;
      request_pool_size 4k;
      output_buffers 4 32k;
      postpone_output 1460;
      client_max_body_size 10m;
      client_body_buffer_size 256k;
      client_body_temp_path /usr/local/nginx/client_body_temp;
      proxy_temp_path /usr/local/nginx/proxy_temp;
      fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
      fastcgi_intercept_errors on;
      tcp_nodelay on;
      gzip on;
      gzip_min_length 1k;
      gzip_buffers 4 8k;
      gzip_comp_level 5;
      gzip_http_version 1.1;
      gzip_types text/plain application/x-javascript text/css text/htm 
      application/xml;
  ​
      server
      {
          listen 80;
          server_name localhost;
          index index.html index.htm index.php;
          root /usr/local/nginx/html;
  ​
          location ~ \.php$ 
          {
              include fastcgi_params;
              fastcgi_pass unix:/tmp/php-fcgi.sock;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
          }    
      }
  }user nobody nobody;
  worker_processes 2;  ##定义子进程有几个
  error_log /usr/local/nginx/logs/nginx_error.log crit;
  pid /usr/local/nginx/logs/nginx.pid;
  worker_rlimit_nofile 51200;  ##定义最多打开多少个
  ​
  events
  {
      use epoll;
      worker_connections 6000;
  }
  ​
  http
  {
      include mime.types;
      default_type application/octet-stream;
      server_names_hash_bucket_size 3526;
      server_names_hash_max_size 4096;
      log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
      ' $host "$request_uri" $status'
      ' "$http_referer" "$http_user_agent"';
      sendfile on;
      tcp_nopush on;
      keepalive_timeout 30;
      client_header_timeout 3m;
      client_body_timeout 3m;
      send_timeout 3m;
      connection_pool_size 256;
      client_header_buffer_size 1k;
      large_client_header_buffers 8 4k;
      request_pool_size 4k;
      output_buffers 4 32k;
      postpone_output 1460;
      client_max_body_size 10m;
      client_body_buffer_size 256k;
      client_body_temp_path /usr/local/nginx/client_body_temp;
      proxy_temp_path /usr/local/nginx/proxy_temp;
      fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
      fastcgi_intercept_errors on;
      tcp_nodelay on;
      gzip on;
      gzip_min_length 1k;
      gzip_buffers 4 8k;
      gzip_comp_level 5;
      gzip_http_version 1.1;
      gzip_types text/plain application/x-javascript text/css text/htm 
      application/xml;
  ​
      server
      {
          listen 80;
          server_name localhost;
          index index.html index.htm index.php;
          root /usr/local/nginx/html;
  ​
          location ~ \.php$ 
          {
              include fastcgi_params;
              fastcgi_pass unix:/tmp/php-fcgi.sock;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
          }    
      }
  }

启动nginx服务

  [[email protected] conf]# /usr/local/nginx/sbin/nginx -t  ##检查语法
  nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  [[email protected] conf]# 
  [[email protected] conf]# /etc/init.d/nginx start
  Starting nginx (via systemctl):                            [  确定  ]
  [[email protected] conf]# ps aux |grep nginx
  root     55981  0.0  0.0  20540   628 ?        Ss   23:23   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  nobody   55982  0.0  0.1  22984  3208 ?        S    23:23   0:00 nginx: worker process
  nobody   55983  0.0  0.1  22984  3208 ?        S    23:23   0:00 nginx: worker process
  root     55986  0.0  0.0 112720   972 pts/0    R+   23:23   0:00 grep --color=auto nginx[[email protected] conf]# /usr/local/nginx/sbin/nginx -t  ##检查语法
  nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  [[email protected] conf]# 
  [[email protected] conf]# /etc/init.d/nginx start
  Starting nginx (via systemctl):                            [  确定  ]
  [[email protected] conf]# ps aux |grep nginx
  root     55981  0.0  0.0  20540   628 ?        Ss   23:23   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  nobody   55982  0.0  0.1  22984  3208 ?        S    23:23   0:00 nginx: worker process
  nobody   55983  0.0  0.1  22984  3208 ?        S    23:23   0:00 nginx: worker process
  root     55986  0.0  0.0 112720   972 pts/0    R+   23:23   0:00 grep --color=auto nginx

测试:

  [[email protected] conf]# curl localhost
  <!DOCTYPE html>
  <html>
  <head>
  <title>Welcome to nginx!</title>
  <style>
      body {
          width: 35em;
          margin: 0 auto;
          font-family: Tahoma, Verdana, Arial, sans-serif;
      }
  </style>
  </head>
  <body>
  <h1>Welcome to nginx!</h1>
  <p>If you see this page, the nginx web server is successfully installed and
  working. Further configuration is required.</p>
  ​
  <p>For online documentation and support please refer to
  <a href="http://nginx.org/">nginx.org</a>.<br/>
  Commercial support is available at
  <a href="http://nginx.com/">nginx.com</a>.</p>
  ​
  <p><em>Thank you for using nginx.</em></p>
  </body>
  </html>[[email protected] conf]# curl localhost
  <!DOCTYPE html>
  <html>
  <head>
  <title>Welcome to nginx!</title>
  <style>
      body {
          width: 35em;
          margin: 0 auto;
          font-family: Tahoma, Verdana, Arial, sans-serif;
      }
  </style>
  </head>
  <body>
  <h1>Welcome to nginx!</h1>
  <p>If you see this page, the nginx web server is successfully installed and
  working. Further configuration is required.</p>
  ​
  <p>For online documentation and support please refer to
  <a href="http://nginx.org/">nginx.org</a>.<br/>
  Commercial support is available at
  <a href="http://nginx.com/">nginx.com</a>.</p>
  ​
  <p><em>Thank you for using nginx.</em></p>
  </body>
  </html>

新建1.php,测试成功

  [[email protected] conf]# vi /usr/local/nginx/html/1.php
  <?php
  echo "This is nginx test page";
  [[email protected] conf]# curl localhost/1.php
  This is nginx test page[[email protected] conf]#[[email protected] conf]# vi /usr/local/nginx/html/1.php
  <?php
  echo "This is nginx test page";
  [[email protected] conf]# curl localhost/1.php
  This is nginx test page[[email protected] conf]#

 

 

 

拓展:

Nginx为什么比Httpd高效:原理篇 http://www.toxingwang.com/linux-unix/linux-basic/1712.html

mod_php 和 mod_fastcgi以及php-fpm的比较 http://dwz.cn/1lwMSd

概念了解:CGI,FastCGI,PHP-CGI与PHP-FPMhttp://www.nowamagic.net/librarys/veda/detail/1319/ https://www.awaimai.com/371.html

 

 

Nginx 隐藏服务器名称和版本号http://www.voidcn.com/article/p-epufjzny-od.html

为什么基于事件驱动的服务器能实现高并发?https://www.zhihu.com/question/64727674

CGI、FastCGI和PHP-FPM关系图解https://www.awaimai.com/371.html

https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/IO.md

原文链接:加载失败,请重新获取