nginx安装

docker部署

运行nginx

我们需要把nginx的配置目录和页面拷贝出来一份(如果开发给了的话可以不用这一步直接挂载)

[root@localhost ~]# docker run -itd --name nginx nginx:latest

拷贝目录

将nginx容器内部的文件拷到指定目录

[root@localhost ~]# mkdir /opt/nginx
[root@localhost ~]# cd /opt/nginx/
[root@localhost nginx]# docker cp nginx:/etc/nginx/ .
[root@localhost nginx]# docker cp nginx:/usr/share/nginx/html .
[root@localhost nginx]# ll
total 0
drwxr-xr-x 2 root root  40 Mar  1 08:59 html
drwxr-xr-x 3 root root 132 Mar  1 09:00 nginx

删除nginx

[root@localhost nginx]# docker rm -f nginx

部署nginx

将旧的nginx删除后,我们重新启动nginx,把容器内部nginx端口绑定到宿主机端口81上,将宿主机目录挂载到nginx容器内部

[root@localhost nginx]# docker run -itd --name nginx -p 81:80 \
-v /opt/nginx/nginx/:/etc/nginx \
-v /opt/nginx/html/:/usr/share/nginx/html \
--restart=always nginx
e90ea853ce022cf6029e7d7ab5326dc120ab31c7813ac5867071087d2ca695e6
[root@localhost nginx]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS        PORTS                               NAMES
e90ea853ce02   nginx     "/docker-entrypoint.…"   2 seconds ago   Up 1 second   0.0.0.0:81->80/tcp, :::81->80/tcp   nginx

docker-compose部署

在以上步骤上,删除nginx那一步开始

[root@localhost nginx]# pwd
/opt/nginx
[root@localhost nginx]# ll
total 0
drwxr-xr-x 2 root root  40 Mar  1 08:59 html
drwxr-xr-x 3 root root 132 Mar  1 09:00 nginx

编写yaml文件

[root@localhost nginx]# cat docker-compose.yaml 
version: '3'
services:
  nginx:
    container_name: nginx                       #容器名称
    image: nginx                                #镜像
    volumes:                                    #挂载卷
      - ./nginx/:/etc/nginx
      - ./html/:/usr/share/nginx/html
    ports:                                      #映射端口
      - 81:80
    restart: always                             #重启策略

启动

[root@localhost nginx]# docker-compose up -d
[root@localhost nginx]# docker-compose ps 
NAME                COMMAND                  SERVICE             STATUS              PORTS
nginx               "/docker-entrypoint.…"   nginx               running             0.0.0.0:81->80/tcp, :::81->80/tcp

kubernetes部署

创建文件夹

需要创建一个目录,方便管理

[root@localhost ~]# mkdir -p /opt/nginx/{app,html}
[root@localhost ~]# cd /opt/nginx/

编写namespace

区分namespace,也可以不区分,可以根据自己的需求来

[root@localhost nginx]# cat 01_namespace_nginx.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: nginx

运行

[root@localhost nginx]# kubectl apply -f 01_namespace_nginx.yaml 

编写deployment

编写好要注释挂载的卷,先启动然后拷贝相应的文件到宿主机,或者使用自己写好的文件可以取消注释直接运行

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
#        volumeMounts:
#        - name: conf
#          mountPath: /etc/nginx
#        - name: html
#          mountPath: /usr/share/nginx/html
#      volumes:
#      - name: conf
#        hostPath:
#          path: /opt/nginx/app
#      - name: html
#        hostPath:
#          path: /opt/nginx/html

运行

[root@localhost nginx]# kubectl apply -f 02_deployment_nginx.yaml 

拷贝文件

将我们 需要的文件拷出来放在指定的宿主机上

[root@localhost nginx]# kubectl cp nginx/nginx-7848d4b86f-tdljg:/usr/share/nginx/html html
[root@localhost nginx]# kubectl cp nginx/nginx-7848d4b86f-tdljg:/etc/nginx app

挂载文件

取消注释重新运行

[root@localhost nginx]# sed -i 's/#//g' 02_deployment_nginx.yaml 
[root@localhost nginx]# kubectl apply -f 02_deployment_nginx.yaml 
deployment.apps/nginx configured
[root@localhost nginx]# kubectl get -n nginx pod
NAME                    READY   STATUS    RESTARTS   AGE
nginx-6b98f4cf9-mhxt9   1/1     Running   0          76s

编写service

将pod端口映射到宿主机端口对外开放

