Nginx网站服务

目录

1.Nginx概述

2.Nginx和Apache的区别

3.Nginx应用场景

4.编译安装Nginx服务

1. 关闭防火墙,将安装nginx所需软件包传到/opt目录下

2. 安装依赖包

3. 创建运行用户、组(Nginx服务程序默认以 nobody身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限)

4. 编译安装Nginx

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

6. 添加 Nginx 系统服务


1.Nginx概述

一款高性能、轻量级Web服务软件

  • 稳定性高
  • 系统资源消耗低
  • 对HTTP并发连接的处理能力高

单台物理服务器可支持30 000~50 000个并发请求

2.Nginx和Apache的区别

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

3.Nginx应用场景

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

4.编译安装Nginx服务

1. 关闭防火墙,将安装nginx所需软件包传到/opt目录下

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

nginx-1.24.0.tar.gz

2. 安装依赖包

#nginx的配置及运行需要pcre、zlib、openssl等软件包的支持,因此需要安装这些软件的开发包,以便提供相应的库和头文件。
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make

3. 创建运行用户、组(Nginx服务程序默认以 nobody身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限)

useradd -M -s /sbin/nologin nginx

4. 编译安装Nginx

cd /opt
tar zxvf nginx-1.24.0.tar.gz -C /opt/

cd nginx-1.24.0/
./configure \
--prefix=/usr/local/nginx \							#指定nginx的安装路径
--user=nginx \										#指定用户名
--group=nginx \										#指定组名
--with-http_stub_status_module						#启用 http_stub_status_module 模块以支持状态统计

make && make install                                #编译安装

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/		#让系统识别nginx的操作命令




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

5.检查、启动、重启、停止 nginx服务
nginx -t								#检查配置文件是否配置正确
#启动
/usr/local/nginx/sbin/nginx									
#停止
cat /usr/local/nginx/logs/nginx.pid		#先查看nginx的PID号
kill -3 <PID号>
kill -s QUIT <PID号>
killall -3 nginx
killall -s QUIT nginx
#重载
kill -1 <PID号>
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx
#日志分割,重新打开日志文件
kill -USR1 <PID号>
#平滑升级
kill -USR2 <PID号>

新版本升级

tar -xf nginx-1.26.0.tar.gz 
cd nginx-1.26.0
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module 
make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
cp objs/nginx /usr/local/nginx/sbin/nginx
make upgrade  #要保证当前 nginx 进程是通过 /usr/local/nginx/sbin/nginx 启动的,而不是通过查找环境变量中那个 nginx 命令启动的
#或者先 killall nginx ,再/usr/local/nginx/sbin/nginx
[root@localhost sbin]# cd /opt       #上传最新版本压缩包
[root@localhost opt]# ls
nginx-1.24.0  nginx-1.24.0.tar.gz  nginx-1.26.0.tar.gz  
[root@localhost opt]# tar xf nginx-1.26.0.tar.gz  #解压
[root@localhost opt]# ls
nginx-1.24.0  nginx-1.24.0.tar.gz  nginx-1.26.0  nginx-1.26.0.tar.gz  rh
[root@localhost opt]# cd nginx-1.26.0
[root@localhost nginx-1.26.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost nginx-1.26.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module 
[root@localhost nginx-1.26.0]# make   #编译
[root@localhost nginx-1.26.0]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@localhost nginx-1.26.0]# cd objs
[root@localhost objs]# ls
autoconf.err  nginx    ngx_auto_config.h   ngx_modules.c  src
Makefile      nginx.8  ngx_auto_headers.h  ngx_modules.o
[root@localhost objs]# cd /usr/local/nginx/sbin
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# mv nginx nginx_old 
[root@localhost sbin]# ls
nginx_old
[root@localhost sbin]# cp /opt/nginx-1.26.0/objs/nginx ./
[root@localhost sbin]# ls
nginx  nginx_old
[root@localhost sbin]# cd /opt/nginx-1.26.0
[root@localhost nginx-1.26.0]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@localhost 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@localhost nginx-1.26.0]# nginx -v   #查询版本
nginx version: nginx/1.26.0

6. 添加 Nginx 系统服务

方法一:

vim /etc/init.d/nginx
#!/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


chmod +x /etc/init.d/nginx
chkconfig --add nginx							#添加为系统服务
systemctl stop nginx
systemctl start nginx

方法二:

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]服务安装的相关设置,可设置为多用户
[root@localhost nginx-1.26.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
#进行配置后保存退出

[root@localhost nginx-1.26.0]# netstat -lntp | grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9357/nginx: master  
[root@localhost nginx-1.26.0]# nginx -s quit
[root@localhost nginx-1.26.0]# netstat -lntp | grep :80
[root@localhost nginx-1.26.0]# systemctl start nginx.service 
[root@localhost nginx-1.26.0]# netstat -lntp | grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9654/nginx: master  
[root@localhost nginx-1.26.0]# systemctl stop nginx.service 
[root@localhost nginx-1.26.0]# netstat -lntp | grep :80


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值