linux C++ 通讯架构(一)nginx安装、目录、进程模型
nginx是C语言开发的,号称并发处理百万级别的TCP连接,稳定,热部署(运行时升级),高度模块化设计,可以用C++开发。
一、安装和目录
1.1 前提
epoll,linux内核版本为2.6或以上
gcc编译器,g++编译器
pcre库:函数库,支持解析正则表达式
zlib库:压缩解压功能
openssl库:ssl功能相关库,用于网站加密通讯
1.2 源码下载及目录结构简单认识
auto/:编译相关的脚本,可执行文件configure会用到这些脚本
cc/:检查编译器的脚本
lib/:检查依赖库的脚本
os/:检查操作系统类型的脚本
type/:检查平台类型的脚本
CHANGES:修复的bug,新增功能说明
CHANGES.ru:俄语版CHANGES
conf/:默认的配置文件
configure:编译ngnix之前必须先执行脚本以生成一些必要的中间文件
contrib/:脚本和工具,典型的是vim高亮工具
html/:欢迎界面和错误界面相关的文件
man/:帮助文件目录
src/:源代码目录
- core:核心代码
- event:event(事件)模块相关代码
- http:http(web服务)模块相关代码
- mail:邮件模块相关代码
- os:操作系统相关代码
- stream:流处理相关代码
1.3 nginx的编译安装
编译的第一步:用configure来进行编译之前的配置工作
下面的命令将会进行默认配置
- [root@JD nginx-1.16.1]# ./configure
configure的help命令可以查看configure的选项
- --prefix:指定最终安装到的目录,默认值 /usr/local/nginx
- --sbin-path:用来指定可执行文件目录,默认的是 sbin/nginx
- --conf-path:用来指定配置文件目录,默认的是 conf/nginx.conf
- --with开头的是默认不会编译到nginx的,可以用该命令包含进去
- --without则是默认编译进了nginx中的
执行完后,会多出来objs目录
- ngx_modules.c会决定哪些模块会真正被编译到可执行文件中
- Makefile:执行了configure脚本产生的编译规则文件,执行make命令时用到
用make编译
- [root@JD nginx-1.16.1]# make
ogjs目录中多处了nginx文件
这是nginx的可执行文件
用make命令进行安装
- [root@JD nginx-1.16.1]# make install
1.4 启动和简单使用
- [root@JD nginx-1.16.1]# whereis nginx
- nginx: /usr/local/nginx
进入上面目录的sbin目录
- [root@JD nginx-1.16.1]# cd /usr/local/nginx
- [root@JD nginx]# ll
- total 0
- drwx------ 2 nobody root 6 Nov 28 13:00 client_body_temp
- drwxr-xr-x 2 root root 333 Nov 28 12:56 conf
- drwx------ 2 nobody root 6 Nov 28 13:00 fastcgi_temp
- drwxr-xr-x 2 root root 40 Nov 28 12:56 html
- drwxr-xr-x 2 root root 58 Nov 28 13:00 logs
- drwx------ 2 nobody root 6 Nov 28 13:00 proxy_temp
- drwxr-xr-x 2 root root 19 Nov 28 12:56 sbin
- drwx------ 2 nobody root 6 Nov 28 13:00 scgi_temp
- drwx------ 2 nobody root 6 Nov 28 13:00 uwsgi_temp
- [root@JD nginx]# cd sbin
启动
- [root@JD sbin]# ./nginx
启动后访问服务器IP地址,网页显示:
上面的内容正是html目录下index.html文件
二、进程模型
nginx启动后查看进程
- [root@JD sbin]# ps -ef | grep nginx
- root 5789 10021 0 11:57 pts/0 00:00:00 grep --color=auto nginx
- root 24193 1 0 Nov28 ? 00:00:00 nginx: master process ./nginx
- nobody 24194 24193 0 Nov28 ? 00:00:01 nginx: worker process
两个进程,master进程属于root,worker进程属于nobody,两个进程是父子关系
上面的第三列是父进程ID,可以看到worker进程是被master进程通过fork()创造出来的
进程模型
通过一个master进程,一到多个worker进程这种工作机制对外服务的,这种工作机制保证了nginx稳定灵活运行
master进程是监控进程,不处理具体业务,专门用来管理worker进程
worker进程是用来对用户提供服务的
master进程和worker进程之间通讯,可以用信号,也可以用共享内存
worker进程一旦挂掉,那么master进程会立即fork()一个新的worker进程投入工作中去
调整worker进程数量
worker进程几个合适?多核计算机,让每个worker运行在一个单独的内核上,最大限度减少CPU进程切换成本,提高系统性能
查看processor数量
- [root@JD sbin]# grep -c processor /proc/cpuinfo
- 2
即是两核的,我们所说的多少核实际是有多少processor,一般是多少逻辑核
基础概念
CPU(Central Processing Unit):中央处理单元,CPU != 物理核,更 != 逻辑核。
物理核(physical core/processor):可以看的到的,真实的cpu核,有独立的电路元件以及L1,L2缓存,可以独立地执行指令。
逻辑核(logical core/processor):在同一个物理核内,逻辑层面的核。(比喻,像动画片一样,我们看到的“动画”,其实是一帧一帧静态的画面,24帧/s连起来就骗过了人类的眼睛,看起来像动起来一样。逻辑核也一样,物理核通过高速运算,让应用程序以为有两个cpu在运算)。
超线程(Hyper-threading, HT):超线程可以在一个逻辑核等待指令执行的间隔(等待从cache或内存中获取下一条指令),把时间片分配到另一个逻辑核。高速在这两个逻辑核之间切换,让应用程序感知不到这个间隔,误认为自己是独占了一个核。
关系:一个CPU可以有多个物理核。如果开启了超线程,一个物理核可以分成n个逻辑核,n为超线程的数量。Linux查看cpu信息
cat /proc/cpuinfo
- ...
- processor : 23 // 逻辑核编号
- cpu MHz : 2299.863 // CPU主频
- cache size : 15360 KB // L2 cache大小
- physical id : 1 // CPU编号
- siblings : 12 // 一颗CPU中有多少逻辑核
- core id : 5 // CPU的物理核编号
- cpu cores : 6 // 一颗CPU中有多少物理核
- ...
针对于上边的这个服务器,我们说,它有2颗CPU,每颗CPU中有6个物理核,CPU开启了超线程,每颗CPU中有12个逻辑核。所以,这台服务器共有12个物理核,24个逻辑核。我们通常意义上说的24核,是指逻辑核是24个。
作者:rockops
链接:https://www.jianshu.com/p/4233b080048c
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
修改nginx的worker数量
- [root@JD nginx]# cd conf
- [root@JD conf]# pwd
- /usr/local/nginx/conf
- [root@JD conf]# ll
- total
- -rw-r--r-- root root Nov : fastcgi.conf
- -rw-r--r-- root root Nov : fastcgi.conf.default
- -rw-r--r-- root root Nov : fastcgi_params
- -rw-r--r-- root root Nov : fastcgi_params.default
- -rw-r--r-- root root Nov : koi-utf
- -rw-r--r-- root root Nov : koi-win
- -rw-r--r-- root root Nov : mime.types
- -rw-r--r-- root root Nov : mime.types.default
- -rw-r--r-- root root Nov : nginx.conf
- -rw-r--r-- root root Nov : nginx.conf.default
- -rw-r--r-- root root Nov : scgi_params
- -rw-r--r-- root root Nov : scgi_params.default
- -rw-r--r-- root root Nov : uwsgi_params
- -rw-r--r-- root root Nov : uwsgi_params.default
- -rw-r--r-- root root Nov : win-utf
- [root@JD conf]# vi nginx.conf
把默认的1改为processor的数量
重新启动
- [root@JD sbin]# ./nginx -s stop
- [root@JD sbin]# ./nginx
- [root@JD sbin]# ps -ef | grep -v grep | grep nginx
- root : ? :: nginx: master process ./nginx
- nobody : ? :: nginx: worker process
- nobody : ? :: nginx: worker process
已经有两个 worker 进程了
nginx重载配置文件
查看帮助命令
- [root@JD sbin]# ./nginx -?
- nginx version: nginx/1.16.
- 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: /usr/local/nginx/)
- -c filename : set configuration file (default: conf/nginx.conf)
- -g directives : set global directives out of configuration file
重载配置文件
修改配置文件后可重载
- [root@JD sbin]# ./nginx -s reload
nginx 可以热升级,热回滚
linux C++ 通讯架构(一)nginx安装、目录、进程模型的更多相关文章
- linux系统下nginx安装目录和nginx.conf配置文件目录
linux系统下nginx安装目录和nginx.conf配置文件目录 1.查看nginx安装目录 输入命令 # ps -ef | grep nginx 返回结果包含安装目录 root 26 ...
- 第一次项目上Linux服务器(六:Nginx安装及相关命令(转))
1.下载nginx 方法一 wget http://nginx.org/download/nginx-1.11.6.tar.gz 方法二 http://nginx.org/en/download.ht ...
- Nginx安装目录详解
Nginx安装目录详解 1. 查看有关nginx的所有目录列表,输入命令 rpm -ql nginx 可以查看有关nginx目录信息,但是注意 这种命令只能是在基于yum安装的方式才可以. 2. 下 ...
- nginx安装目录文件
nginx安装目录 conf 存放nginx的配置文件 在修改配置文件nginx.conf之前,一般先备份cp nginx.conf nginx.conf.bak html 存放前端文件的 默认带有首 ...
- Nginx的内部(进程)模型
nginx是以多进程的方式来工作的,当然nginx也是支持多线程的方式的,只是我们主流的方式还是多进程的方式,也是nginx的默认方式.nginx采用多进程的方式有诸多好处. (1)nginx在启动后 ...
- Linux下查看Nginx安装目录、版本号信息?
Linux环境下,怎么确定Nginx是以那个config文件启动的? 输入命令行: ps -ef | grep nginx 摁回车,将出现如下图片: master process 后面的就是 ngi ...
- Linux下怎么确定Nginx安装目录
linux环境下,怎么确定nginx是以那个config文件启动的? 输入命令行: ps -ef | grep nginx 摁回车,将出现如下图片: master process 后面的就是 ngi ...
- CentOS 6.5玩转自制Linux、远程登录及Nginx安装测试
前言 系统定制在前面的博文中我们就有谈到过了,不过那个裁减制作有简单了点,只是能让系统跑起来而,没有太多的功能,也没的用户登录入口,而这里我们将详细 和深入的来谈谈Linux系统的详细定制过程和 ...
- 二、nginx 安装目录详解
rpm -ql nginx 路径 类型 介绍 /etc/logrotate.d/nginx 配置文件 Nginx 日志轮转,用于logrotate服务日志切割 /etc/nginx /etc/ng ...
随机推荐
- Linux_OpenSSH远程连接
目录 目录 SSH server SSH服务属性 SSH协议执行原理 SSH连接方式 ssh Commands scp commands sftp commands SSH在生产环境中的使用 系统间备 ...
- 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_03.jdbc操作数据库的问题分析
分析一段代码,传统的jdbc的功能,最终要实现的功能就是sql语句.
- css让字体细长
transform: scale(1,3); -ms-transform: scale(1,3); -webkit-transform: scale(1,3); -moz-transform: sca ...
- Powershell 脚本输出前十条消耗内存的进程到excel
# create new excel instance $objExcel = New-Object -comobject Excel.Application $objExcel.Visible = ...
- Java实验3与第五周总结
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码.结果截图.) •统计该字符串中字母s出现的次数. •统计该字符串中子串" ...
- sql server监控工具
图形化监控工具 [1]toad toad官网介绍:https://www.quest.com/cn-zh/products/toad-for-sql-server/ 破解版网上搜索下载使用:https ...
- spring-第N篇整合SSM,即Mybatis+Spring+Spring MVC
1.Mybatis的配置使用 1>Jar包:mybatis-3.4.5.jar.mysql-connector-6.0.2或者ojdbc6-11.2.0.4.jar. 2>编写conf.x ...
- merge into 导致序列跳号
For each row merged by a MERGE statement. The reference to NEXTVAL can appear in the merge_insert_cl ...
- Windows下图文详解Mongodb安装及配置
这两天接触了MongoDB数据库,发现和mysql数据库还是有很大差别的,同时使用前的配置看起来有些繁杂,踩过不少坑,其实只要一步一步搞清了,并不难. 接下来,我就整理下整个安装及配置过程. 安装的M ...
- Javascript的是三种字符串连接方式
第一种:用连接符“+”连接字符串 str="a"; str+="b"; 这种方法相对以下两种,最便捷快速.建议100字符以下的连接使用这种连接方式. 第二种:以 ...