[root@localhost nginx]# cat 03_service_nginx.yaml 
apiVersion: v1
kind: Service
metadata:
  name: ngx-service
  namespace: nginx
  labels:
    app: nginx
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30080

运行

查看创建的service是否绑定到对应的容器上

[root@localhost nginx]# kubectl apply -f 03_service_nginx.yaml 
[root@localhost nginx]# kubectl get -n nginx pod,svc -o wide
NAME                        READY   STATUS    RESTARTS   AGE     IP               NODE                    NOMINATED NODE   READINESS GATES
pod/nginx-6b98f4cf9-mhxt9   1/1     Running   0          2m30s   100.103.244.74   localhost.localdomain   <none>           <none>

NAME                  TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/ngx-service   NodePort   10.104.84.189   <none>        80:30080/TCP   19m   app=nginx

编译安装

下载安装包

首先我们要去到nginx官网获取到nginx的安装包
nginx的官方网站:https://nginx.org/en/download.html

[root@localhost ~]# wget https://nginx.org/download/nginx-1.21.6.tar.gz

解压安装包

将文件解压到指定目录

[root@localhost ~]# tar -zxf nginx-1.21.6.tar.gz -C /usr/local/src/

更换yum源

使用centos自带的源太慢,可以换成国内的阿里源,或者其他的源

[root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum makecache

下载依赖包

  • GCC编译器::gcc gcc-c++
  • 正则表达式PCRE库:pcre pcre-devel
  • zlib压缩库:zlib zlib-devel
  • OpenSSL开发库:openssl openssl-devel
[root@localhost ~]# yum -y install net-tools vim gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

编译安装

进入到解压的路径进行配置,先不配置直接编译安装。

[root@localhost ~]# cd /usr/local/src/nginx-1.21.6/
[root@localhost nginx-1.21.6]# ./configure 
[root@localhost nginx-1.21.6]# make && make install

启动nginx

默认编译安装路径是/usr/local/nginx/,进入使用命令启动nginx

[root@localhost nginx-1.21.6]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx
[root@localhost sbin]# netstat -ntlp|grep 80       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      60224/nginx: master

默认配置是80端口,查看80端口是否打开,登入页面查看nginx页面,不能的话可以检查一下防火墙之类的,或者网络是否通

重新编译

当已经编译安装后的nginx需要用到某些模块的时候,需要重新编译,首先查看原本nginx带那些模块

[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.21.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments:

编译安装的时候并没有带模块,现在需要安装一些我们需要的模块,去到安装包解压的目录

[root@localhost sbin]# cd /usr/local/src/nginx-1.21.6/

选择自己所需要的模块进行配置,我这边选择了nginx的用户和安全模块

[root@localhost nginx-1.21.6]# ./configure --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module

编译,千万别安装,否则就覆盖了

[root@localhost nginx-1.21.6]# make

备份旧的nginx并替换

[root@localhost nginx-1.21.6]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak
[root@localhost nginx-1.21.6]# cp -rf objs/nginx /usr/local/nginx/sbin/
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? yes

查看是否加载模块

[root@localhost nginx-1.21.6]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.21.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module

修改nginx用户配置

[root@localhost sbin]# vi ../conf/nginx.conf
user  nginx;

添加nginx用户并重启

[root@localhost sbin]# useradd nginx
[root@localhost sbin]# ./nginx -s stop
[root@localhost sbin]# ./nginx

查看是否生效

[root@localhost sbin]# ps -ef |grep nginx
root      60224      1  0 02:29 ?        00:00:00 nginx: master process ./nginx
nginx     60225  60224  0 02:29 ?        00:00:00 nginx: worker process
root      60487   2962  0 02:29 pts/0    00:00:00 grep --color=auto nginx

拓展

nginx命令

nginx的命令很少,基本的暂停,查看版本等等

[root@localhost sbin]# ./nginx -h
nginx version: nginx/1.21.6
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -e filename   : set error log file (default: logs/error.log)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

应用场景

查看nginx版本

[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.21.6

查看nginx编译信息和版本

[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.21.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments:

测试配置文件是否正确

[root@localhost 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

测试配置文件是否正确并输出

[root@localhost sbin]# ./nginx -T

关闭nginx

[root@localhost sbin]# ./nginx -s stop
[root@localhost sbin]# ./nginx -s quit

不重启nginx读取最新配置

[root@localhost sbin]# ./nginx -s reload

启动时指定nginx配置文件

[root@localhost sbin]# ./nginx -c /usr/local/nginx/conf/nginx.conf
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值