学习笔记0514----LNMP架构

2020-11-24 10:28:28 浏览数 (1)

LNMP架构

预习笔记

这里是引用 12.1 LNMP架构介绍 12.2 MySQL安装 12.3/12.4 PHP安装 12.5 Nginx介绍 12.6 Nginx安装 扩展 Nginx为什么比Apache Httpd高效:原理篇 http://www.toxingwang.com/linux-unix/linux-basic/1712.html https://www.zhihu.com/question/64727674 apache和nginx工作原理比较 http://www.server110.com/nginx/201402/6543.html 概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM https://www.awaimai.com/371.html

1.LNMP架构介绍

LNMP架构和LAMP架构挺像的,只不过一个用的是Apache,一个用的是Nginx。LNMP就是Linux Nginx MySQL PHP,Nginx和Apache一样都是web服务器。

有一点不同的是在LNMP结构里php会启动一个服务:php-fpm,而LANP中php只是作为Apache的一个模块存在。Nginx会把用户的动态请求交给php服务去处理,这个php服务就会去和数据库进行交互。用户的静态请求Nginx会直接处理,Nginx处理静态请求的速度要比Apache快很多性能上要好,所以Apache和Nginx在动态请求处理上区别不大,但如果是静态请求处理的话就会明显发现Nginx要快于Apache,而且Nginx能承受的并发量要比Apache大,可以承受好几万的并发量,所以大一些的网站都会使用Nginx作为web服务器。

2.MySQL安装

MySQL的安装和LAMP中的mysql安装方法是一样的,在这我们在来安装一次。这儿使用的mysql包也是已经编译好的二进制包。

2.1下载解压mysql压缩包文件

代码语言:javascript复制
[root@linux-02 src]#  wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
## 下载过程省略 
[root@linux-02 src]# tar -zxvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
## 解压过程省略

2.2 mysql初始化

mysql初始化之前需要创建一个mysql的用户,这次在另外一台机器上安装,安装了两个依赖包:perl-Data-Dumper 和 libaio

代码语言:javascript复制
[root@linux-02 src]# cp -r  mysql-5.6.43-linux-glibc2.12-x86_64/  /usr/local/mysql
[root@linux-02 src]# ls /usr/local/mysql/
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files
[root@linux-02 src]# cd /usr/local/mysql/
[root@linux-02 mysql]# ./scripts/mysql_install_db  --user=mysql --datadir=/data/mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper


[root@linux-02 mysql]# yum install -y perl-Data-Dumper.x86_64
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                          | 3.6 kB  00:00:00     
extras                                                                                                        | 3.4 kB  00:00:00     
mariadb-main                                                                                                  | 2.9 kB  00:00:00     
mariadb-maxscale                                                                                              | 2.4 kB  00:00:00     
mariadb-tools                                                                                                 | 2.9 kB  00:00:00     
updates                                                                                                       | 3.4 kB  00:00:00     
updates/7/x86_64/primary_db                                                                                   | 4.2 MB  00:00:07     
正在解决依赖关系
--> 正在检查事务
---> 软件包 perl-Data-Dumper.x86_64.0.2.145-3.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=====================================================================================================================================
 Package                               架构                        版本                              源                         大小
=====================================================================================================================================
正在安装:
 perl-Data-Dumper                      x86_64                      2.145-3.el7                       base                       47 k

事务概要
=====================================================================================================================================
安装  1 软件包

总下载量:47 k
安装大小:97 k
Downloading packages:
perl-Data-Dumper-2.145-3.el7.x86_64.rpm                                                                       |  47 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : perl-Data-Dumper-2.145-3.el7.x86_64                                                                              1/1 
  验证中      : perl-Data-Dumper-2.145-3.el7.x86_64                                                                              1/1 

已安装:
  perl-Data-Dumper.x86_64 0:2.145-3.el7                                                                                              

完毕!
[root@linux-02 mysql]# ./scripts/mysql_install_db  --user=mysql --datadir=/data/mysql
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory


[root@linux-02 mysql]# yum install -y  libaio
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 libaio.x86_64.0.0.3.109-13.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=====================================================================================================================================
 Package                       架构                          版本                                  源                           大小
