mys sting 互联 pri linux 第 5 章 connected 但是 目录结构
第 1 章 web 网站服务介绍
1.1 常用来提供静态 Web 服务的软件
u
Apache
:这是中小型 Web 服务的主流,Web 服务器中的老大哥.u
Nginx
:大型网站 Web 服务的主流,曾经 Web 服务器中的初生牛犊,现已长大.Nginx 的分支 Tengine(http://tengine.taobao.org/)目前也在飞速发展.
u Lighttpd:这是一个不温不火的优秀 Web 软件,社区不活跃,静态解析效率很高.在 Nginx 流行前,
它是大并发静态业务的首选,国内百度贴吧,豆瓣等众多网站都有 Lighttpd 奋斗的身影.
1.2 常用来提供动态服务的软件
u PHP(FastCGI):大中小型网站都会使用,动态网页语言 PHP 程序的解析容器.它可配合 Apache 解析动态程序,不过,这里的 PHP 不是 FastCGI 守护进程模式,而是 mod_php5.so(module).也可配合 Nginx 解析动态程序,此时的 PHP 常用 FastCGI 守护进程模式提供服务.(.php)
u
Tomcat
:中小企业动态 Web 服务主流,互联网 Java 容器主流(如 jsp,do).u
Resin
:大型动态 Web 服务主流,互联网 Java 容器主流(如 jsp,do).:微软 windows 下的 Web 服务软件(如 asp,aspx)
u
IIS(Internet information services)
第 2 章 nginx 网站服务软件
2.1 Nginx 是什么
如果你听说或使用过 Apache 软件,那么很快就会熟悉 Nginx 软件,与 Apache 软件类似, Nginx("engine x")是一个开源的,支持高性能,高并发的 WWW 服务器和代理服务软件.
Nginx 因具
有高并发(特别是静态资源),占用系统资源少等特性
,且功能丰富逐渐流行起来.
在功能应用方面,Nginx 不但是一个优秀的 web 服务软件,还具有反向代理负载均衡功能和缓存服务功能.在反向代理负载均衡功能方面,它类似与大名鼎鼎的 LVS 负载均衡及 Haproxy 等专业的缓存服务软件.
Nginx 可以运行在 UNIX,Linux,BSD,Mac OS X,Solarls,以及 Microsoft Windows 等操作系统中.随着 Nginx 在国内很多大型网站中的稳定高效运行,近两年它也逐渐被越来越多的中小型网站所使用.当前流行的 Nginx Web 组合被称为 LNMP 或 LEMP(即 Linux Nginx MySql PHP),其中 LEMP 里的 E 取自 Nginx("engine x")
Nginx 的官网:http://nginx.org/
2.2 Nginx 的特色及优点
2.2.1 Nginx 的重要特性
u 可针对静态资源高速高并发访问及缓存
u 可使用反向代理加速,并且可进行数据缓存
u 具有简单负载均衡,节点健康检查和容错功能
u
支持远程 FastCGI 服务的缓存加速
u
支持 FastCGI,Uwsgi,SCGI,Memcached Servers 的加速和缓存
u
支持 SSL,TLS,SNI.
u 具有模块化的架构:过滤器包括 gzip 压缩,ranges 支持,chunked 响应,XSLT,SSI 及图像缩放等功能.在 SSI 过滤器中,一个包含多个 SSI 的页面,如果经由 FastCGI 或反向代理处理,可被并行处理.
2.2.2 Nginx HTTP 服务器的特色及优点(重要)
u 支持高并发:能支持几万并发连接(特别是静态小文件业务环境)
u 资源消耗少:在 3 万并发连接下,开启 10 个 Nginx 线程消耗的内存不到 200MB(ab webbenh)
u 可以做 HTTP 反向代理及加速缓存,即负载均衡功能,内置对 RS 节点服务器健康检查功能,这相当于专业的 Haproxy 软件或 LVS 的功能.
u
具备 Squid 等专业缓存软件等的缓存功能.
u
支持异步网络 I/O 事件模型 epoll(Linux 2.6+)(apache select)
2.3 Nginx 软件的主要企业功能应用
2.3.1 作为 Web 服务软件
Nginx 是一个支持高性能,高并发的 Web 服务软件,它具有很多优秀的特性,作为 Web 服务器,与 Apache 相比,Nginx 能够支持更多的并发连接访问,但占用的资源却更少,效率更高,在功能上也强大了很多,几乎不逊于 Apache.
2.3.2 反向代理或负载均衡服务
在反向代理或负载均衡服务方面,Nginx 可以作为 Web 服务,PHP 等动态服务及 Memcached 缓存的代理服务器,它具有类似专业代理软件(如 Haproxy)的功能,同时也是一个优秀的邮件代理服务软件(最早开发这个产品的目的之一就是作为邮件代理服务).Nginx 的代理功能在逐渐增强.
2.3.3 前端业务数据缓存服务
在 web 缓存服务方面,Nginx 可通过自身的 proxy_cache 模块实现类 Squid 等专业缓存软件的功能.
Nginx 的这三大功能(web 服务,反向代理或负载均衡服务,前端业务数据缓存服务)是国内使用 Nginx 的主要场景,特别是前两个.
2.4 为什么 Nginx 总体性能比 Apache 高
Nginx 使用最新的 epoll(Linux 2.6 内核)和 Kqueue(freebsd)异步网络 I/O 模型,
而 Apache 使用的是传统的 select 模型,处理大量连接的读写时,Apache 所采用的 select 网络 I/O 模型比较低效.
目前 Linux 先能够承受高并发访问的 Squid,Memcached 软件采用的都是 epoll 模型.
Apache select 和 Nginx epoll 的技术对比
指标 | select | epoll |
性能 | 随着连接数的增加性能急剧下降.处理成千上万并发连接数,性能很差 | 随着连接数的增加,性能基本上没有下降.处理成千上万并发连接时,性能很好. |
连接数 | 连接数有限制,处理的最大连接数不超过 1024,如果要处理的连接数超过 1024 个,则需要修改 FD_SETSIZE 宏,并重新编译 | 连接数无限制 |
内在处理机制 | 线性轮询 | 回调 callback |
开发复杂性 | 低 | 中 |
第 3 章 部署 nginx 软件(编译安装)
3.1 第一个里程碑:进行 nginx 软件下载
cd /server/tools
wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@web02 ~]# cd /server/tools/ [root@web02 tools]# wget http://nginx.org/download/nginx-1.12.2.tar.gz --2018-02-02 18:26:50-- http://nginx.org/download/nginx-1.12.2.tar.gz Resolving nginx.org... 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3, ... Connecting to nginx.org|206.251.255.63|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 981687 (959K) [application/octet-stream] Saving to: "nginx-1.12.2.tar.gz"100%[===================================================>] 981,687 23.8K/s in 43s 2018-02-02 18:27:40 (22.1 KB/s) - "nginx-1.12.2.tar.gz" saved [981687/981687] |
3.2 第二个里程碑:安装依赖软件
yum install -y pcre-devel openssl-devel
[root@web02 tools]# yum install -y pcre-devel openssl-devel |
3.3 第三个里程碑:解压软件,创建虚拟用户,进行编译安装(三步曲)
3.3.1 解压软件,创建虚拟用户
tar xf nginx-1.12.2.tar.gz
useradd -M -s /sbin/nologin www
[root@web02 tools]# tar xf nginx-1.12.2.tar.gz [root@web02 tools]# ls nginx-1.12.2 nginx-1.12.2.tar.gz |
[root@web02 tools]# useradd -M -s /sbin/nologin www [root@web02 tools]# id www uid=502(www) gid=502(www) groups=502(www) |
3.3.2 编译第一步:进行配置
./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
配置参数说明
--prefix=PATH | set installation prefix 指定软件程序安装的路径信息 |
--user=USER | set non-privileged user for worker processes 创建一个虚拟用户,用于管理 nginx 服务的 worker 进程 |
--group=GROUP | set non-privileged group for worker processes 创建一个虚拟用户组,用于管理 nginx 服务的 worker 进程 |
--with-http_ssl_module | enable ngx_http_ssl_module 让 nginx 服务可以支持 https 访问 |
--with-http_stub_status_module | enable ngx_http_stub_status_module 便于监控软件监视 nginx 服务运行状态 |
[root@web02 tools]# cd nginx-1.12.2 [root@web02 nginx-1.12.2]# pwd /server/tools/nginx-1.12.2 | |
[root@web02 nginx-1.12.2]# ./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module |
3.3.3 编译第二步:编译(翻译)
说明:编译过程实质是将各种程序语言转换为系统可以识别的二进制信息
make
[root@web02 nginx-1.12.2]# make make -f objs/Makefile make[1]: Entering directory `/server/tools/nginx-1.12.2' ...... |
3.3.4 编译第三步:编译安装
make install
[root@web02 nginx-1.12.2]# make install make -f objs/Makefile install make[1]: Entering directory `/server/tools/nginx-1.12.2' ...... |
3.4 第四个里程碑:创建程序目录软链接文件
ln -s /application/nginx-1.12.2/ /application/nginx
[root@web02 nginx-1.12.2]# ln -s /application/nginx-1.12.2/ /application/nginx [root@web02 nginx-1.12.2]# cd /application/ [root@web02 application]# ll total 4 lrwxrwxrwx 1 root root 26 Feb 2 19:30 nginx -> /application/nginx-1.12.2/ drwxr-xr-x 6 root root 4096 Feb 2 19:28 nginx-1.12.2 |
3.5 第五个里程碑:网站服务启动成功
/application/nginx/sbin/nginx
[root@web02 application]# /application/nginx/sbin/nginx |
3.6 第六个里程碑:检查测试
[root@web02 application]# ps -ef |grep nginx root 4781 1 0 19:32 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx www 4782 4781 0 19:32 ? 00:00:00 nginx: worker process root 4784 2022 1 19:32 pts/0 00:00:00 grep nginx [root@web02 application]# netstat -lntup |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4781/nginx |
worker 进程表示真正处理用户请求的进程
第 4 章 部署 nginx 软件可能会遇到的问题
1. 依赖包软件没有安装
2.
启动 Nginx 时如下报错 "nginx:[emerg]getpwnam("nginx")failed" 解答:这是因为没有对应的 Nginx 服务用户,执行 useradd nginx -s /sbin/nologin -M 创建 Nginx 用户即可.3. 如何查看 Nginx 编译时的参数?
解答:可采用如下命令查看:/application/nginx/sbin/nginx -V
[root@web02 application]# /application/nginx/sbin/nginx -V nginx version: nginx/1.12.2 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module |
第 5 章 nginx 软件程序目录结构
5.1 nginx 软件程序目录结构
[root@web02 nginx]# ll total 36 drwx------ 2 www root 4096 Feb 2 19:32 client_body_temp drwxr-xr-x 2 root root 4096 Feb 2 19:28 conf --- 配置文件目录 drwx------ 2 www root 4096 Feb 2 19:32 fastcgi_temp drwxr-xr-x 2 root root 4096 Feb 2 19:28 html --- 站点目录 drwxr-xr-x 2 root root 4096 Feb 2 19:32 logs --- 日志目录 drwx------ 2 www root 4096 Feb 2 19:32 proxy_temp drwxr-xr-x 2 root root 4096 Feb 2 19:28 sbin --- 保存服务命令目录 drwx------ 2 www root 4096 Feb 2 19:32 scgi_temp drwx------ 2 www root 4096 Feb 2 19:32 uwsgi_temp |
5.2 配置文件目录说明
mime.types --- 媒体资源类型文件
nginx.conf --- nginx 服务主配置文件
5.3 精简 nginx 配置文件信息
grep -Ev "#|^$" nginx.conf.default >nginx.conf
[root@web02 conf]# ll total 60 -rw-r--r-- 1 root root 1077 Feb 2 19:28 fastcgi.conf -rw-r--r-- 1 root root 1077 Feb 2 19:28 fastcgi.conf.default -rw-r--r-- 1 root root 1007 Feb 2 19:28 fastcgi_params -rw-r--r-- 1 root root 1007 Feb 2 19:28 fastcgi_params.default -rw-r--r-- 1 root root 2837 Feb 2 19:28 koi-utf -rw-r--r-- 1 root root 2223 Feb 2 19:28 koi-win -rw-r--r-- 1 root root 3957 Feb 2 19:28 mime.types -rw-r--r-- 1 root root 3957 Feb 2 19:28 mime.types.default -rw-r--r-- 1 root root 484 Feb 2 20:13 nginx.conf -rw-r--r-- 1 root root 2656 Feb 2 19:28 nginx.conf.default -rw-r--r-- 1 root root 636 Feb 2 19:28 scgi_params -rw-r--r-- 1 root root 636 Feb 2 19:28 scgi_params.default -rw-r--r-- 1 root root 664 Feb 2 19:28 uwsgi_params -rw-r--r-- 1 root root 664 Feb 2 19:28 uwsgi_params.default -rw-r--r-- 1 root root 3610 Feb 2 19:28 win-utf [root@web02 conf]# grep -Ev "#|^$" nginx.conf.default >nginx.conf |
[root@web02 conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } |
main 区块
event 区块
http 区块
server 区块(可以有多个)
location 区块(可以有多个)
扩展命令说明
:
vimdiff nginx.conf nginx.conf.default --- 比较两个文件之间配置区别
5.4 nginx 服务命令参数说明
/application/nginx/sbin/nginx -s reload --- -s 参数表示一个信号参数,后面可以指定一些信号信息
信号信息:reload stop
/application/nginx/sbin/nginx -t --- 检查配置文件语法信息
[root@web02 nginx-1.12.2]# /application/nginx/sbin/nginx -h nginx version: nginx/1.12.2 Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]Options: -?,-h : this help -v : show version and exit -V : show version and configure options then exit -t : test configuration and exit -T : test configuration, dump it and exit -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload -p prefix : set prefix path (default: /application/nginx-1.12.2/) -c filename : set configuration file (default: conf/nginx.conf) -g directives : set global directives out of configuration file |
5.5 实践配置编写配置文件
[root@web02 conf]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.wuhuang.org; location / { root html/www; index wuhuang.html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } |
编写静态访问页面文件信息
[root@web02 www]# pwd /application/nginx/html/www 站点目录 [root@web02 www]# cat wuhuang.html <html> <meta charset="utf-8"> <head> <title> 吾皇 </title> </head> <body> 吾皇 <table border=1> <tr> <td>01</td> <td> 吾皇 </td> </tr> <tr> <td>02</td> <td> 巴扎黑 </td> </tr> <tr> <td>03</td> <td> 糙汉 </td> </tr> </table> <a href="http://www.baidu.com"> <img src="wuhuang.jpg"/> </a> </body> </html> |
[root@web02 www]# ls wuhuang.html wuhuang.jpg |
修改本地的 hosts 文件
验证
nginx 网站服务
来源: http://www.bubuko.com/infodetail-2485032.html