Nginx安装演示

1. Nginx是什么

Nginx 是一款自由的、开源的、高性能的 HTTP 服务器(同类产品如Apache)和反向代理服务器
同时也是一个 IMAP、POP3、SMTP 代理服务器。
Nginx选择了epoll and kqueue作为开发模型,在连接高并发的情况下,Nginx是Apache服务器不错的替代品,能够支持高达 50,000 个并发连接数的响应.

说到代理,那么我们来解释一下代理和反向代理.

1)代理服务器(正向代理)

在这里插入图片描述
客户端知道自己想要请求的服务端.

如图所示,当我们想指定的服务器发起请求,但是由于某种原因,请求无法直接到达时,可以借由代理服务器来帮我们实现请求.
比如,我们大陆想直接访问google是做不到的,因此可以借由一些可以直接访问到google代理服务器,来将我们的请求转发给google,然后在得到响应后,再将响应返回给我们.

2)反向代理服务器

在这里插入图片描述
真正暴露给客户端的是反向代理服务器
客户端直接请求反向代理服务器,而事实上,客户端并不知道反向代理背后还有多少台服务器,也不知道自己的请求究竟会到达哪一台服务器.
Nginx 还可以作为反向代理进行负载均衡的实现.

2. 安装配置

网上各种各样的版本有很多,但是,最权威的仍旧是官方,所以这里优先推荐官方的文档,有不懂的再去查资料.

http://nginx.org/en/docs/install.html

在这里插入图片描述
我这里用的是Centos7.4,所以可以选择(1).安装包来安装,或者(2)选择编译nginx源码的方式.(当然了了,也可以使用Docker,直接pull一个nginx的镜像来,这里会分别做演示.)

2.1 采用docker的方式

2.1.1 拉取镜像

首先到dockerhub找到nginx官方提供的镜像仓库.

https://hub.docker.com/_/nginx

在这里插入图片描述
然后根据自己需求,可以选择对应的版本,和标签.
这里我使用的是stable版本的.

# 拉取镜像
sudo docker pull nginx:stable

在这里插入图片描述

2.2.2 启动容器
  1. 启动前准备
    准备一个目录,用来存放我们的html文件,与容器内的/usr/share/nginx/html进行关联.
mkdir -p nginx/sub/html/

在上面的目录中,创建一个index.html的文件,并写入一些内容.

echo '<h1>Helow World Nginx!</h1>' > nginx/sub/html/index.html
  1. 启动容器
sudo docker run --name nginx01 -v /home/shuu/demo/nginx/sub/html:/usr/share/nginx/html:ro -d nginx:stable

查看容器ip

sudo docker inspect nginx01 -f='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'

在这里插入图片描述
访问nginx容器

curl 172.18.0.2

在这里插入图片描述
ok,搭建成功,是不是灰常easy!,这里仅做安装演示,具体复杂配置不是本文范畴.

2.2 采用package方式(yum)安装

2.2.1 环境准备

由于本文主旨是演示安装方式,所以这里为了能更详细的掌握安装过程中可能会遇到的问题,所以这里还是会用到docker,但是不同的是,这次是启动一个Centos7的容器,在容器里面尝试安装nginx.

  1. 拉取镜像
sudo docker pull centos:centos7

在这里插入图片描述

  1. 启动&进入容器
# 启动容器
sudo docker run --name "centos" -d -it --privileged centos:centos7 /usr/sbin/init
# 进入容器
sudo docker exec -it centos /bin/bash
2.2.2 安装nginx

官方安装指南

http://nginx.org/en/linux_packages.html#RHEL-CentOS

1)事前准备
要养成能不用root用户就不用的习惯.

# 安装sudo
yum install -y sudo
# 创建nginx用户
useradd nginx
passwd nginx
# 给nginx用户sudo权限
visudo
# 接下来的操作需要切换至nginx
su - nginx

在这里插入图片描述

1)配置nginx.repo

# 创建nginx.repo
sudo touch /etc/yum.repos.d/nginx.repo
# 编辑
sudo vi /etc/yum.repos.d/nginx.repo

在nginx.repo中填入下列内容:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

一个stable的一个mainline的,默认使用stable,而mainline是disable的,如果想启用mainline的:

sudo yum-config-manager --enable nginx-mainline

2)安装nginx

sudo yum install nginx