=====================================================================================================================================
正在安装:
 libaio                        x86_64                        0.3.109-13.el7                        base                         24 k

事务概要
=====================================================================================================================================
安装  1 软件包

总下载量:24 k
安装大小:38 k
Downloading packages:
libaio-0.3.109-13.el7.x86_64.rpm                                                                              |  24 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : libaio-0.3.109-13.el7.x86_64                                                                                     1/1 
  验证中      : libaio-0.3.109-13.el7.x86_64                                                                                     1/1 

已安装:
  libaio.x86_64 0:0.3.109-13.el7                                                                                                     

完毕!




[root@linux-02 mysql]# ./scripts/mysql_install_db  --user=mysql --datadir=/data/mysql
Installing MySQL system tables...2019-05-13 20:48:51 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-05-13 20:48:51 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-05-13 20:48:51 0 [Note] ./bin/mysqld (mysqld 5.6.43) starting as process 40002 ...
2019-05-13 20:48:51 40002 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-05-13 20:48:51 40002 [Note] InnoDB: The InnoDB memory heap is disabled
2019-05-13 20:48:51 40002 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-05-13 20:48:51 40002 [Note] InnoDB: Memory barrier is not used
2019-05-13 20:48:51 40002 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-05-13 20:48:51 40002 [Note] InnoDB: Using Linux native AIO
2019-05-13 20:48:51 40002 [Note] InnoDB: Using CPU crc32 instructions
2019-05-13 20:48:51 40002 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-05-13 20:48:51 40002 [Note] InnoDB: Completed initialization of buffer pool
2019-05-13 20:48:51 40002 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2019-05-13 20:48:51 40002 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2019-05-13 20:48:51 40002 [Note] InnoDB: Database physically writes the file full: wait...
2019-05-13 20:48:51 40002 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2019-05-13 20:48:51 40002 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2019-05-13 20:48:51 40002 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2019-05-13 20:48:51 40002 [Warning] InnoDB: New log files created, LSN=45781
2019-05-13 20:48:51 40002 [Note] InnoDB: Doublewrite buffer not found: creating new
2019-05-13 20:48:51 40002 [Note] InnoDB: Doublewrite buffer created
2019-05-13 20:48:51 40002 [Note] InnoDB: 128 rollback segment(s) are active.
2019-05-13 20:48:51 40002 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-05-13 20:48:51 40002 [Note] InnoDB: Foreign key constraint system tables created
2019-05-13 20:48:51 40002 [Note] InnoDB: Creating tablespace and datafile system tables.
2019-05-13 20:48:51 40002 [Note] InnoDB: Tablespace and datafile system tables created.
2019-05-13 20:48:51 40002 [Note] InnoDB: Waiting for purge to start
2019-05-13 20:48:51 40002 [Note] InnoDB: 5.6.43 started; log sequence number 0
2019-05-13 20:48:51 40002 [Note] Binlog end
2019-05-13 20:48:51 40002 [Note] InnoDB: FTS optimize thread exiting.
2019-05-13 20:48:51 40002 [Note] InnoDB: Starting shutdown...
2019-05-13 20:48:53 40002 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK

Filling help tables...2019-05-13 20:48:53 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-05-13 20:48:53 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-05-13 20:48:53 0 [Note] ./bin/mysqld (mysqld 5.6.43) starting as process 40024 ...
2019-05-13 20:48:53 40024 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-05-13 20:48:53 40024 [Note] InnoDB: The InnoDB memory heap is disabled
2019-05-13 20:48:53 40024 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-05-13 20:48:53 40024 [Note] InnoDB: Memory barrier is not used
2019-05-13 20:48:53 40024 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-05-13 20:48:53 40024 [Note] InnoDB: Using Linux native AIO
2019-05-13 20:48:53 40024 [Note] InnoDB: Using CPU crc32 instructions
2019-05-13 20:48:53 40024 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-05-13 20:48:53 40024 [Note] InnoDB: Completed initialization of buffer pool
2019-05-13 20:48:53 40024 [Note] InnoDB: Highest supported file format is Barracuda.
2019-05-13 20:48:53 40024 [Note] InnoDB: 128 rollback segment(s) are active.
2019-05-13 20:48:53 40024 [Note] InnoDB: Waiting for purge to start
2019-05-13 20:48:53 40024 [Note] InnoDB: 5.6.43 started; log sequence number 1625977
2019-05-13 20:48:54 40024 [Note] Binlog end
2019-05-13 20:48:54 40024 [Note] InnoDB: FTS optimize thread exiting.
2019-05-13 20:48:54 40024 [Note] InnoDB: Starting shutdown...
2019-05-13 20:48:55 40024 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  ./bin/mysqladmin -u root password 'new-password'
  ./bin/mysqladmin -u root -h linux-02 password 'new-password'

