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

news/2024/7/7 21:47:30 标签: nginx, docker, 运维

目录

nginx-toc" style="margin-left:40px;">1.yum安装:Centos7源默认没有nginx

2.源码安装:

3.Docker安装:


nginx">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.niftyadmin.cn/n/239458.html

相关文章

【Linux阅读笔记】LinuxC一站式编程2-数据类型、运算符与汇编基本

目录 数据类型分析浮点型类型转换 运算符分析移位问题异或运算特性 计算机体系结构MMU 内存管理单元 汇编基本最简汇编程序汇编语法分异x86 寄存器求最值汇编寻址方式ELF 文件 数据类型分析 浮点型 浮点数在不同平台上实现不同 有的处理器有浮点运算单元&#xff08;Floating…

领跑行泊一体,纵目科技剑指自动驾驶L2到L4的规模化商业落地机遇

‍数据智能产业创新服务媒体 ——聚焦数智 改变商业 2019年&#xff0c;通用、丰田、特斯拉等11家车企承诺自动驾驶时间表&#xff0c;他们大都表示在2020年底实现高级别自动驾驶。以特斯拉为例&#xff0c;其CEO埃隆马斯克曾承诺在2020年实现自动驾驶食言后&#xff0c;随后在…

API 鉴权都有哪些分类,这些重点不要错过

API鉴权是保证API安全性和可用性的一项重要措施。通过API鉴权&#xff0c;系统可以对用户或者应用进行有效的身份认证和权限管理。一般来说&#xff0c;在实际开发中&#xff0c;我们使用以下几种API鉴权方式&#xff1a; 1. 基本认证 基本认证是API鉴权的一种最基本形式。此方…

论:开发者信仰之“天下IT是一家“(Java .NET篇)

比尔盖茨公认的IT界领军人物&#xff0c;打造了辉煌一时的PC时代。 2008年&#xff0c;史蒂夫鲍尔默接替了盖茨的工作&#xff0c;成为微软公司的总裁。 2013年他与微软做了最后的道别。 2013年以后&#xff0c;我才真正看到了微软的变化。尤其是它的“云优先&#xff0c;移…

重学Java设计模式-行为型模式-迭代器模式

重学Java设计模式-行为型模式-迭代器模式 内容摘自&#xff1a;https://bugstack.cn/md/develop/design-pattern/2020-06-23-重学 Java 设计模式《实战迭代器模式》.html#重学-java-设计模式-实战迭代器模式「模拟公司组织架构树结构关系-深度迭代遍历人员信息输出场景」 迭代…

抖音滑块以及轨迹分析

声明 本文以教学为基准、本文提供的可操作性不得用于任何商业用途和违法违规场景。 本人对任何原因在使用本人中提供的代码和策略时可能对用户自己或他人造成的任何形式的损失和伤害不承担责任。 如有侵权,请联系我进行删除。 我们在web端打开用户主页的时候,时不时的会出现滑…

内存的分区

目录 内存分区介绍 区域功能 内存分区运行前后的区别 运行之前&#xff08;代码区数据区未初始化数据区&#xff09; 运行之后&#xff08;代码区数据区未初始化数据区栈区堆区&#xff09; 缓冲区 缓冲区有什么用&#xff1f; 缓冲区的三种类型 缓冲区的刷新 内存分布图 栈与堆…

Salesforce如何防止黑客攻击和数据泄露?了解他们的安全措施!

安全性一直是Salesforce密切关注的问题。Google的安全浏览报告指出&#xff0c;2022年网络钓鱼网站的数量增加了80&#xff05;。面对着黑客攻击、安全漏洞、数据泄露等不安全事件频发&#xff0c;实施更强大的安全措施比以往更加重要。 调查显示&#xff0c;电子邮件目前是网…