1、Nginx

  • 一款高性能、轻量级Web服务软件
  • 稳定性高
  • 系统资源消耗低
  • 对HTTP并发连接的处理能力高(单台物理服务器可支持30000-50000个并发请求)

(1)常用的Web服务器

windows:Apache、Nginx
linux:IIS

(2)Nginx 与 Apache 的区别

最核心的区别在于 Nginx 采用异步非阻塞机制,多个连接可以对应一个进程;Apache 采用的是同步阻塞+多进程/线程模型,一个连接对应一个进程
Nginx 抗并发能力更高
Nginx 更轻量,内存、CPU资源消耗更少
Nginx 配置简洁,使用场景多,稳定性高

(3)Nginx的应用场景

用作Web网站服务,处理http静态页面请求
用作虚拟主机,实现一个服务器用于做多个网站站点
用作反向代理、负载均衡,可以作为网关代理服务器接收客户端的请求转发给后端节点服务器集群
用作web缓存服务器

2、编译安装Nginx服务

(1)关闭防火墙,将安装nginx所需软件包传到/opt目录下并解压


[root@zx2 ~]# systemctl stop firewalld
[root@zx2 ~]# setenforce 0
[root@zx2 ~]# cd /opt/
[root@zx2 opt]# ls
nginx-1.24.0.tar.gz  rh
[root@zx2 opt]# tar xf nginx-1.24.0.tar.gz
[root@zx2 opt]# ls
nginx-1.24.0  nginx-1.24.0.tar.gz  rh
[root@zx2 opt]#

(2)安装依赖包

[root@zx2 opt]# df
文件系统                   1K-块    已用     可用 已用% 挂载点
devtmpfs                 1913504       0  1913504    0% /dev
tmpfs                    1930624       0  1930624    0% /dev/shm
tmpfs                    1930624   21148  1909476    2% /run
tmpfs                    1930624       0  1930624    0% /sys/fs/cgroup
/dev/mapper/centos-root 36805060 5428228 31376832   15% /
/dev/sda1                1038336  191268   847068   19% /boot
tmpfs                     386128      40   386088    1% /run/user/0
/dev/sr0                 4635056 4635056        0  100% /mnt
[root@zx2 opt]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make

(3)创建运行用户、组

[root@zx2 opt]# useradd -M -s /sbin/nologin nginx
[root@zx2 opt]# cat /etc/passwd | grep nginx:
nginx:x:1001:1001::/home/nginx:/sbin/nologin
[root@zx2 opt]#

(4)编译安装Nginx

[root@zx2 opt]# cd nginx-1.24.0/
[root@zx2 nginx-1.24.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@zx2 nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
[root@zx2 nginx-1.24.0]# make -j4 && make install
[root@zx2 nginx-1.24.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

(5)检查、启动、重启、停止 nginx服务

1)检查配置文件是否配置正确

[root@zx2 nginx-1.24.0]# 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@zx2 nginx-1.24.0]#

2)启动

[root@zx2 nginx-1.24.0]# /usr/local/nginx/sbin/nginx
[root@zx2 nginx-1.24.0]# netstat -lntp | grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      65903/nginx: master
[root@zx2 nginx-1.24.0]#

Nginx网站服务插图

3)停止

cat /usr/local/nginx/logs/nginx.pid        #先查看nginx的PID号
kill -3
kill -s QUIT
killall -3 nginx
killall -s QUIT nginx

[root@zx2 nginx-1.24.0]# cat /usr/local/nginx/logs/nginx.pid
65903
[root@zx2 nginx-1.24.0]# kill -3 65903
[root@zx2 nginx-1.24.0]# netstat -lntp | grep :80
[root@zx2 nginx-1.24.0]#

4)重载

kill -1
kill -s HUP
killall -1 nginx
killall -s HUP nginx

5)日志分割,重新打开日志文件

[root@zx2 nginx-1.24.0]# kill -USR1 65958

6)平滑升级

[root@zx2 nginx-1.24.0]# kill -USR2 65958

(6)新版本升级

准备nginx-1.26.0.tar.gz压缩包并上传到/opt目录中

[root@zx2 nginx-1.24.0]# cd /opt/
[root@zx2 opt]# ls
nginx-1.24.0  nginx-1.24.0.tar.gz  nginx-1.26.0.tar.gz  rh
[root@zx2 opt]# tar xf nginx-1.26.0.tar.gz
[root@zx2 opt]# ls
nginx-1.24.0  nginx-1.24.0.tar.gz  nginx-1.26.0  nginx-1.26.0.tar.gz  rh
[root@zx2 opt]# cd nginx-1.26.0/
[root@zx2 nginx-1.26.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
[root@zx2 nginx-1.26.0]# make
[root@zx2 nginx-1.26.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
[root@zx2 nginx-1.26.0]# cd objs/
[root@zx2 objs]# ls
autoconf.err  Makefile  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src
[root@zx2 objs]# cd /usr/local/nginx/sbin/
[root@zx2 sbin]# ls
nginx
[root@zx2 sbin]# mv nginx nginx_old
[root@zx2 sbin]# ls
nginx_old
[root@zx2 sbin]# cp /opt/nginx-1.26.0/objs/nginx ./
[root@zx2 sbin]# ls
nginx  nginx_old
[root@zx2 sbin]# cd /opt/nginx-1.26.0/
[root@zx2 nginx-1.26.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
[root@zx2 nginx-1.26.0]# make upgrade
/usr/local/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
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
[root@zx2 nginx-1.26.0]# ng
ngettext  nginx
[root@zx2 nginx-1.26.0]# nginx -v
nginx version: nginx/1.26.0
[root@zx2 nginx-1.26.0]#

(7)添加Nginx服务

[root@zx2 nginx-1.26.0]# vim /etc/init.d/nginx
[root@zx2 nginx-1.26.0]# chmod +x /etc/init.d/nginx
[root@zx2 nginx-1.26.0]# chkconfig --add nginx
[root@zx2 nginx-1.26.0]# systemctl stop nginx
[root@zx2 nginx-1.26.0]# systemctl start nginx
[root@zx2 nginx-1.26.0]# netstat -lntp | grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      66057/nginx: master
[root@zx2 nginx-1.26.0]#

文件内容(设置了启动、关闭、重启、重新加载服务)

#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
  $COM
;;

stop)
  kill -s QUIT $(cat $PID)
;;

restart)
  $0 stop
  $0 start
;;

reload)
  kill -s HUP $(cat $PID)
;;

*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1

esac
exit 0

方法二

vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
———————————————————
[Unit]:服务的说明
Description:描述服务
After:依赖,当依赖的服务启动之后再启动自定义的服务

[Service]服务运行参数的设置
Type=forking是后台运行的形式,使用此启动类型应同时指定PIDFile=,以便systemd能够跟踪服务的主进程。
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径

[Install]服务安装的相关设置,可设置为多用户
———————————————————

chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service

本站无任何商业行为
个人在线分享 » Nginx网站服务
E-->