Alternatively you can run:

  ./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

  cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

WARNING: Found existing config file ./my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as ./my-new.cnf,
please compare it with your file and take the changes you need.

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@linux-02 mysql]# echo $?
0

2.3 修改mysql的启动脚本和配置文件

2.3.1 修改mysql启动脚本
代码语言:javascript复制
[root@linux-02 src]# cd  /usr/local/mysql/
[root@linux-02 mysql]# ls
bin  COPYING  docs  include  lib  man  README  share  support-files
[root@linux-02 mysql]# cp /usr/local/mysql/support-files/mysql.server   /etc/init.d/mysqld
[root@linux-02 mysql]# vim /etc/init.d/mysqld
……
basedir=/usr/local/mysql
datadir=/data/mysql
……
2.3.2 修改mysql配置文件
代码语言:javascript复制
[root@linux-02 mysql]# vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
 basedir = /usr/local/mysql
 datadir = /data/mysql
# port = .....
# server_id = .....
 socket = /tmp/mysql.sock

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transact
ions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
2.3.3 mysql服务启动
代码语言:javascript复制
[root@linux-02 mysql]# chmod 755 /etc/init.d/mysqld 
[root@linux-02 mysql]# mkdir /data
[root@linux-02 mysql]# chkconfig --add mysqld
[root@linux-02 mysql]# chconfig mysqld on
[root@linux-02 mysql]# service mysqld start
Starting MySQL.Logging to '/data/mysql/linux-02.err'.
. SUCCESS!

3.php安装

由于我是在新的虚拟机上进行的操作,安装php的时候需要安装很多依赖包,有些依赖包是需要yum扩展源的支持。

3.1 安装扩展源

代码语言:javascript复制
## 安装一个阿里云的源 ##
[root@linux-02 php-5.6.39] wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

## 安装yum的一个扩展源epel ##
[root@linux-02 php-5.6.39] yum install epel-release.noarch 

[root@linux-02 php-5.6.39] yum clean all   //清除yum缓存

[root@linux-02 php-5.6.39] yum makecache   //服务器的包信息下载到本地电脑缓存起来

3.2 安装php所必须的依赖包

代码语言:javascript复制
[root@linux-02 php-5.6.39] yum install -y  libxml2-devel  openssl-devel libcurl-devel  libjpeg-devel  libpng-devel  freetype-devel  libmcrypt-deve

安装过程略过

3.3 安装php-fpm

代码语言:javascript复制
[root@linux-02 php-5.6.39] useradd php-fpm
[root@linux-02 php-5.6.39] ./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

[root@linux-02 php-5.6.39] make

[root@linux-02 php-5.6.39] make install

3.4 php-fpm的配置文件

此处我们需要配置一个php.ini 和php-fpm.conf。 两个配置文件,这两个配置文件分别的含义是:php.ini是所以php模式中必须的配置文件,php-fpm.conf是PHP-FPM特有的配置文件。 两者的区别是,php-fpm.conf是PHP-FPM进程管理器的配置文件,php.ini是PHP解析器的配置文件。

php-fpm的配置文件解析 [global]=定义全局参数 [www]=自定义模块 listen = 监听的地址 或者 listen = 127.0.0.1:9000 (port默认为9000,也可以更改) listen.mode = 666 用来定义listen = /tmp/php-fcgi.sock的权限,只有这个sock打开的情况下才生效!