会发现nginx依赖makeopenssl
在这里插入图片描述
3)启动nginx

查看nginx服务是否开启(默认是不开启的)

sudo systemctl status nginx

在这里插入图片描述
启动nginx服务.

sudo systemctl start nginx

启动后报错
在这里插入图片描述

# 查看日志,找到报错信息
sudo journalctl -xe

在这里插入图片描述
原来80端口已经被占用了,而nginx默认监听80端口.为了查看是什么占用了80端口我们还需要netstat命令.

sudo yum install -y net-tools
sudo netstat -nltp | grep 80

在这里插入图片描述
kill掉这个进程:

sudo kill 242

会发现,还会有一个nginx: worker进程,再kill掉
在这里插入图片描述

sudo kill 243

再次查看,已经没有占用80端口的进程了
在这里插入图片描述
再次尝试启动nginx成功

sudo systemctl start nginx

在这里插入图片描述
访问nginx默认的欢迎页,成功!

curl localhost:80

在这里插入图片描述

2.3 采用编译源码的方式

2.3.1 准备容器

和yum安装一样,这里我还是用一个Centos的docker容器来演示.
将之前的容器删除.

sudo docker stop centos
sudo docker rm centos

在这里插入图片描述
启动&进入容器

# 启动容器
sudo docker run --name "centos" -d -it --privileged centos:centos7 /usr/sbin/init
# 进入容器
sudo docker exec -it centos /bin/bash
2.2.2 安装nginx
2.2.2.1 事前准备

要养成能不用root用户就不用的习惯.

# 安装sudo
yum install -y sudo
# 创建nginx用户
useradd nginx
passwd nginx
# 给nginx用户sudo权限
visudo
# 接下来的操作需要切换至nginx
su - nginx
# 安装wget用来下载
sudo yum install -y wget

在这里插入图片描述

2.2.2.2 重要组件安装

官方安装指南

http://nginx.org/en/docs/configure.html

介绍了N多配置选项,因为是刚接触不就,目前没有可能全都用到,这里给出几个觉得重要的内容:

1)–with-pcre=path

sets the path to the sources of the PCRE library. The library distribution (version 4.4 — 8.43) needs to be downloaded from the PCRE site and extracted.
The rest is done by nginx’s ./configure and make.
The library is required for regular expressions support in the location directive and for the ngx_http_rewrite_module module.

The ngx_http_rewrite_module module is used to change request URI using PCRE regular expressions,
return redirects, and conditionally select configurations.

PCRE是nginx配置location是,用来支持正则表达式的,如果没有PCRE,则location的匹配无法支持正则表达式,即只能完整的输入匹配名称,而且ngx_http_rewrite_module模块是用来匹配请求的URI,然后进行响应的替换转发作用的,也需要正则表达式的支持.也就是说很重要,需要安装.

2)–with-zlib=path

sets the path to the sources of the zlib library. The library distribution (version 1.1.3 — 1.2.11) needs to be downloaded from the zlib site and extracted. The rest is done by nginx’s ./configure and make. The library is required for the ngx_http_gzip_module module.

**The ngx_http_gzip_module module is a filter that compresses responses using the “gzip” method. 
This often helps to reduce the size of transmitted data by half or even more.**

zlib是用来支持ngx_http_gzip_module模块的,而ngx_http_gzip_module可以将响应用gzip来进行 压缩,可以节约一半以上的空间,所以也很重要.

3)–with-http_ssl_module

enables building a module that adds the HTTPS protocol support to an HTTP server. This module is not built by default. The OpenSSL library is required to build and run this module.

用来生成一个为HTTP server提供HTTPS协议支持的模块,很重要.
所以综上所述,pcre,zlib,openssl这些就是目前大致对官方资料查阅后,觉得有必要安装的内容.下面开始安装吧.
注:前两种安装方式虽然成功安装并运行了欢迎页,但是nginx真正核心的功能所依赖的模块并未安装,

4)重要依赖下载&安装

  1. pcre
mkdir download && cd download
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
# 创建一个module目录,用来放解压后的文件
mkdir ~/module
# 解压缩到module目录下
tar -xzvf pcre-8.43.tar.gz -C ~/module/
# 习惯性创建连接
ln -s ~/module/pcre-8.43 ~/module/pcre
  1. zlib
