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

本文介绍了在Centos7系统中通过yum、源码以及Docker三种方式安装Nginx的详细步骤,包括配置yum源、下载编译安装Tengine以及使用Docker创建容器运行Nginx服务。
摘要由CSDN通过智能技术生成

目录

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值