代码语言:javascript复制
[root@linux-02 php-5.6.39]# cd /usr/local/php-fpm/
[root@linux-02 php-fpm]# ls
bin  etc  include  lib  php  sbin  var

[root@linux-02 php-fpm] vim etc/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.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

[root@linux-02 php-fpm]# cp /usr/local/src/php-5.6.39/php.ini-development  /usr/local/php-fpm/etc/php.ini
[root@linux-02 php-fpm]# ls etc/
pear.conf  php-fpm.conf  php-fpm.conf.default  php.ini

3.5 编译php-fpm的启动脚本

代码语言:javascript复制
[root@linux-02 sbin]# cd /usr/local/src/php-5.6.39/
[root@linux-02 php-5.6.39]# ls
acinclude.m4      footer              Makefile.gcov        README.GIT-RULES                  run-tests.php
aclocal.m4        generated_lists     Makefile.global      README.input_filter               sapi
build             genfiles            Makefile.objects     README.MAILINGLIST_RULES          scripts
buildconf         header              makerpm              README.md                         server-tests-config.php
buildconf.bat     include             missing              README.namespaces                 server-tests.php
CODING_STANDARDS  INSTALL             mkinstalldirs        README.NEW-OUTPUT-API             snapshot
config.guess      install-sh          modules              README.PARAMETER_PARSING_API      stamp-h.in
config.log        libs                netware              README.REDIST.BINS                stub.c
config.nice       libtool             NEWS                 README.RELEASE_PROCESS            tests
config.status     LICENSE             pear                 README.SELF-CONTAINED-EXTENSIONS  travis
config.sub        ltmain.sh           php5.spec            README.STREAMS                    TSRM
configure         main                php5.spec.in         README.SUBMITTING_PATCH           UPGRADING
configure.in      makedist            php.gif              README.TESTING                    UPGRADING.INTERNALS
CREDITS           Makefile            php.ini-development  README.TESTING2                   vcsclean
ext               Makefile.frag       php.ini-production   README.UNIX-BUILD-SYSTEM          win32
EXTENSIONS        Makefile.fragments  README.EXT_SKEL      README.WIN32-BUILD-SYSTEM         Zend
[root@linux-02 php-5.6.39]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@linux-02 php-fpm]# chmod 755 /etc/init.d/php-fpm

3.6 启动php-fpm服务

代码语言:javascript复制
[root@linux-02 php-fpm]# chkconfig --add php-fpm
[root@linux-02 php-fpm]# chkconfig  php-fpm  on
[root@linux-02 php-fpm]# chkconfig  --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
php-fpm        	0:关	1:关	2:开	3:开	4:开	5:开	6:关


[root@linux-02 php-fpm]# service php-fpm start
Starting php-fpm  done
[root@linux-02 php-fpm]# ps aux | grep php-fpm
root      63019  0.1  0.2 125924  4964 ?        Ss   22:49   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm   63020  0.0  0.2 125924  4720 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63021  0.0  0.2 125924  4720 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63022  0.0  0.2 125924  4720 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63023  0.0  0.2 125924  4720 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63024  0.0  0.2 125924  4724 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63025  0.0  0.2 125924  4724 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63026  0.0  0.2 125924  4724 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63027  0.0  0.2 125924  4724 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63028  0.0  0.2 125924  4724 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63029  0.0  0.2 125924  4724 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63030  0.0  0.2 125924  4728 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63031  0.0  0.2 125924  4728 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63032  0.0  0.2 125924  4728 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63033  0.0  0.2 125924  4728 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63034  0.0  0.2 125924  4728 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63035  0.0  0.2 125924  4728 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63036  0.0  0.2 125924  4728 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63037  0.0  0.2 125924  4728 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63038  0.0  0.2 125924  4728 ?        S    22:49   0:00 php-fpm: pool www
php-fpm   63039  0.0  0.2 125924  4728 ?        S    22:49   0:00 php-fpm: pool www
root      63041  0.0  0.0 112724   988 pts/1    R    22:49   0:00 grep --color=auto php-fpm

