LNMP架構(一)

LNMP架構介紹

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

  • 並且php是作爲一個獨立服務存在的,這個服務叫做php-fpm

  • Nginx直接處理靜態請求,動態請求會轉發給php-fpm

 

MySQL安裝

先把舊的mysql卸載,卸載步驟:

  [root@zyshanlinux-001 ~]# ps aux |grep mysql  ##檢查mysql服務是否開啓,開啓需要關閉
  root      1627  0.0  0.0 112720   968 pts/0    R+   21:22   0:00 grep --color=auto mysql
  [root@zyshanlinux-001 ~]# rm -rf /usr/local/mysql  ##刪除mysql的目錄
  [root@zyshanlinux-001 ~]# rm -rf /etc/init.d/mysqld  ##刪除mysqld的啓動腳本
  [root@zyshanlinux-001 ~]# vim /etc/my.cnf  ##可以保留[root@zyshanlinux-001 ~]# ps aux |grep mysql  ##檢查mysql服務是否開啓,開啓需要關閉
  root      1627  0.0  0.0 112720   968 pts/0    R+   21:22   0:00 grep --color=auto mysql
  [root@zyshanlinux-001 ~]# rm -rf /usr/local/mysql  ##刪除mysql的目錄
  [root@zyshanlinux-001 ~]# rm -rf /etc/init.d/mysqld  ##刪除mysqld的啓動腳本
  [root@zyshanlinux-001 ~]# 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
   [root@zyshanlinux-001 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
  [root@zyshanlinux-001 mysql]# chkconfig --add mysqld  ##把mysqld加入到服務列表
  [root@zyshanlinux-001 mysql]# chkconfig mysqld on  ##開機啓動
  [root@zyshanlinux-001 mysql]# service mysqld stop  ##加入列表後可以這樣停止
  Shutting down MySQL.. SUCCESS! 
  [root@zyshanlinux-001 mysql]# service mysqld start   ##加入列表後可以這樣啓動
  Starting MySQL. SUCCESS! 
  [root@zyshanlinux-001 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
   [root@zyshanlinux-001 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
  [root@zyshanlinux-001 mysql]# chkconfig --add mysqld  ##把mysqld加入到服務列表
  [root@zyshanlinux-001 mysql]# chkconfig mysqld on  ##開機啓動
  [root@zyshanlinux-001 mysql]# service mysqld stop  ##加入列表後可以這樣停止
  Shutting down MySQL.. SUCCESS! 
  [root@zyshanlinux-001 mysql]# service mysqld start   ##加入列表後可以這樣啓動
  Starting MySQL. SUCCESS! 
  [root@zyshanlinux-001 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服務
   [root@zyshanlinux-001]# cd /usr/local/src/
   [root@zyshanlinux-001 php-5.6.30]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
   [root@zyshanlinux-001 php-5.6.30]# tar zxf php-5.6.30.tar.gz
   [root@zyshanlinux-001 php-5.6.30]# groupadd php-fpm;useradd -g php-fpm php-fpm  ##創建php-fpm用戶
   [root@zyshanlinux-001 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服務
   [root@zyshanlinux-001]# cd /usr/local/src/
   [root@zyshanlinux-001 php-5.6.30]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
   [root@zyshanlinux-001 php-5.6.30]# tar zxf php-5.6.30.tar.gz
   [root@zyshanlinux-001 php-5.6.30]# groupadd php-fpm start;useradd -g php-fpm php-fpm  ##創建php-fpm組和用戶
   [root@zyshanlinux-001 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/
  [root@zyshanlinux-001 php-5.6.30]# 
  [root@zyshanlinux-001 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/
  [root@zyshanlinux-001 php-5.6.30]# 
  [root@zyshanlinux-001 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

下面這步需要等待一段時間,完成

  [root@zyshanlinux-001 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/[root@zyshanlinux-001 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的區別

  [root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php-fpm
  bin  etc  include  lib  php  sbin  var
  [root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php
  bin  etc  include  lib  php
  [root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php-fpm/sbin  ##php-fpm服務所在
  php-fpm
  [root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php-fpm/var/
  log  run
  [root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php-fpm/var/log/  ##存放日誌
  [root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php-fpm/var/run/  ##存放PID
  [root@zyshanlinux-001 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -m
  [root@zyshanlinux-001 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -i
  [root@zyshanlinux-001 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t ##測試配置文件的語法
  [root@zyshanlinux-001 php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
  [root@zyshanlinux-001 php-5.6.30]# cd /usr/local/php-fpm/etc/
  [root@zyshanlinux-001 etc]# ls
  pear.conf  php-fpm.conf.default  php.ini
  [root@zyshanlinux-001 etc]# vim php-fpm.conf[root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php-fpm
  bin  etc  include  lib  php  sbin  var
  [root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php
  bin  etc  include  lib  php
  [root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php-fpm/sbin  ##php-fpm服務所在
  php-fpm
  [root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php-fpm/var/
  log  run
  [root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php-fpm/var/log/  ##存放日誌
  [root@zyshanlinux-001 php-5.6.30]# ls /usr/local/php-fpm/var/run/  ##存放PID
  [root@zyshanlinux-001 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -m
  [root@zyshanlinux-001 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -i
  [root@zyshanlinux-001 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t ##測試配置文件的語法
  [root@zyshanlinux-001 php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
  [root@zyshanlinux-001 php-5.6.30]# cd /usr/local/php-fpm/etc/
  [root@zyshanlinux-001 etc]# ls
  pear.conf  php-fpm.conf.default  php.ini
  [root@zyshanlinux-001 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
  ​
  [root@zyshanlinux-001 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
  ​
  [root@zyshanlinux-001 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t ##測試配置文件的語法

拷貝啓動腳本:

  [root@zyshanlinux-001 php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm[root@zyshanlinux-001 php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

加入服務列表,啓動php-fpm服務

  [root@zyshanlinux-001 php-5.6.30]# chmod 755 /etc/init.d/php-fpm
  [root@zyshanlinux-001 php-5.6.30]# chkconfig --add php-fpm
  [root@zyshanlinux-001 php-5.6.30]# chkconfig php-fpm on
  [root@zyshanlinux-001 php-5.6.30]# service php-fpm start
  Starting php-fpm  done
  [root@zyshanlinux-001 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[root@zyshanlinux-001 php-5.6.30]# chmod 755 /etc/init.d/php-fpm
  [root@zyshanlinux-001 php-5.6.30]# chkconfig --add php-fpm
  [root@zyshanlinux-001 php-5.6.30]# chkconfig php-fpm on
  [root@zyshanlinux-001 php-5.6.30]# service php-fpm start
  Starting php-fpm  done
  [root@zyshanlinux-001 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安裝

安裝步驟:

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

nginx各目錄情況

  [root@zyshanlinux-001 nginx-1.12.1]# ls /usr/local/nginx
  conf  html  logs  sbin
  [root@zyshanlinux-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
  [root@zyshanlinux-001 nginx-1.12.1]# ls /usr/local/nginx/html/
  50x.html  index.html
  [root@zyshanlinux-001 nginx-1.12.1]# ls /usr/local/nginx/logs/
  [root@zyshanlinux-001 nginx-1.12.1]# ls /usr/local/nginx/sbin/
  nginx
  [root@zyshanlinux-001 nginx-1.12.1]# ls /usr/local/nginx/sbin/nginx
  /usr/local/nginx/sbin/nginx
  [root@zyshanlinux-001 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[root@zyshanlinux-001 nginx-1.12.1]# ls /usr/local/nginx
  conf  html  logs  sbin
  [root@zyshanlinux-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
  [root@zyshanlinux-001 nginx-1.12.1]# ls /usr/local/nginx/html/
  50x.html  index.html
  [root@zyshanlinux-001 nginx-1.12.1]# ls /usr/local/nginx/logs/
  [root@zyshanlinux-001 nginx-1.12.1]# ls /usr/local/nginx/sbin/
  nginx
  [root@zyshanlinux-001 nginx-1.12.1]# ls /usr/local/nginx/sbin/nginx
  /usr/local/nginx/sbin/nginx
  [root@zyshanlinux-001 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

[root@zyshanlinux-001 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服務

  [root@zyshanlinux-001 nginx-1.12.1]# chmod 755 /etc/init.d/nginx
  [root@zyshanlinux-001 nginx-1.12.1]# chkconfig --add nginx 
  [root@zyshanlinux-001 nginx-1.12.1]# chkconfig nginx on 
  [root@zyshanlinux-001 nginx-1.12.1]# cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak[root@zyshanlinux-001 nginx-1.12.1]# chmod 755 /etc/init.d/nginx
  [root@zyshanlinux-001 nginx-1.12.1]# chkconfig --add nginx 
  [root@zyshanlinux-001 nginx-1.12.1]# chkconfig nginx on 
  [root@zyshanlinux-001 nginx-1.12.1]# cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak

跳轉,備份

  [root@zyshanlinux-001 nginx-1.12.1]# cd /usr/local/nginx/conf/
  [root@zyshanlinux-001 conf]# mv nginx.conf nginx.conf.bak[root@zyshanlinux-001 nginx-1.12.1]# cd /usr/local/nginx/conf/
  [root@zyshanlinux-001 conf]# mv nginx.conf nginx.conf.bak

編輯nginx.conf [root@zyshanlinux-001 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服務

  [root@zyshanlinux-001 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
  [root@zyshanlinux-001 conf]# 
  [root@zyshanlinux-001 conf]# /etc/init.d/nginx start
  Starting nginx (via systemctl):                            [  確定  ]
  [root@zyshanlinux-001 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[root@zyshanlinux-001 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
  [root@zyshanlinux-001 conf]# 
  [root@zyshanlinux-001 conf]# /etc/init.d/nginx start
  Starting nginx (via systemctl):                            [  確定  ]
  [root@zyshanlinux-001 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

測試:

  [root@zyshanlinux-001 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>[root@zyshanlinux-001 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,測試成功

  [root@zyshanlinux-001 conf]# vi /usr/local/nginx/html/1.php
  <?php
  echo "This is nginx test page";
  [root@zyshanlinux-001 conf]# curl localhost/1.php
  This is nginx test page[root@zyshanlinux-001 conf]#[root@zyshanlinux-001 conf]# vi /usr/local/nginx/html/1.php
  <?php
  echo "This is nginx test page";
  [root@zyshanlinux-001 conf]# curl localhost/1.php
  This is nginx test page[root@zyshanlinux-001 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

發佈了57 篇原創文章 · 獲贊 22 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章