centos7安装nginx的三种方式~yum源,源码,Docker

news/2024/10/21 10:20:36/

目录

1.yum安装:Centos7源默认没有nginx

2.源码安装:

3.Docker安装:


1.yum安装:Centos7源默认没有nginx

  • 配置yum源:

    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  • 查看nginx源:

    yum list | grep nginx
  • 安装nginx:

    yum install -y nginx
  • 查看是否安装成功:

    nginx -v
  • 查看nginx的安装目录:

    rpm -qc nginx

2.源码安装:

  • 下载tengine包:

    wget -c http://tengine.taobao.org/download/tengine-2.3.0.tar.gz      # -c支持断点续传
    链接:https://pan.baidu.com/s/1Gko5dOpntrIwzio5i35K-g 
    提取码:7bpf
    http://tengine.taobao.org/download.html
  • 解压:

    tar -zxvf tengine-2.3.0.tar.gz -C /usr/local
  • 修改目录名:

    cd /usr/local;mv tengine-2.3.0 tengine
  • 安装 Nginx 所需的 pcre 库(rewrite 规则)

    yum install -y pcre-devel
  • 安装 openssl 相关包:如果不安装 openssl 相关包,安装Nginx 的过程中会报错

    yum install -y openssl-devel
  • 安装编译环境:

    yum install gcc gcc-c++ make -y     # 编译需要编译环境
  • 安装nginx

    ./configure \
    --prefix=/usr/share/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/run/nginx.pid \
    --lock-path=/run/lock/subsys/nginx \
    --user=nginx --group=nginx \
    --with-debug --with-file-aio \
    --with-http_addition_module \
    --with-http_auth_request_module \
    --with-http_dav_module \
    --with-http_degradation_module \
    --with-http_flv_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_mp4_module \
    --with-http_random_index_module \
    --with-http_realip_module \
    --with-http_secure_link_module \
    --with-http_slice_module \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_sub_module \
    --with-http_v2_module \
    --with-pcre --with-pcre-jit \
    --with-threads
  • 编译并安装

    make
    make install
  • 创建用户:

    useradd -r -c "Nginx web server" -d /var/lib/nginx -M -s /sbin/nologin nginx
    # -r:建立系统账号
    # -c:注释 
    # -d:家目录
    # -M:不要自动建立用户的登入目录
    # -s:指定shell
  • 编写nginx的服务脚本:

    cat << eof > /usr/lib/systemd/system/nginx.service
    [Unit]
    Description=nginx - high performance web server
    After=network.target remote-fs.target nss-lookup.target
    Description=Nginx
    Wants=network-online.target
    ​
    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    ExecStartPre=/usr/bin/rm -f /run/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t
    ExecStart=/usr/sbin/nginx
    ExecStop=/usr/sbin/nginx -s stop
    ExecReload=/usr/sbin/nginx -s reload
    PrivateTmp=true
    ​
    [Install]
    WanteBy=multi-user.target
    eof
  • 重载systemctl:

    systemctl daemon-reload
  • 重启:

    systemctl restart nginx

