栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

一篇小文章带你了解nginx

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一篇小文章带你了解nginx

一、nginx的官方文档

nginx documentation

二、nginx是什么?

nginx是一个做网站服务器的软件,是静态的网站

三、shell里一键部署安装nginx的脚本
#!/bin/bash

#解决软件的依赖关系,需要安装的软件包
yum install epel-release -y
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make psmisc net-tools lsof vim geoip geoip-devel wget -y

#新建luogan用户和组
id  sclilin99 || useradd sclilin99 -s /sbin/nologin

#下载nginx软件
mkdir  /sclilin99 -p
cd /sclilin99
wget  https://nginx.org/download/nginx-1.21.4.tar.gz

#解压软件
tar xf nginx-1.21.4.tar.gz 
#进入解压后的文件夹
cd nginx-1.21.4

#编译前的配置
./configure --prefix=/usr/local/sclilin99  --user=sclilin99 --group=sclilin99  --with-http_ssl_module   --with-threads  --with-http_v2_module  --with-http_stub_status_module  --with-stream  --with-http_geoip_module --with-http_gunzip_module

#如果上面的编译前的配置失败,直接退出脚本
if (( $? != 0));then
	exit
fi
#编译
make -j 2
#编译安装
make  install

#修改PATH变量
echo  "PATH=$PATH:/usr/local/sclilin99/sbin" >>/root/.bashrc
#执行修改了环境变量的脚本
source /root/.bashrc


#firewalld and selinux

#stop firewall和设置下次开机不启动firewalld
service firewalld stop
systemctl disable firewalld

#临时停止selinux和永久停止selinux
setenforce 0
sed  -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config

#开机启动
chmod +x /etc/rc.d/rc.local
echo  "/usr/local/sclilin99/sbin/nginx" >>/etc/rc.local

#修改nginx.conf的配置,例如:端口号,worker进程数,线程数,服务域名

sed  -i '/worker_processes/ s/1/2/' /usr/local/sclilin99/conf/nginx.conf
sed  -i  '/worker_connections/ s/1024/2048/' /usr/local/sclilin99/conf/nginx.conf
sed  -i -r '36c \tlisten  80;' /usr/local/sclilin99/conf/nginx.conf
sed  -i -r '37c \tserver_name www.sclilin99.com;' /usr/local/sclilin99/conf/nginx.conf

#killall nginx进程
killall -9 nginx

#启动nginx
/usr/local/sclilin99/sbin/nginx
四、nginx安装

1、yum安装    到nginx或者centos的官方去下载nginx的软件包安装
                        nginx.*.tar.gz --->制作成--》nginx.*.rpm结尾

2、编译安装    nginx是使用c语言编写的,将源码编译成二进制程序,然后安装
                        需要自己解决软件之间的依赖关系,还需要指定很多的配置,难度大,可以可定                              制开启需要的功能--》可以定制功能和指定安装的路径

编译安装经典三部曲:

1、编译前的配置文件  例:./configure --prefix=/usr/local/sclilin99  --user=lilin --group=lilin  --with-http_ssl_module   --with-threads  --with-http_v2_module  --with-http_stub_status_module  --with-stream  --with-http_gunzip_module

2、编译(理解为讲源代码编译成二进制程序) make

3、编译安装(将已经编译好的二进制程序安装(cp)到指定的路径) make install

五、安装nginx后里面几个文件的作用
[root@localhost ~]# cd /usr/local/sclilin99/
[root@localhost sclilin99]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[root@localhost sclilin99]# cd html
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# 

conf-----》存放nginx的配置文件

log-------》存放日志

sbin------》存放nginx的启动程序

html------》存放网站的网页的目录

html里的index.html指的是首页-------》进入某个网站看到的第一个页面,想要去其他页面需要通过首页里的链接进行跳转

六、如何判断nginx是否启动

1、看端口 netstat -anplut lsof -i:80 ss -anplut

2、看进程 ps aux|grep nginx

3、直接访问

4、看日志 tail -f(动态监控)

七、关闭nginx

1、nginx -s stop

[root@localhost html]# ps -aux|grep nginx
root      77302  0.0  0.1  46372  2008 ?        Ss   17:28   0:00 nginx: master process nginx
lilin     77367  0.0  0.1  46812  2044 ?        S    17:29   0:00 nginx: worker process
root      83126  0.0  0.0 112824   988 pts/0    S+   19:27   0:00 grep --color=autonginx
[root@localhost html]# nginx -s stop
[root@localhost html]# ps -aux|grep nginx
root      83148  0.0  0.0 112824   984 pts/0    R+   19:28   0:00 grep --color=autonginx

2、nginx -s quit(推荐)

[root@localhost html]# ps -aux|grep nginx
root      83256  0.0  0.0  46232  1160 ?        Ss   19:30   0:00 nginx: master process nginx
lilin     83257  0.0  0.1  46692  1916 ?        S    19:30   0:00 nginx: worker process
root      83261  0.0  0.0 112824   984 pts/0    R+   19:30   0:00 grep --color=autonginx
[root@localhost html]# nginx -s quit
[root@localhost html]# ps -aux|grep nginx
root      83289  0.0  0.0 112824   988 pts/0    R+   19:30   0:00 grep --color=autonginx

