目錄javascript
1、LNMP架構介紹
2、MySQL安裝
3、PHP安裝
4、Nginx介紹
5、Nginx安裝
6、擴展php
以前已經學習過LAMP架構,與LAMP相比,LNMP用nginx替代apache來提供web服務。php也再也不是做爲模塊形式存在,而是做爲一個叫php-fpm的獨立進程來提供服務。css
Nginx直接處理靜態請求,動態請求會轉發給php-fpm處理。html
LNMP結構圖java
Mysql的安裝方式有多種,yum安裝,二進制免編譯安裝,源碼包編譯安裝等。node
yum安裝缺少定製性,若是不是性能要求特別高的場合,也可使用。安裝比較方便。mysql
二進制包免編譯和源碼包編譯安裝相似,只是免去了編譯過程,節省了時間 ,並且定製性和性能也基本沒有損失。若是沒有特殊需求,推薦二進制免編譯安裝。linux
這裏以源代碼的編譯安裝爲例,掌握了源代碼的編譯安裝,二進制免編譯安裝也沒有什麼難度。過程相似,只是省去了編譯的過程。nginx
1.環境c++
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@localhost ~]# uname -i x86_64
2.依賴包安裝
[root@localhost ~]# yum -y install gcc gcc-c++ ncurses-devel perl cmake autoconf Loaded plugins: fastestmirror base | 3.6 kB 00:00:00 epel/x86_64/metalink | 7.2 kB 00:00:00 epel | 3.2 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 ...中間略... Dependency Updated: cpp.x86_64 0:4.8.5-28.el7_5.1 gcc-gfortran.x86_64 0:4.8.5-28.el7_5.1 libgcc.x86_64 0:4.8.5-28.el7_5.1 libgfortran.x86_64 0:4.8.5-28.el7_5.1 libgomp.x86_64 0:4.8.5-28.el7_5.1 libquadmath.x86_64 0:4.8.5-28.el7_5.1 libquadmath-devel.x86_64 0:4.8.5-28.el7_5.1 libstdc++.x86_64 0:4.8.5-28.el7_5.1 libstdc++-devel.x86_64 0:4.8.5-28.el7_5.1 ncurses.x86_64 0:5.9-14.20130511.el7_4 ncurses-base.noarch 0:5.9-14.20130511.el7_4 ncurses-libs.x86_64 0:5.9-14.20130511.el7_4 Complete!
3.建立mysql的數據庫存放目錄
[root@localhost ~]# mkdir -p /data/mysql [root@localhost ~]# ls -ld !$ ls -ld /data/mysql drwxr-xr-x. 2 root root 6 Jun 30 22:15 /data/mysql
4.建立mysql用戶及用戶組
[root@localhost ~]# useradd -r mysql -s /sbin/nologin [root@localhost ~]# id mysql uid=997(mysql) gid=995(mysql) groups=995(mysql)
5.設置mysql的數據庫存放目錄權限
[root@localhost ~]# chown -R mysql:mysql /data/mysql [root@localhost ~]# ls -ld !$ ls -ld /data/mysql drwxr-xr-x. 2 mysql mysql 6 Jun 30 22:15 /data/mysql
6.編譯安裝MySQL
//進入/usr/local/src目錄 [root@localhost ~]# cd /usr/local/src //下載源碼包 [root@localhost src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36.tar.gz --2018-06-30 22:19:30-- http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36.tar.gz Resolving mirrors.sohu.com (mirrors.sohu.com)... 221.236.12.140 Connecting to mirrors.sohu.com (mirrors.sohu.com)|221.236.12.140|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 32192348 (31M) [application/octet-stream] Saving to: ‘mysql-5.6.36.tar.gz’ 100%[======================================================================================>] 32,192,348 1.33MB/s in 23s 2018-06-30 22:19:54 (1.33 MB/s) - ‘mysql-5.6.36.tar.gz’ saved [32192348/32192348] //解壓mysql-5.6.36.tar.gz [root@localhost src]# tar -zxvf mysql-5.6.36.tar.gz ... mysql-5.6.36/zlib/trees.c mysql-5.6.36/zlib/trees.h mysql-5.6.36/zlib/uncompr.c mysql-5.6.36/zlib/zconf.h mysql-5.6.36/zlib/zlib.3 mysql-5.6.36/zlib/zlib.h mysql-5.6.36/zlib/zutil.c mysql-5.6.36/zlib/zutil.h //進入mysql-5.6.36目錄 [root@localhost src]# cd mysql-5.6.36 [root@localhost mysql-5.6.36]# pwd /usr/local/src/mysql-5.6.36 //mysql比較特殊,用的cmake編譯 [root@localhost mysql-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/data/mysql -DMYSQL_TCP_PORT=3306 -DENABLE_DOWNLOADS=1 -- Running cmake version 2.8.12.2 -- Found Git: /usr/bin/git (found version "1.8.3.1") -- The C compiler identification is GNU 4.8.5 -- The CXX compiler identification is GNU 4.8.5 ...中間略... -- CMAKE_C_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Wwrite-strings -Wdeclaration-after-statement -- CMAKE_CXX_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Woverloaded-virtual -Wno-unused-parameter -- CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF -- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF -- Configuring done -- Generating done -- Build files have been written to: /usr/local/src/mysql-5.6.36 //驗證 [root@localhost mysql-5.6.36]# echo $? 0 //編譯安裝 [root@localhost mysql-5.6.36]# make && make install Scanning dependencies of target INFO_BIN [ 0%] Built target INFO_BIN Scanning dependencies of target INFO_SRC [ 0%] Built target INFO_SRC Scanning dependencies of target abi_check ...中間略... -- Installing: /usr/local/mysql/sql-bench/test-alter-table -- Installing: /usr/local/mysql/sql-bench/test-big-tables -- Installing: /usr/local/mysql/sql-bench/test-connect -- Installing: /usr/local/mysql/sql-bench/test-create -- Installing: /usr/local/mysql/sql-bench/test-insert -- Installing: /usr/local/mysql/sql-bench/test-select -- Installing: /usr/local/mysql/sql-bench/test-transactions -- Installing: /usr/local/mysql/sql-bench/test-wisconsin //驗證 [root@localhost mysql-5.6.36]# echo $? 0
7.配置mysql
7.1初始化mysql
//進入/usr/local/mysql目錄 [root@localhost mysql-5.6.36]# cd /usr/local/mysql [root@localhost mysql]# pwd /usr/local/mysql //執行初始化配置腳本,建立數據庫和表 [root@localhost mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql Installing MySQL system tables...2018-06-30 23:04:59 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2018-06-30 23:04:59 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap. ...中間略... WARNING: Default config file /etc/my.cnf exists on the system This file will be read by default by the MySQL server If you do not want to use this, either remove it, or use the --defaults-file argument to mysqld_safe when starting the server //驗證 [root@localhost mysql]# echo $? 0
7.2設置配置文件
//複製模板配置文件爲/etc/my.cnf [root@localhost mysql]# \cp ./support-files/my-default.cnf /etc/my.cnf [root@localhost mysql]# ls -l /etc/my.cnf -rw-r--r--. 1 root root 1126 Jun 30 23:06 /etc/my.cnf //修改/etc/my.cnf [root@localhost mysql]# vim /etc/my.cnf //修改basedir和datadir爲如下內容 basedir = /usr/local/mysql datadir = /data/mysql
7.3設置啓動腳本
//複製啓動腳本到/etc/init.d/下 [root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld //將mysqld設爲開機啓動 [root@localhost mysql]# chkconfig mysqld on [root@localhost mysql]# chkconfig --list Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off //啓動mysql服務 [root@localhost mysql]# /etc/init.d/mysqld start Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'. SUCCESS! //驗證mysql服務已經正常啓動 [root@localhost mysql]# lsof -i :3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 16771 mysql 10u IPv6 36043 0t0 TCP *:mysql (LISTEN) [root@localhost mysql]# ss -nat | grep 3306 LISTEN 0 80 :::3306 :::* [root@localhost mysql]# netstat -nltup | grep :3306 tcp6 0 0 :::3306 :::* LISTEN 16771/mysqld
八、設置PATH,不然不能直接調用mysql
//修改/etc/profile文件,在文件末尾添加 [root@localhost mysql]# vim /etc/profile //文件末尾添加下行內容 export PATH=/usr/local/mysql/bin:$PATH //使配置文件生效 [root@localhost mysql]# source !$ source /etc/profile //驗證 [root@localhost mysql]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.36 Source distribution Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
在LAMP架構中,php是作爲apache的一個模塊存在的,而在LNMP架構中,php是做爲一個單獨的服務存在,因此安裝方式及參數有些許差異。
1.環境
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@localhost ~]# uname -i x86_64
2.依賴包的安裝
yum -y install libxml2-devel libpng-devel openssl openssl-devel bzip2 bzip2-devel libjpeg-devel freetype-devel libmcrypt.x86_64 libmcrypt-devel.x86_64 curl curl-devel
3.新建php-fpm用戶
[root@localhost php-5.6.30]# useradd -r -s /sbin/nologin php-fpm [root@localhost php-5.6.30]# id php-fpm uid=996(php-fpm) gid=994(php-fpm) groups=994(php-fpm)
4.編譯安裝php-fpm
//進入/usr/local/src目錄 [root@localhost local]# cd /usr/local/src [root@localhost src]# pwd /usr/local/src //下載軟件包 [root@localhost src]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz --2018-06-30 23:25:17-- http://cn2.php.net/distributions/php-5.6.30.tar.gz Resolving cn2.php.net (cn2.php.net)... 36.51.255.144 Connecting to cn2.php.net (cn2.php.net)|36.51.255.144|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 19274631 (18M) [application/x-gzip] Saving to: ‘php-5.6.30.tar.gz’ 100%[======================================================================================>] 19,274,631 72.1KB/s in 6m 5s 2018-06-30 23:31:22 (51.6 KB/s) - ‘php-5.6.30.tar.gz’ saved [19274631/19274631] //解壓軟件包 [root@localhost src]# tar -zxvf php-5.6.30.tar.gz php-5.6.30/ php-5.6.30/php.ini-production php-5.6.30/.gdbinit php-5.6.30/Makefile.gcov php-5.6.30/acinclude.m4 php-5.6.30/.editorconfig ...中間略... php-5.6.30/pear/fetch.php php-5.6.30/pear/Makefile.frag php-5.6.30/README.SUBMITTING_PATCH //進入php-5.6.30目錄 [root@localhost src]# cd php-5.6.30 //配置 [root@localhost src]# ./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 --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 grep that handles long lines and -e... /usr/bin/grep checking for egrep... /usr/bin/grep -E checking for a sed that does not truncate output... /usr/bin/sed checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking target system type... x86_64-unknown-linux-gnu ...中間略... config.status: creating ext/phar/phar.phar.1 config.status: creating main/php_config.h config.status: executing default commands //驗證 [root@localhost php-5.6.30]# echo $? 0 //編譯、安裝 [root@localhost php-5.6.30]# make && make install /bin/sh /usr/local/src/php-5.6.30/libtool --silent --preserve-dup-deps --mode=compile cc -Iext/opcache/ -I/usr/local/src/php-5.6.30/ext/opcache/ -DPHP_ATOM_INC -I/usr/local/src/php-5.6.30/include -I/usr/local/src/php-5.6.30/main -I/usr/local/src/php-5.6.30 -I/usr/local/src/php-5.6.30/ext/date/lib -I/usr/local/src/php-5.6.30/ext/ereg/regex -I/usr/include/libxml2 -I/usr/include/freetype2 -I/usr/local/src/php-5.6.30/ext/mbstring/oniguruma -I/usr/local/src/php-5.6.30/ext/mbstring/libmbfl -I/usr/local/src/php-5.6.30/ext/mbstring/libmbfl/mbfl -I/usr/local/mysql/include -I/usr/local/src/php-5.6.30/ext/sqlite3/libsqlite -I/usr/local/src/php-5.6.30/TSRM -I/usr/local/src/php-5.6.30/Zend -I/usr/include -g -O2 -fvisibility=hidden -c /usr/local/src/php-5.6.30/ext/opcache/ZendAccelerator.c -o ext/opcache/ZendAccelerator.lo /usr/local/src/php-5.6.30/ext/opcache/ZendAccelerator.c: In function ‘accel_use_shm_interned_strings’: ...中間略... 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@localhost php-5.6.30]# echo $? 0
5.配置php-fpm
//複製模版配置文件到/usr/local/php-fpm/etc/下 [root@localhost php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini [root@localhost php-5.6.30]# ls -l /usr/local/php-fpm/etc/php.ini -rw-r--r--. 1 root root 73685 Jul 1 02:23 /usr/local/php-fpm/etc/php.ini //在/usr/local/php-fpm/etc/下建立php-fpm.conf 文件,內容參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/php-fpm.conf [root@localhost php-5.6.30]# cd /usr/local/php-fpm/etc/ [root@localhost etc]# vim php-fpm.conf //文件內容以下: [global] //進程id所在的位置 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 //也可寫成下面的形式,相應的php和nginx結合時語法也有差別 //listen =127.0.0.1:9000 listen.mode = 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 //將啓動腳本複製到/etc/init.d目錄下 [root@localhost etc]# cp /usr/local/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@localhost etc]# ls -l /etc/init.d/php-fpm -rw-r--r--. 1 root root 2358 Jul 1 02:29 /etc/init.d/php-fpm //賦予啓動腳本執行權限 [root@localhost etc]# chmod a+x /etc/init.d/php-fpm [root@localhost etc]# ls -l /etc/init.d/php-fpm -rwxr-xr-x. 1 root root 2358 Jul 1 02:29 /etc/init.d/php-fpm [root@localhost etc]# cd /etc/init.d/ //設置php-fpm開機啓動 [root@localhost init.d]# chkconfig --add php-fpm [root@localhost init.d]# chkconfig --list Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off //啓動php-fpm [root@localhost init.d]# /etc/init.d/php-fpm start Starting php-fpm done //驗證php-fpm是否啓動 [root@localhost init.d]# ps aux | grep php-fpm root 41088 0.1 0.4 227248 4944 ? Ss 02:30 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf) php-fpm 41089 0.0 0.4 227248 4732 ? S 02:30 0:00 php-fpm: pool www php-fpm 41090 0.0 0.4 227248 4732 ? S 02:30 0:00 php-fpm: pool www php-fpm 41091 0.0 0.4 227248 4732 ? S 02:30 0:00 php-fpm: pool www php-fpm 41092 0.0 0.4 227248 4732 ? S 02:30 0:00 php-fpm: pool www php-fpm 41093 0.0 0.4 227248 4736 ? S 02:30 0:00 php-fpm: pool www php-fpm 41094 0.0 0.4 227248 4736 ? S 02:30 0:00 php-fpm: pool www php-fpm 41095 0.0 0.4 227248 4736 ? S 02:30 0:00 php-fpm: pool www php-fpm 41096 0.0 0.4 227248 4736 ? S 02:30 0:00 php-fpm: pool www php-fpm 41097 0.0 0.4 227248 4736 ? S 02:30 0:00 php-fpm: pool www php-fpm 41098 0.0 0.4 227248 4736 ? S 02:30 0:00 php-fpm: pool www php-fpm 41099 0.0 0.4 227248 4736 ? S 02:30 0:00 php-fpm: pool www php-fpm 41100 0.0 0.4 227248 4740 ? S 02:30 0:00 php-fpm: pool www php-fpm 41101 0.0 0.4 227248 4740 ? S 02:30 0:00 php-fpm: pool www php-fpm 41102 0.0 0.4 227248 4740 ? S 02:30 0:00 php-fpm: pool www php-fpm 41103 0.0 0.4 227248 4740 ? S 02:30 0:00 php-fpm: pool www php-fpm 41104 0.0 0.4 227248 4740 ? S 02:30 0:00 php-fpm: pool www php-fpm 41105 0.0 0.4 227248 4740 ? S 02:30 0:00 php-fpm: pool www php-fpm 41106 0.0 0.4 227248 4740 ? S 02:30 0:00 php-fpm: pool www php-fpm 41107 0.0 0.4 227248 4740 ? S 02:30 0:00 php-fpm: pool www php-fpm 41108 0.0 0.4 227248 4740 ? S 02:30 0:00 php-fpm: pool www root 41110 0.0 0.0 112660 976 pts/0 S+ 02:30 0:00 grep --color=auto php-fpm
Error
1.configure: error: xml2-config not found. Please check your libxml2 installation.
[root@localhost php-5.6.30]# yum -y install libxml2-devel
2.configure: error: Cannot find OpenSSL's <evp.h>
[root@localhost php-5.6.30]# yum -y install openssl openssl-devel
3.configure: error: Please reinstall the libcurl distribution -easy.h should be in
[root@localhost php-5.6.30]#yum -y install curl curl-devel
4.configure: error: jpeglib.h not found.
[root@localhost php-5.6.30]# yum -y install libjpeg-devel
5.configure: error: png.h not found.
[root@localhost php-5.6.30]# yum -y install libpng-devel
6.configure: error: freetype-config not found.
[root@localhost php-5.6.30]# yum -y install freetype-devel
Nginx("engine x")是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP代理服務器。Nginx是由Igor Sysoev爲俄羅斯訪問量第二的Rambler.ru站點開發的,第一個公開版本0.1.0發佈於2004年10月4日。其將源代碼以類BSD許可證的形式發佈,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。 它已經在衆多流量很大的俄羅斯網站上使用了很長時間,這些網站包括Yandex、Mail.Ru、VKontakte,以及Rambler。目前中國互聯網企業70%以上公司都在使用nginx做爲本身的web服務器。包括目前互聯網主流公司百度、騰訊、阿里等。Nginx 特色是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好。
Nginx由內核和模塊組成,其中內核的設計很是微小和簡潔,完成的工做也很是簡單,僅僅經過查找配置文件將客戶端請求映射到一個location block(location是Nginx配置中的一個指令,用於URL匹配),而在這個location 中所配置的每一個指令將會啓動不一樣的模塊去完成相應的工做。Nginx相對於Apache優勢:
1) 高併發響應性能很是好,官方測試Nginx處理靜態文件併發5w/s
2) 反向代理性能很是強。(可用於負載均衡)
3) 內存和 cpu 佔用率低。(爲 Apache 的 1/5-1/10)
4) 對後端服務有健康檢查功能。
5) 支持 PHP cgi 方式和 fastcgi 方式。
6) 配置代碼簡潔且容易上手。
Nginx 工做原理
Nginx由內核和模塊組成,其中,內核的設計很是微小和簡潔,完成的工做也很是簡單,僅僅經過查找配置文件將客戶端請求映射到 一個location block(location是Nginx 配置中的一個指令,用於URL匹配),而在這個location中配置的每一個指令將會啓動不一樣的模塊去完成相應的工做。Nginx的模塊從結構上分爲核心模塊、基礎模塊和第三方模塊:
核心模塊:HTTP模塊、EVENT 模塊和 MAIL 模塊
基礎模塊:HTTP Access 模塊、HTTP FastCGI 模塊、HTTP Proxy模塊和 HTTP Rewrite 模塊,
第三方模塊:HTTP Upstream Request Hash 模塊、Notice 模塊和HTTP Access Key 模塊。
Nginx 的高併發得益於其採用了epoll模型,與傳統的服務器程序架構不一樣,epoll 是 linux 內核 2.6 之後纔出現的。Nginx 採用epoll模型,異步非阻塞,而apache 採用的是 select模型:Select特色:select選擇句柄的時候,是遍歷全部句柄,也就是說句柄有事件響應時,select 須要遍歷全部句柄才能獲取到哪些句柄有事件通知,所以效率是很是低。epoll 的特色:epoll 對於句柄事件的選擇不是遍歷的,是事件響應的,就是句柄上事件來就立刻選擇出來,不須要遍歷整個句柄鏈表,所以效率很是高。
nginx官網:http://nginx.org/,最新版本1.15,最新穩定版:1.14。(2018年7月)
Nginx著名分支:Tengine, 是淘寶基於Nginx開發的,使用上和Nginx一致,服務名,配置文件名都同樣,和Nginx的最大區別在於Tenging增長了一些定製化模塊,在安全限速方面表現突出,另外它支持對js,css合併。
另外,Nginx核心+lua相關的組件和模塊組成了一個支持lua的高性能web容器openresty,使用Lua編寫腳本,而後部署到Nginx Web容器中運行。從而很是輕鬆就能開發出高性能的Web服務。
當前最新穩定版1.14.0
安裝過程以下
1.環境
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@localhost ~]# uname -i x86_64
2.依賴包安裝
[root@localhost src]# yum -y install pcre.x86_64 pcre-devel.x86_64 openssl openssl-devel
3.新建nginx服務用戶
[root@localhost src]# useradd nginx -r -s /sbin/nologin [root@localhost src]# id nginx uid=995(nginx) gid=993(nginx) groups=993(nginx)
4.編譯安裝nginx
//官網下載最新穩定版源代碼包 [root@localhost init.d]# wget http://nginx.org/download/nginx-1.14.0.tar.gz -O /usr/local/src/nginx-1.14.0.tar.gz --2018-07-01 03:44:41-- http://nginx.org/download/nginx-1.14.0.tar.gz Resolving nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3, ... Connecting to nginx.org (nginx.org)|206.251.255.63|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1016272 (992K) [application/octet-stream] Saving to: ‘/usr/local/src/nginx-1.14.0.tar.gz’ 100%[======================================================================================>] 1,016,272 486KB/s in 2.0s 2018-07-01 03:44:44 (486 KB/s) - ‘/usr/local/src/nginx-1.14.0.tar.gz’ saved [1016272/1016272] [root@localhost init.d]# cd /usr/local/src/ && ls mysql-5.6.36 mysql-5.6.36.tar.gz nginx-1.14.0.tar.gz php-5.6.30 php-5.6.30.tar.gz //解壓nginx [root@localhost src]# tar -zxvf nginx-1.14.0.tar.gz nginx-1.14.0/ nginx-1.14.0/auto/ nginx-1.14.0/conf/ nginx-1.14.0/contrib/ ...中間略... nginx-1.14.0/auto/cc/msvc nginx-1.14.0/auto/cc/name nginx-1.14.0/auto/cc/owc nginx-1.14.0/auto/cc/sunc //進入nginx軟件包目錄 [root@localhost src]# cd nginx-1.14.0 [root@localhost nginx-1.14.0]# pwd /usr/local/src/nginx-1.14.0 //配置 [root@localhost nginx-1.14.0]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module checking for OS + Linux 3.10.0-693.el7.x86_64 x86_64 checking for C compiler ... found + using GNU C compiler + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ...中間略... nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" //編譯和安裝 [root@localhost nginx-1.14.0]# make && make install make -f objs/Makefile make[1]: Entering directory `/usr/local/src/nginx-1.14.0' cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ ...中間略... test -d '/usr/local/nginx/html' \ || cp -R html '/usr/local/nginx' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' make[1]: Leaving directory `/usr/local/src/nginx-1.14.0' //驗證 [root@localhost nginx-1.14.0]# echo $? 0
5.初始配置
//添加啓動腳本 [root@localhost nginx-1.14.0]# 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 //賦予腳本執行權限 [root@localhost nginx-1.14.0]# chmod 755 /etc/init.d/nginx [root@localhost nginx-1.14.0]# ls -l !$ ls -l /etc/init.d/nginx -rwxr-xr-x. 1 root root 1133 Jul 1 04:16 /etc/init.d/nginx //將nginx設爲開機啓動 [root@localhost nginx-1.14.0]# cd /etc/init.d/ [root@localhost init.d]# chkconfig --add nginx [root@localhost init.d]# chkconfig --list Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off nginx 0:off 1:off 2:off 3:off 4:off 5:off 6:off php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off //新建nginx配置文件,原有的備份一下, [root@localhost init.d]# cd /usr/local/nginx/conf/ [root@localhost conf]# cp nginx.conf nginx.conf.bak [root@localhost conf]# vim nginx.conf //內容參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf user nginx nginx; 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; } } } //測試配置 [root@localhost conf]# /etc/init.d/nginx configtest 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@localhost 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 //啓動nginx [root@localhost conf]# /etc/init.d/nginx start Starting nginx (via systemctl): [ OK ] [root@localhost conf]# //驗證 [root@localhost conf]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 50119 root 6u IPv4 129891 0t0 TCP *:http (LISTEN) nginx 50120 nginx 6u IPv4 129891 0t0 TCP *:http (LISTEN) nginx 50121 nginx 6u IPv4 129891 0t0 TCP *:http (LISTEN) //或者 [root@localhost conf]# ss -nat |grep :80 LISTEN 0 128 *:80 *:* //或者 [root@localhost conf]# netstat -nltup |grep :80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 50119/nginx: master [root@localhost conf]# curl 127.0.0.1 <!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@localhost conf]# firewall-cmd --get-default-zone //public //[root@localhost conf]# firewall-cmd --zone=public --add-service=http //success //[root@localhost conf]# firewall-cmd --zone=public --list-service //ssh dhcpv6-client http //php解析測試 [root@localhost conf]# vim /usr/local/nginx/html/1.php [root@localhost conf]# cat /usr/local/nginx/html/1.php <?php echo "That's OK"; ?> [root@localhost conf]# curl 127.0.0.1/1.php That's OK
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-FPM
http://www.nowamagic.net/librarys/veda/detail/1319/ https://www.awaimai.com/371.html