[root@linux-02 php-fpm]# ll /tmp/php-fcgi.sock 
srw-rw-rw-. 1 root root 0 5月  13 22:49 /tmp/php-fcgi.sock

4.nginx安装

nginx特点:体积小、处理能力强、支持并发高、可扩展性好 Nginx应用场景:web服务、反向代理、负载均衡 Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并 Nginx核心 lua(开发语言)相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考http://jinnianshilongnian.iteye.com/blog/2280928

4.1 下载解压nginx

代码语言:javascript复制
[root@linux-02 src]# wget  http://nginx.org/download/nginx-1.16.0.tar.gz
--2019-05-13 23:07:19--  http://nginx.org/download/nginx-1.16.0.tar.gz
正在解析主机 nginx.org (nginx.org)... 95.211.80.227, 2001:1af8:4060:a004:21::e3
正在连接 nginx.org (nginx.org)|95.211.80.227|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:http://112.17.13.213/files/B211000004FFE6BD/nginx.org/download/nginx-1.16.0.tar.gz [跟随至新的 URL]
--2019-05-13 23:07:28--  http://112.17.13.213/files/B211000004FFE6BD/nginx.org/download/nginx-1.16.0.tar.gz
正在连接 112.17.13.213:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1032345 (1008K) [application/octet-stream]
正在保存至: “nginx-1.16.0.tar.gz”

100%[===========================================================================================>] 1,032,345   --.-K/s 用时 0.1s    

2019-05-13 23:07:28 (6.72 MB/s) - 已保存 “nginx-1.16.0.tar.gz” [1032345/1032345])

[root@linux-02 src]# tar -zxvf nginx-1.16.0.tar.gz 
解压省略

4.2 安装nginx

代码语言:javascript复制
[root@linux-02 src]# cd nginx-1.16.0/
[root@linux-02 nginx-1.16.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@linux-02 nginx-1.16.0]# ./configure --prefix=/usr/local/nginx
安装省略
[root@linux-02 nginx-1.16.0]# make
安装省略
[root@linux-02 nginx-1.16.0]# make install
安装省略

4.3 nginx目录解析

代码语言:javascript复制
[root@linux-02 nginx-1.16.0]# ls /usr/local/nginx/
conf  html  logs  sbin

目录

解析

conf

nginx配置文件

html

主页样例文件

logs

站点日志

sbin

核心进程文件

4.4 Nginx的配置文件

代码语言:javascript复制
[root@linux-02 nginx-1.16.0]# cd /usr/local/nginx/
[root@linux-02 nginx]# ls 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@linux-02 nginx]# mv conf/nginx.conf  /usr/local/nginx/conf/nginx.conf.bak
[root@linux-02 nginx]# vim conf/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;定义子进程的数量 worker_rlimit_nofile 51200;最多可以打开多少个文件 worker_connections 6000;允许最大的连接数 server; 下面对应的就是虚拟主机配置 server_name localhost;定义网站的域名 root /usr/local/nginx/html;定义网站的根目录 location ~ .php$;配置解析PHP fastcgi_pass unix:/tmp/php-fcgi.sock;监听端口或者监听socket,通过此命令去执行 fastcgi_pass 127.0.0.1:9000;(或者携程这种方式,服务器IP地址 端口)

4.5 Nginx的启动脚本

代码语言:javascript复制
[root@linux-02 nginx-1.16.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@linux-02 nginx-1.16.0]# chmod  755 /etc/init.d/nginx

4.6 nginx启动

代码语言:javascript复制
[root@linux-02 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@linux-02 sbin]# cd ..
[root@linux-02 nginx]# 
[root@linux-02 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@linux-02 nginx]# chkconfig  --add nginx
[root@linux-02 nginx]# service nginx start
Starting nginx (via systemctl):                            [  确定  ]
[root@linux-02 nginx]# 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@linux-02 nginx]# 

4.7 nginx测试php解析

代码语言:javascript复制
[root@linux-02 nginx]# vim html/1.php
<?php
echo "this is nginx";

[root@linux-02 nginx]# curl localhost/1.php
this is nginx[root@linux-02 nginx]# 

0 人点赞