3.Docker安装:

  • 创建目录:

    mkdir ~/nginx;cd ~/nginx;mkdir conf
  • 复制别的nginx.conf文件,或者在conf目录下创建如下的nginx.conf配置文件文件:

    scp root@IP地址:/etc/nginx/nginx.conf ./conf/nginx.conf
    cat << eof > conf/nginx.conf
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    ​
    # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;
    ​
    events {worker_connections 1024;
    }
    ​
    http {log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';
    ​access_log  /var/log/nginx/access.log  main;
    ​sendfile            on;tcp_nopush          on;tcp_nodelay         on;keepalive_timeout   65;types_hash_max_size 2048;
    ​include             /etc/nginx/mime.types;default_type        application/octet-stream;
    ​# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf;
    ​server {listen       80 default_server;listen       [::]:80 default_server;server_name  _;root         /usr/share/nginx/html;
    ​# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;
    ​location / {}
    ​error_page 404 /404.html;location = /40x.html {}
    ​error_page 500 502 503 504 /50x.html;location = /50x.html {}}
    ​
    # Settings for a TLS enabled server.
    #
    #    server {
    #        listen       443 ssl http2 default_server;
    #        listen       [::]:443 ssl http2 default_server;
    #        server_name  _;
    #        root         /usr/share/nginx/html;
    #
    #        ssl_certificate "/etc/pki/nginx/server.crt";
    #        ssl_certificate_key "/etc/pki/nginx/private/server.key";
    #        ssl_session_cache shared:SSL:1m;
    #        ssl_session_timeout  10m;
    #        ssl_ciphers PROFILE=SYSTEM;
    #        ssl_prefer_server_ciphers on;
    #
    #        # Load configuration files for the default server block.
    #        include /etc/nginx/default.d/*.conf;
    #
    #        location / {
    #        }
    #
    #        error_page 404 /404.html;
    #            location = /40x.html {
    #        }
    #
    #        error_page 500 502 503 504 /50x.html;
    #            location = /50x.html {
    #        }
    #    }
    eof
  • 创建容器:

    docker run -id --name=c_nginx 
    -p 80:80 \
    -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
    -v $PWD/logs:/var/log/nginx \
    -v $PWD/html:/usr/share/nginx/html \
    nginx
  • 测试是否成功:在html目录下创建index.html文件,并访问:

    echo This is Hello > html/index.html
    http://192.168.178.52/index.html


http://www.ppmy.cn/news/48337.html

相关文章

Vue中的路由导航

声明式路由导航 router官网-起步 声明式路由导航其实就是使用官方给的<router-link>路由导航标签直接进行路由跳转 <body> <div id"app"><!--<router-link>路由导航标签&#xff0c;用于找到path属性中url对应的组件&#xff0c;通过传入…

Spring的循环依赖

什么是循环依赖&#xff1f; 循环依赖其实就是循环引用&#xff0c;也就是两个或者两个以上的 bean 互相持有对方&#xff0c;最终形成闭环。比如 A 依赖于 B&#xff0c;B 依赖于 C&#xff0c;C 又依赖于 A。如下图&#xff1a; 注意&#xff0c;这里不是函数的循环调用&…

rk3568-rk809电池电量计

简介&#xff1a; RK809 集成在RK3568上的一个高性能的 PMIC&#xff08;(Power Management IC):电源管理集成电路&#xff09;&#xff0c;PMIC全称Power management integrated circuit&#xff0c;一般情况下是一颗独立于主控的芯片&#xff0c;集成了电源控制&#xff0c;电…

Nginx rewrite ——重写跳转

Nginx常见模块 http http块是Nginx服务器配置中的重要部分&#xff0c;代理、缓存和日志定义等绝大多数的功能和第三方模块的配置都可以放在这模块中。作用包括&#xff1a;文件引入、MIME-Type定义、日志自定义、是否使用sendfile传输文件、连接超时时间、单连接请求数上限等…

不讲废话普通人了解 ChatGPT——基础篇第一课

wx供重浩&#xff1a;创享日记 获取更多内容 文章目录 前言什么是 ChatGPT它是如何工作的ChatGPT 和其它机器人有什么不同 前言 不知道大家在第一次会使用 ChatGPT 并尝试和他对话时有没有感到震惊。当ChatGPT首次推出时&#xff0c;我立即被它的功能所吸引。 曾经在遇到繁杂…

设计模式 -- 装饰模式

前言 月是一轮明镜,晶莹剔透,代表着一张白纸(啥也不懂) 央是一片海洋,海乃百川,代表着一块海绵(吸纳万物) 泽是一柄利剑,千锤百炼,代表着千百锤炼(输入输出) 月央泽,学习的一种过程,从白纸->吸收各种知识->不断输入输出变成自己的内容 希望大家一起坚持这个过程,也同…

【通知】CSDN学院:<华为流程体系课程> 正式上线啦!

目录 前言 适用人群 你将收获 课程介绍 前言 经过两个月的准备和短视频测试&#xff0c;这门介绍华为流程体系的课程就正式上线了。 虽然由于公开的原因&#xff0c;华为的发展受到了一定程度的影响&#xff0c;但是丝毫不妨碍企业、以及一些个人对学习华为的热情。 原因…

Qt扫盲-QAbstractSeries理论总结

QAbstractSeries理论总结 一、概述二、常用函数1. 属性2. 设置功能3. 显示隐藏4. 与 绘图的交互 三、信号 一、概述 QAbstractSeries类是所有Qt图表线的基类。通常&#xff0c;特定于序列类型的继承类会被使用&#xff0c;而不是这个基类。这个基类只是提供了一些管理和控制这…