CentOS7.X安装Nginx

简介: CentOS7.X安装Nginx

安装nginx

  1. 安装前的准备

    yum install \
    vim \
    gcc \
    gcc-c++ \
    wget \
    make \
    libtool \
    automake \
    autoconf \
    -y \
  2. 安装PCRE库

    cd /root
    wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
    或者(第三方):wget https://fossies.org/linux/misc/pcre-8.39.tar.gz
    tar -zxvf pcre-8.39.tar.gz
    cd pcre-8.39
    ./configure
    make
    make install
  3. 安装zlib库

    cd /root
    wget http://zlib.net/zlib-1.2.11.tar.gz
    tar -zxvf zlib-1.2.11.tar.gz
    cd zlib-1.2.11
    ./configure
    make
    make install
  4. 安装openssl

    # openssl从1.0.2开始支持http2
    cd /root
    wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz
    tar -zxvf openssl-1.0.2l.tar.gz
  5. 安装nginx

    # nginx在1.9.5开始支持http2
    cd /root
    wget http://nginx.org/download/nginx-1.13.2.tar.gz
    tar -zxvf nginx-1.13.2.tar.gz
    cd nginx-1.13.2
    
    ---------------------------------------------------------
    # 安装前小提示:如果要隐藏Web服务名称'nginx',可以使用以下方法
    vim ./src/http/ngx_http_header_filter_module.c
    ngx_http_server_string[] = "Server: nginx" CRLF;
    # 改为
    ngx_http_server_string[] = "Server: My web" CRLF;
    ESC
    :wq
    
    vim ./src/http/ngx_http_special_response.c
    <hr><center>nginx</center>
    # 改为
    <center><h1>My web</h1></center>
    # 并且
    static u_char ngx_http_msie_padding[] =
    "<!-- a padding to disable MSIE and Chrome friendly error page -->" CRLF
    "<!-- a padding to disable MSIE and Chrome friendly error page -->" CRLF
    "<!-- a padding to disable MSIE and Chrome friendly error page -->" CRLF
    "<!-- a padding to disable MSIE and Chrome friendly error page -->" CRLF
    "<!-- a padding to disable MSIE and Chrome friendly error page -->" CRLF
    "<!-- a padding to disable MSIE and Chrome friendly error page -->" CRLF;
    # 改为
    static u_char ngx_http_msie_padding[] = "<!-- My web -->" CRLF;
    ESC
    :wq
    ---------------------------------------------------------
    
    ./configure \
    --prefix=/usr/local/nginx/ \
    --with-http_v2_module \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_stub_status_module \
    --with-pcre=../pcre-8.39/ \
    --with-zlib=../zlib-1.2.11/ \
    --with-openssl=../openssl-1.0.2l/ \
    
    make
    make install
  6. 开启80端口(方式1)

    netstat -ano|grep 80
    iptables -t filter -A INPUT -p tcp -j ACCEPT
  7. 开启80端口(方式2)

    yum install firewalld
    systemctl enable firewalld
    systemctl start firewalld
    firewall-cmd --zone=public --add-port=80/tcp --permanent
    firewall-cmd --zone=public --add-port=443/tcp --permanent
    firewall-cmd --reload
  8. 修改nginx配置

    vim /usr/local/nginx/conf/nginx.conf
    
    user                www;
    worker_processes    auto;
    worker_cpu_affinity auto;
    pid                 logs/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    http {
        charset         utf-8;
        server_tokens   off;
        include         mime.types;
        default_type    application/octet-stream;
    
        access_log         logs/access.log;
        error_log          logs/error.log;
        sendfile           on;
        keepalive_timeout  65;
        gzip               on;
    
        server {
            listen       80;
            server_name  www.test.com;
            root         /www;
    
            location / {
                index    index.php index.html index.htm;
            }
    
            location ~ \.php$ {
                fastcgi_index  index.php;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
        }
    }
    
    ESC
    :wq
  9. 如果没有www用户,创建www用户

    useradd www
  10. 启动nginx并设置开机启动

    # 将nginx目录所属用户设置为www
    mkdir /www
    chown -R www:www /www
    chown -R www:www /usr/local/nginx
    
    # 进入单元文件目录
    cd /etc/systemd/system
    
    # 创建nginx单元文件,格式为: [单元文件名].[单元文件类型]
    vim nginx.service
    
    [Unit]
    Description=Start nginx on boot.
    After=network.target
    
    [Service]
    User=root
    Group=root
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s quit
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
    ESC
    :wq
    
    # 修改文件权限为只有root用户可以编辑该文件
    chown -R root:root /etc/systemd/system/nginx.service
    chmod -R 644 /etc/systemd/system/nginx.service
    
    # 更新systemd
    systemctl daemon-reload
    systemctl enable nginx
    systemctl start nginx
  11. nginx操作

    /usr/local/nginx/sbin/nginx    启动
    /usr/local/nginx/sbin/nginx -t 检查当前配置错误
    /usr/local/nginx/sbin/nginx -s reload 重载配置
    /usr/local/nginx/sbin/nginx -s reopen 重开日志
    /usr/local/nginx/sbin/nginx -s quit   正常关闭
    /usr/local/nginx/sbin/nginx -s stop   强制关闭
  12. 领支付宝红包支持作者

    扫码领支付宝红包

相关文章
|
4天前
|
前端开发 jenkins 持续交付
新的centos7.9安装docker版本的jenkins2.436.1最新版本-前端项目发布(五)
新的centos7.9安装docker版本的jenkins2.436.1最新版本-前端项目发布(五)
20 1
|
4天前
|
jenkins 网络安全 持续交付
新的centos7.9安装docker版本的jenkins2.436.1最新版本-后端项目发布(四)
新的centos7.9安装docker版本的jenkins2.436.1最新版本-后端项目发布(四)
17 3
|
4天前
|
网络协议 Linux Docker
在centos7下通过docker 安装onlyoffice
在centos7下通过docker 安装onlyoffice
29 0
|
1天前
|
安全 关系型数据库 MySQL
解决centos7.0安装mysql后出现access defind for user@'localhost'的错误
在使用yum 安装完mariadb, mariadb-server, mariadb-devel后
7 0
|
2天前
|
Linux 网络安全 数据安全/隐私保护
centos7安装gitlab-ce社区版全过程,详细到爆炸,这些面试官常问的开发面试题你都掌握好了吗
centos7安装gitlab-ce社区版全过程,详细到爆炸,这些面试官常问的开发面试题你都掌握好了吗
|
4天前
|
NoSQL Linux Redis
在CentOS上安装和配置Redis
在CentOS上安装和配置Redis
46 0
|
4天前
|
安全 Linux 测试技术
在CentOS上安装Elasticsearch和Kibana
在CentOS上安装Elasticsearch和Kibana
11 0
|
4天前
|
运维 Kubernetes 监控
本地CentOS安装轻量级容器PaaS平台KubeSphere并实现无公网IP远程访问
本地CentOS安装轻量级容器PaaS平台KubeSphere并实现无公网IP远程访问
6 0
|
4天前
|
Kubernetes Docker 容器
Docker 安装 Portainer
Portainer Community Edition是一个针对容器化应用程序的轻量级服务交付平台,可用于管理 Docker、Swarm、Kubernetes 和 ACI 环境。它的设计理念是部署和使用都简单,该应用程序允许您通过“智能”GUI 和/或广泛的 API 管理所有编排器资源。
42 3
|
4天前
|
Java 开发工具 git
新的centos7.9安装docker版本的jenkins2.436.1最新版本-项目发布(三)
新的centos7.9安装docker版本的jenkins2.436.1最新版本-项目发布(三)
12 4

热门文章

最新文章