Nginx(一):安装与常用命令
简介
Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,特点是占有内存少,并发能 力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,在高连接并发的情况下,Nginx是Apache服务器不错的替代品。中国大陆使用nginx 网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
nginx 原理与参数

master-workers 的机制的好处
首先,对于每个 worker 进程来说,独立的进程,不需要加锁,所以省掉了锁带来的开销, 同时在编程以及问题查找时,也会方便很多。其次,采用独立的进程,可以让互相之间不会 影响,一个进程退出后,其它进程还在工作,服务不会中断,master 进程则很快启动新的 worker 进程。当然,worker 进程的异常退出,肯定是程序有 bug 了,异常退出,会导致当 前 worker 上的所有请求失败,不过不会影响到所有请求,所以降低了风险。
设置 worker
Nginx 同 redis 类似都采用了 io 多路复用机制,每个 worker 都是一个独立的进程,但每个进 程里只有一个主线程,通过异步非阻塞的方式来处理请求, 即使是千上万个请求也不在话 下。每个 worker 的线程可以把一个 cpu 的性能发挥到极致。所以 worker 数和服务器的 cpu 数相等是最为适宜的。设少了会浪费 cpu,设多了会造成 cpu 频繁切换上下文带来的损耗。
连接数 worker_connection
这个值是表示每个 worker 进程所能建立连接的最大值,所以,一个 nginx 能建立的最大连接数,应该是 worker_connections * worker_processes。当然,这里说的是最大连接数,对于 HTTP 请 求 本 地 资 源 来 说 , 能 够 支 持 的 最 大 并 发 数 量 是 worker_connections * worker_processes,如果是支持 http1.1 的浏览器每次访问要占两个连接,所以普通的静态访 问最大并发数是: worker_connections * worker_processes /2,而如果是 HTTP 作 为反向代理来说,最大并发数量应该是 worker_connections * worker_processes/4。因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。
安装
初次安装
[root@localhost ~]# yum install -y nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
No package nginx available.
Error: Nothing to do
原因是nginx位于第三方的yum源里面,而不在centos官方yum源里面
解决方案
#使用 yum-config-manager 软件包命令
[root@localhost ~]# yum install -y yum-utils
#安装wget
[root@localhost ~]#yum install -y wget
#切换到repo配置文件目录
[root@localhost ~]# cd /etc/yum.repos.d/
#备份CentOs-Base.repo源文件
[root@localhost yum.repos.d]# mv CentOs-Base.repo CentOs-Base.repo.bak
#网络下载centos官方和扩展.repo文件
[root@localhost yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
#安装epel-release
[root@localhost yum.repos.d]# yum install -y epel-release
[root@localhost yum.repos.d]#mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
[root@localhost yum.repos.d]# wget http://mirrors.aliyun.com/repo/epel-7.repo
#清理软件源缓存
[root@localhost yum.repos.d]# yum clean all
#重建缓存
[root@localhost yum.repos.d]# yum makecache
#可选选项,更新系统 可选操作
[root@localhost yum.repos.d]# yum update
# 也可以使用yum-config-manager --add-repo 命令。进行以下操作推荐备份原.repo文件。yum-config-manager命令必要条件: yum install -y yum-utils
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/repo/epel-7.repo
二次安装
[root@localhost ~]# yum install -y nginx
[root@localhost ~]# find / -name 'nginx'
/etc/logrotate.d/nginx
/etc/nginx
/var/lib/nginx
/var/log/nginx
/usr/sbin/nginx
/usr/lib64/perl5/vendor_perl/auto/nginx
/usr/lib64/nginx
/usr/share/nginx
校验
# 启动
[root@localhost ~]# cd /usr/sbin/
[root@localhost sbin]# ./nginx
# 查看状态
[root@localhost sbin]# ps -ef|grep nginx
root 40783 1 0 21:25 ? 00:00:00 nginx: master process ./nginx
nginx 40784 40783 0 21:25 ? 00:00:00 nginx: worker process
nginx 40785 40783 0 21:25 ? 00:00:00 nginx: worker process
nginx 40786 40783 0 21:25 ? 00:00:00 nginx: worker process
nginx 40787 40783 0 21:25 ? 00:00:00 nginx: worker process
停止
[root@localhost sbin]# ./nginx -s stop
重载
[root@localhost sbin]# ./nginx -s reload
# 重载指定配置文件
[root@localhost sbin]# ./nginx -s reload -c /etc/nginx/nginx.conf
第二种启动方式
# 启动
[root@localhost ~]# systemctl start nginx
# 查看状态
[root@localhost ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2020-04-04 21:52:43 CST; 50s ago
Process: 42188 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 42185 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 42183 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 42191 (nginx)
CGroup: /system.slice/nginx.service
├─42191 nginx: master process /usr/sbin/nginx
├─42192 nginx: worker process
├─42193 nginx: worker process
├─42194 nginx: worker process
└─42195 nginx: worker process
Apr 04 21:52:43 localhost systemd[1]: Starting The nginx HTTP and reverse proxy server...
Apr 04 21:52:43 localhost nginx[42185]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Apr 04 21:52:43 localhost nginx[42185]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr 04 21:52:43 localhost systemd[1]: Started The nginx HTTP and reverse proxy server.
# 停止
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Apr 04 21:48:37 localhost nginx[41953]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr 04 21:48:37 localhost systemd[1]: Started The nginx HTTP and reverse proxy server.
Apr 04 21:49:40 localhost systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Apr 04 21:49:40 localhost systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Apr 04 21:52:43 localhost systemd[1]: Starting The nginx HTTP and reverse proxy server...
Apr 04 21:52:43 localhost nginx[42185]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Apr 04 21:52:43 localhost nginx[42185]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr 04 21:52:43 localhost systemd[1]: Started The nginx HTTP and reverse proxy server.
Apr 04 21:54:12 localhost systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Apr 04 21:54:12 localhost systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Nginx(一):安装与常用命令的更多相关文章
- Nginx编译安装及常用命令
一个执着于技术的公众号 前言 前面我们已经了解Nginx基础入门知识,今天就带大家一起学习下Nginx编译安装部署 准备工作 一台linux机器(本次实验以CentOS 7.5为例) 到Nginx官方 ...
- ios开发环境配置及cordova安装与常用命令
一.ios开发环境配置 1.首先要有台Mac Book,如果有Mac Book,跳过步骤2.3.4,如果没有,执行步骤2.3.4: 2.下载并安装VMware Workstation,最好是下最新版本 ...
- Git安装以及常用命令(图文详解)
**Git安装以及常用命令** 1.下载安装Git,傻瓜式安装相信大家都会. 官网下载地址:[https://git-scm.com/downloads] 2.Git基本操作 (1)git --ver ...
- linux基础学习之软件安装以及常用命令
linux基础学习之软件安装以及常用命令 调用中央仓库: yum install wget 然后下载nodejs: wget https://nodejs.org/dist/v10.14.2/node ...
- Git安装和常用命令
Git是目前世界上最先进的分布式版本控制系统!!! Git能自动帮我们记录每次文件的改动,还可以让同事协作编辑. 接下来,简单的介绍下Git的安装和常用命令: Git安装: 1.Windows系统,进 ...
- RabbitMQ入门教程(一):安装和常用命令
原文:RabbitMQ入门教程(一):安装和常用命令 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn ...
- 记录redis安装及常用命令
Redis安装及常用命令 一.安装 1.下载,解压,进入redis解压目录,make. make PREFIX=目录/redis install :安装到指定目录文件名为redis. 2.将解压目录里 ...
- 实验 1 Linux 系统的安装和常用命令
实验 1 Linux 系统的安装和常用命令 (题目) 一.实验目的 (1)掌握 Linux 虚拟机的安装方法.Spark 和 Hadoop 等大数据软件在 Linux 操作系统 上运行可以发挥最佳性能 ...
- Nginx安装及常用命令
一.选定源码目录 cd /usr/local/src 可以是任何目录,本文选定的是/usr/local/src 二.安装依赖库 yum install gcc yum install pcre-dev ...
- Docker安装和常用命令
Docker安装 Docker的安装可以参考 https://docs.docker.com/ 下面的 Get Docker / Docker CE / Linux, 需要关注的主要是CentOS和U ...
随机推荐
- 总是说spring难学?来看完这些spring的注解及其解释,真香!
前言 用过spring的人都知道,spring简单的通过注解就可以完成很多事情,但这些东西是如何实现的呢以及如何应用到我们自己的代码中?接下来,让我们一起开启注解的旅程. 1. @Controller ...
- 如何使用ABBYY FineReader 处理无法识别的字符?
在识别PDF文档时,我们可能会遇到文档中存在多种语言.多种不同类型文字字符的情况.在ABBYY FineReader 15(Windows系统)OCR文字识别软件的默认语言数据下,可能无法识别PDF文 ...
- 商业智能(BI)可视化大屏的设计及使用原则
信息时代,数据是一种可贵的资源,我们可能经常听到的一句话就是:用数据说话.但是,在没有进行系统化整理之前,数据不过只是一串串冰冷的数字,我们很难从大量的数据中获取到有价值的信息.只有通过合适的可视化工 ...
- 如何循序渐进、有效地学习JavaScript?
转载链接:https://www.zhihu.com/question/19713563/answer/23068003 分享一篇 超毛 的一篇文章<如何学习javascript>(原文链 ...
- synchronized关键字的内存语义
以下内容摘自:Java并发编程之美 加锁和释放锁的语义:当获取锁以后会清空锁块内本地内存中将会被用到的共享变量,在使用这些共享变量的时从主内存进行加载,在释放锁时将本地内存中修改的 共享变量刷新到主内 ...
- nmap安装和使用
nmap安装和使用 安装 官网地址 https://nmap.org/download.html 许多流行的Linux发行版(Redhat.Mandrake.Suse等)都使用RPM软件包管理系统,方 ...
- P3619 魔法
考虑两个任务 \(1\) 和 \(2\),当前时间为 \(T\),两个任务都要完成. 先完成任务 \(1\) 的条件是 \(T>t_1\) 且 \(T+b_1>t_2\),先完成任务 \( ...
- c语言版单链表
1 //c语言单链表 2 #include <stdio.h> 3 #include <stdlib.h> 4 typedef struct Node 5 { 6 int da ...
- redis数据量大时bgsave线程阻塞redis原因
rt 转载 Latency generated by fork In order to generate the RDB file in background, or to rewrite the A ...
- sqli-labs-master 闯关前知识点学习
1).前期准备.知识点 开始之前,为了方便查看sql注入语句,我在sqli-labs-master网页源码php部分加了两行代码,第一行意思是输出数据库语句,第二行是换行符 一.Mysql 登录 1. ...