nginx-1-nginx简介和安装

Ubuntu 20.04 安装nginx
官网nginx下载地址
ubuntu16.04 离线安装nginx

1 什么是nginx

俄罗斯人研发的一款软件。
nginx专门为性能优化而研发。
高性能的http和反向代理服务器。
可达50000个并发连接数。

1.1 基本概念

反向代理
负载均衡
动静分离
高可用

1.2 动静分离

为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度,降低原来单个服务器的压力。

静态资源html、CSS、js
动态资源jsp、serverlet

tomcat服务器:【动态资源】
nginx反向代理服务器:【静态资源服务器:静态资源】

1.3 负载均衡

客户端----请求>>>>服务端----查询>>>>数据库
增加服务器的数量
在这里插入图片描述

1.4 正向代理和反向代理

(1)正向代理
nginx可以利用正向代理来进行上网等功能。

用户-xxx>www.google.com
用户->浏览器中配置代理服务器->代理服务器->www.google.com
用户<-内容<-代理服务器

正向代理:在客户端(浏览器)配置代理服务器,通过代理服务器进行互联网访问。

用户请求的还是www.google.com

(2)反向代理
反向代理:客户端对代理是无感知的。
用户->反向代理服务器9001->转发到对应的->tomcat服务器8001

用户请求的是IP:9001,而不是目标服务器IP:8001。

2 安装nginx

2.1 Centos7安装nginx

#yum upgrade -y更新
yum -y update:升级所有包同时也升级软件和系统内核;
yum -y upgrade:只升级所有包,不升级软件和系统内核。

因为默认的源里面没有 nginx,这里推荐使用 EPEL,它是 yum 的一个软件源,里面包含了许多基本源里没有的软件。一些软件如 PHP、Redis、Nginx,必须添加 EPEL 源之后,才能用 yum 来安装。

EPEL的全称叫 Extra Packages for Enterprise Linux 。EPEL是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。装上了 EPEL之后,就相当于添加了一个第三方源。

#yum -y install epel-release
#yum repolist检查软件源
#firewall-cmd --state查看防火墙状态
#systemctl stop firewalld.service停止防火墙
#systemctl disable firewalld.service 禁止开机自启动

(1)使用远程工具连接centos系统
#yum -y install nginx安装
#yum remove nginx卸载
注意:使用yum进行Nginx安装时,Nginx配置文件在/etc/nginx目录下。

(2)配置 Nginx 服务
#systemctl enable nginx # 设置开机启动
#systemctl start nginx # 启动nginx服务
#systemctl stop nginx # 停止nginx服务
#systemctl restart nginx # 重启nginx服务
#service nginx reload # 重新加载配置,一般是在修改过nginx配置文件时使用。
查看是否启动成功
#ps -ef | grep nginx
在这里插入图片描述
(3)查看nginx的版本
#nginx -v 显示 nginx 的版本。
#nginx -V显示 nginx 的版本,编译器版本和配置参数。

2.2 Ubuntu安装nginx

下载安装包nginx-1.18.0.tar.gz

2.2.1 安装依赖

(1)PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式。

sudo dpkg -i libpcre3_8.39-12build1_amd64.deb
sudo dpkg -i libpcre16-3_8.39-12build1_amd64.deb
sudo dpkg -i libpcre32-3_8.39-12build1_amd64.deb
sudo dpkg -i libpcrecpp0v5_8.39-12build1_amd64.deb
sudo dpkg -i libpcre3-dev_8.39-12build1_amd64.deb

(2)zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip。

sudo dpkg -i zlib1g_1.2.11.dfsg-2ubuntu1_amd64.deb
sudo dpkg -i zlib1g-dev_1.2.11.dfsg-2ubuntu1_amd64.deb

(3)OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http)。

sudo dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb
sudo dpkg -i libssl-dev_1.1.1f-1ubuntu2_amd64.deb

2.2.2 解压安装

(1)解压
sudo tar -xzvf nginx-1.18.0.tar.gz

(2)配置nginx
sudo ./configure --with-http_ssl_module

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
(3)编译
sudo make

(4)安装
sudo make install

(5)创建软连接
sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx

2.2.3 设置开机自启动

服务文件/usr/lib/systemd/system/nginx.service

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

Description:描述服务
After:依赖,当依赖的服务启动之后再启动自定义的服务

[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令(需要根据路径适配)
ExecReload为重启命令(需要根据路径适配)
ExecStop为停止命令(需要根据路径适配)
PrivateTmp=True表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径

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

sudo chmod a+x nginx.service
sudo systemctl disable nginx.service 关闭开机自启
sudo systemctl enable nginx.service 开启开机自启
sudo systemctl status nginx.service 查看状态
sudo systemctl restart nginx.service 重启服务
sudo systemctl list-units --type=service 查看所有服务
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮冰燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值