nginx服务安装

一、nginx介绍

1.nginx概述

Nginx是一个开源且高性能、可靠的Http Web服务、代理服务。
开源: 直接获取源代码
高性能: 支持海量并发
可靠: 服务稳定

2.nginx特点

1)高性能高并发

性能高,支持海量并发,当并发特别高的时候,nginx比其他的web服务响应速度快

2)轻量且高扩展性

1.功能模块多,但仅需要保留必要的模块
2.需要哪个模块添加哪个模块,可以兼容第三方模块

3)高可靠性

很多web服务跑一段事件后需要重启,nginx不需要
nginx支持宕机时间级别为 9999、99999

4)支持热部署

nginx可以在开机情况下进行升级和重启

5)互联网公司使用nginx

nginx技术成熟,可以做负载,安全,web,缓存

6)nginx支持epool网络模型

1.epool:当用户发起请求,直接对请求的内容进行处理
2.select:当用户发起请求,先遍历扫描数据,然后对请求的内容进行处理

Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。

3.其他web服务

1.apache:最早使用的web服务,难以掌握,性能不高
2.nginx
   1)Tengine:淘宝根据nginx进一步开发得到的服务
   2)openresty:是一个基于 Nginx 与 Lua 的高性能 Web 平台
3.lighttpd:消耗的内存和cpu较低
4.IIS:windows的web服务
5.GWS:Google web server
6.BWS:baidu web server

二、nginx的安装方式

1.epol源安装


[root@web01 ~]# yum install -y nginx

2.官方源安装

1)配置官方源


[root@web02 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

2)安装依赖


[root@web02 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

3)安装nginx


[root@web02 ~]# yum install -y nginx

4)修改启动用户


[root@web02 ~]# groupadd www -g 666
[root@web02 ~]# useradd www -u 666 -g 666

[root@web02 ~]# vim /etc/nginx/nginx.conf
user www;

5)启动服务并验证


[root@web02 ~]# systemctl restart nginx

[root@web02 ~]# ps -ef | grep nginx

6)nginx常用命令


1.启动命令
[root@web02 ~]# systemctl start nginx
#或者
[root@web02 ~]# nginx

#注意,使用哪种方式启动就用哪种方式关闭
2.关闭命令
[root@web02 ~]# systemctl stop nginx
#或者
[root@web02 ~]# nginx -s stop

3.nginx重启
[root@web02 ~]# systemctl restart nginx

4.nginx重载配置文件
[root@web02 ~]# systemctl reload nginx
#或者
[root@web02 ~]# nginx -s reload

5.检查nginx配置
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

6.加入开机自启
[root@web01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

7.CentOS6操作
#启动
[root@web01 ~]# nginx
[root@web01 ~]# /etc/init.d/nginx start
[root@web01 ~]# service nginx start
#配置开机自启
[root@web01 ~]# chkconfig nginx on

#检验启动
#方式一:
	[root@web02 ~]# ps -ef | grep nginx
#方式二:
	[root@web02 ~]# netstat -lntp | grep 80
#方式三:
	访问页面 10.0.0.8:80
#方式四:
    #查看版本
    [root@web02 ~]# nginx -v
    #查看安装模块
    [root@web02 ~]# nginx -V
    

3.源码包安装

1)安装依赖


[root@web03 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

2)下载或者上传包并解压


[root@web03 ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
#或者
[root@web03 ~]# rz nginx-1.18.0.tar.gz

[root@web03 ~]# tar xf nginx-1.18.0.tar.gz

3)创建用户


[root@web03 ~]# groupadd www -g 666
[root@web03 ~]# useradd www -u 666 -g 666

4)生成


[root@web03 ~]# cd nginx-1.18.0/
[root@web03 ~/nginx-1.18.0]# ./configure --prefix=/usr/local/nginx-1.18.0 --user=www --group=www --with-http_addition_module --with-http_auth_request_module --without-http_gzip_module

5)编译安装


[root@web03 ~/nginx-1.18.0]# make && make install

6)配置system管理


#配置system管理
[root@web03 ~]# vim /etc/systemd/system/nginx.service 
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target

7)做软链接


[root@web03 ~]# ln -s /usr/local/nginx-1.18.0 /usr/local/nginx

#配置环境变量
[root@web03 ~]# cat /etc/profile.d/nginx.sh 
export PATH=/usr/local/nginx/sbin/:$PATH

#重新执行全局变量
[root@web03 ~]# source /etc/profile

#软链接的作用:
1.配置环境变量可以不加版本号
2.配置system启动可以不加版本号
3.升级直接切换软连接的链接文件即可

8)启动


[root@web03 ~]# systemctl daemon-reload
[root@web03 ~]# systemctl start nginx
#配置开机自启
[root@web03 ~]# systemctl enable nginx

9)nginx升级


1.下载新版本的包
[root@web03 ~]# wget http://nginx.org/download/nginx-1.19.2.tar.gz

2.解压
[root@web03 ~]# tar xf nginx-1.19.2.tar.gz

3.生成编译安装
[root@web03 ~]# cd nginx-1.19.2/
[root@web03 nginx-1.19.2]# ./configure --prefix=/usr/local/nginx-1.19.2 --user=www --group=www --with-http_addition_module --with-http_auth_request_module --without-http_gzip_module
[root@web03 nginx-1.19.2]# make && make install

4.替换配置文件
[root@web03 /usr/local]# cp nginx-1.18.0/conf/nginx.conf nginx-1.19.2/conf/
cp: overwrite ‘nginx-1.19.2/conf/nginx.conf’? y

5.更换版本
[root@web03 /usr/local]# rm -rf /usr/local/nginx
[root@web03 /usr/local]# ln -s /usr/local/nginx-1.19.2 nginx

6.重启nginx
[root@web03 /usr/local]# systemctl restart nginx

7.检验
[root@web03 /usr/local]# nginx -v
nginx version: nginx/1.19.2

10)版本回退


1.回退版本
[root@web03 ~]# rm -rf /usr/local/nginx
[root@web03 ~]# ln -s /usr/local/nginx-1.18.0 /usr/local/nginx

2.重启
[root@web03 ~]# systemctl restart nginx

3.检验
[root@web03 ~]# nginx -v
nginx version: nginx/1.18.0


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值