3、nginx -s reload(重点)

修改了nginx的配置文件,相当于刷新服务(启用新的配置),不会中断业务

[root@localhost ~]# cd /usr/local/sclilin99/
[root@localhost sclilin99]# cd conf/
[root@localhost conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf
[root@localhost conf]# vim nginx.conf
# worker_processes  1      ----》改成   worker_processes  4
[root@localhost conf]# nginx
[root@localhost conf]# nginx -s reload
[root@localhost conf]# ps -aux|grep nginx
root      83534  0.0  0.1  46372  2008 ?        Ss   19:35   0:00 nginx: master process nginx
lilin     83542  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process
lilin     83543  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process
lilin     83544  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process

4、kill(直接杀死)

使用kill的话得先杀死master,如果先杀掉worker的话,master会创建一个新的worker来取代

也可以理解为master是老板不需要干活,只需要监督worker;worker是员工老板手底下干活的,

新来的员工取代了老员工,取之不尽用之不竭,所以必须先把老板master杀死,再杀员工。

例:杀掉所有的nginx进程

[root@localhost conf]# ps -aux|grep nginx
root      83534  0.0  0.1  46372  2008 ?        Ss   19:35   0:00 nginx: master process nginx
lilin     83542  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process
lilin     83543  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process
lilin     83544  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process
lilin     83545  0.0  0.1  46812  2044 ?        S    19:35   0:00 nginx: worker process
[root@localhost conf]# killall -9 nginx
[root@localhost conf]# ps -aux|grep nginx
root      84189  0.0  0.0 112824   984 pts/0    R+   19:48   0:00 grep --color=autonginx

杀老板(master)再杀员工(worker)  kill -9 进程号       -9-------》强制杀死

[root@localhost conf]# ps -aux|grep nginx
root      84351  0.0  0.1  46372  2008 ?        Ss   19:52   0:00 nginx: master process nginx
lilin     84356  0.0  0.1  46812  2044 ?        S    19:52   0:00 nginx: worker process
root      84360  0.0  0.0 112824   988 pts/0    S+   19:52   0:00 grep --color=autonginx
[root@localhost conf]# kill -9 84351
[root@localhost conf]# ps -aux|grep nginx
lilin     84356  0.0  0.1  46812  2044 ?        S    19:52   0:00 nginx: worker process
root      84392  0.0  0.0 112824   988 pts/0    S+   19:52   0:00 grep --color=autonginx
[root@localhost conf]# kill -9 84356
[root@localhost conf]# ps -aux|grep nginx
root      84428  0.0  0.0 112824   988 pts/0    R+   19:53   0:00 grep --color=autonginx

老板员工一起杀死(kill -3 老板进程号)  -3 ---》相当于信号quit

[root@localhost conf]# ps -aux|grep nginx
root      92315  0.0  0.1  46372  2008 ?        Ss   10:42   0:00 nginx: master process nginx
lilin     92336  0.0  0.1  46812  2284 ?        S    10:42   0:00 nginx: worker process
root      92797  0.0  0.0 112824   988 pts/0    R+   10:52   0:00 grep --color=auto nginx
[root@localhost conf]# kill -3 92315
[root@localhost conf]# ps -aux|grep nginx
root      92828  0.0  0.0 112824   988 pts/0    S+   10:52   0:00 grep --color=auto nginx

八、进程和进程之间通信的方式与信号 1、进程和进程之间通信的方式
    1.信号
    2.信号量
    3.管道
    4.共享内存
    5.socket
    6.消息队列 2、信号
[root@localhost conf]# kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX	

signal 信号
 1) SIGHUP     
         nohup可以用来屏蔽hup信号
通常是在终端的控制进程结束时, 通知同一session内的各个作业停止运行
登录Linux时,系统会分配给登录用户一个终端(Session)。在这个终端运行的所有程序,包括前台进程组和后台进程组,一般都 属于这个 Session。当用户退出Linux登录时,前台进程组和后台有对终端输出的进程将会收到SIGHUP信号。这个信号的默认操作为终止进程,因此前台进 程组和后台有终端输出的进程就会中止。
    nohup  bash while.sh  &     &---》指放到后台去运行

2) SIGINT 
程序终止(interrupt)信号, 在用户键入INTR字符(通常是Ctrl-C)时发出,用于通知前台进程组终止进程。

3) SIGQUIT    
其实可以理解为中断程序的正常运行,同时也会给子进程也终止

9) SIGKILL
 用来立即结束程序的运行. 本信号不能被阻塞、处理和忽略。
 告诉linux 内核去强制杀死进程--》理解为内核派出锦衣卫去杀死某个人

15) SIGTERM
程序结束(terminate)信号, 与SIGKILL不同的是该信号可以被阻塞和处理。通常用来要求程序自己正常退出,shell命令kill缺省产生这个信号。

trap 屏幕各种信号,唯独9这种信号不能被屏蔽
[root@localhost sclilin99]# cat while.sh 
trap "echo i am busy"  15 3 2 1 9
i=1
while :
do
	echo $i
	((i++))
	sleep 1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/675511.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号