wget https://jaist.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
# 解压缩到module目录下
tar -xzvf zlib-1.2.11.tar.gz -C ~/module/
# 习惯性创建连接
ln -s ~/module/zlib-1.2.11 ~/module/zlib
  1. make,openssl
    由于需要用到make指令,所以肯定需要用到gcc因为nginx是纯c语言写的,而后面在make时候,pcre会用到c++的编译器,所以这里我们直接安装gcc-c++,它在gcc基础上提供了c++的支持,其他的一会儿需要再说,再有就是openssl,也一并使用yum来安装.
    至于还少什么稍后补充,毕竟目前为止只知道这么多.
    事实上还需要 openssl-devel,下面马上出现
sudo yum install make openssl gcc-c++ -y
  1. Nginx下载
wget http://nginx.org/download/nginx-1.16.1.tar.gz
# 解压缩到module目录下
tar -xzvf nginx-1.16.1.tar.gz -C ~/module/
2.2.2.3 编译安装

1)配置&依赖检查

./configure \
    --sbin-path=/usr/local/nginx/nginx \
    --conf-path=/usr/local/nginx/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --with-http_ssl_module \
    --with-pcre=../pcre \
    --with-zlib=../zlib

第一次报错,./configure: error: SSL modules require the OpenSSL library.
在这里插入图片描述

在上面的位置都找不到,但是命名之前已经安装了openssl,于是使用find命令查找下.

sudo find /usr -name openssl

在这里插入图片描述

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

报错信息中还有如下内容,可以使用–with-openssl= 选项,我们再来试试.

./configure \
    --sbin-path=/usr/local/nginx/nginx \
    --conf-path=/usr/local/nginx/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --with-openssl=/usr/lib64/openssl \
    --with-http_ssl_module \
    --with-pcre=../pcre \
    --with-zlib=../zlib

应该是成功了.
在这里插入图片描述
2)编译&安装

make

再次报错,没有ssl.h这个文件,此为header,头文件,需要安装openssl-devel
在这里插入图片描述

sudo yum install -y openssl-devel
# 安装后发现,ssl.h文件的位置所在,所以此时不需要--with-openssl=/usr/lib64/openssl选项了
sudo find / -name ssl.h

在这里插入图片描述
重新进行配置

./configure \
    --sbin-path=/usr/local/nginx/nginx \
    --conf-path=/usr/local/nginx/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --with-http_ssl_module \
    --with-pcre=../pcre \
    --with-zlib=../zlib
# 配置成功后再次 make
make
# make成功后 make install 进行安装
sudo make install

查看版本号

/usr/local/nginx/nginx -v

在这里插入图片描述
3)启动nginx

# 启动
sudo /usr/local/nginx/nginx
# 测试访问
curl localhost

在这里插入图片描述
ok,至此nginx的安装就结束了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
演示nginx在Kubernetes中的使用,可以按照以下步骤进行操作: 1. 首先,使用kubectl命令进入nginx容器内部。通过进入pod的name来进入容器,比如: [root@icv-k8s-node-1 ~]# kubectl exec -it edge-nginx-6d57745bc8-dm998 -- /bin/bash 2. 进入容器后,切换到nginx的html目录: root@edge-nginx-6d57745bc8-pgmdk:/# cd /usr/share/nginx/html 3. 可以执行apt-get update和apt-get install命令来更新和安装需要的软件包。 4. 编辑index.html文件,可以使用vim或其他编辑器: root@edge-nginx-6d57745bc8-dm998:/usr/share/nginx/html# vim index.html 5. 创建一个yaml文件,用于部署nginx服务: apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: app: nginx name: nginx-deployment spec: ports: - port: 89 protocol: TCP targetPort: 80 selector: app: nginx type: NodePort status: loadBalancer: {} 6. 使用kubectl命令应用该yaml文件,创建nginx服务: [root@icv-k8s-node-1 home]# kubectl apply -f k8s_ngx_expose.yaml 7. 可以使用kubectl expose命令暴露nginx服务的端口,比如: [root@icv-k8s-node-1 home]# kubectl expose deployment nginx-deployment --port=88 --target-port=80 --type=NodePort 通过这个命令,可以将nginx的80端口暴露为集群中的一个NodePort。 这样,nginx在Kubernetes中的使用就完成了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [从零开始:使用 Kubernetes 部署 Nginx 应用](https://blog.csdn.net/qq_33589510/article/details/131478541)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值