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. 领支付宝红包支持作者

    扫码领支付宝红包

相关文章
|
11天前
|
存储 安全 Linux
CentOS安装SeaweedFS
通过上述步骤,您应该能够在CentOS系统上成功安装并启动SeaweedFS。记住,根据实际部署规模和需求,可能还需要进一步调整配置参数和优化网络布局。SeaweedFS的灵活性和扩展性意味着随着使用深入,您可能需要探索更多高级配置和管理策略。
94 64
|
12天前
|
存储 安全 Linux
CentOS安装SeaweedFS
通过上述步骤,您应该能够在CentOS系统上成功安装并启动SeaweedFS。记住,根据实际部署规模和需求,可能还需要进一步调整配置参数和优化网络布局。SeaweedFS的灵活性和扩展性意味着随着使用深入,您可能需要探索更多高级配置和管理策略。
100 61
|
5天前
|
Linux 网络安全 数据安全/隐私保护
Linux系统之Centos7安装cockpit图形管理界面
【10月更文挑战第12天】Linux系统之Centos7安装cockpit图形管理界面
24 1
Linux系统之Centos7安装cockpit图形管理界面
|
11天前
|
负载均衡 应用服务中间件 Linux
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
这篇博客文章详细介绍了Nginx的下载、安装、配置以及使用,包括正向代理、反向代理、负载均衡、动静分离等高级功能,并通过具体实例讲解了如何进行配置。
61 4
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
|
11天前
|
NoSQL 数据可视化 Linux
redis学习四、可视化操作工具链接 centos redis,付费Redis Desktop Manager和免费Another Redis DeskTop Manager下载、安装
本文介绍了Redis的两个可视化管理工具:付费的Redis Desktop Manager和免费的Another Redis DeskTop Manager,包括它们的下载、安装和使用方法,以及在使用Another Redis DeskTop Manager连接Redis时可能遇到的问题和解决方案。
36 1
redis学习四、可视化操作工具链接 centos redis,付费Redis Desktop Manager和免费Another Redis DeskTop Manager下载、安装
|
8天前
|
NoSQL Linux Redis
Docker学习二(Centos):Docker安装并运行redis(成功运行)
这篇文章介绍了在CentOS系统上使用Docker安装并运行Redis数据库的详细步骤,包括拉取Redis镜像、创建挂载目录、下载配置文件、修改配置以及使用Docker命令运行Redis容器,并检查运行状态和使用Navicat连接Redis。
64 3
|
8天前
|
关系型数据库 MySQL Linux
在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤
本文介绍了在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置服务等,并与使用 RPM 包安装进行了对比,帮助读者根据需求选择合适的方法。编译源码安装虽然复杂,但提供了更高的定制性和灵活性。
64 2
|
10天前
|
关系型数据库 MySQL Linux
在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤
【10月更文挑战第7天】本文介绍了在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。同时,文章还对比了编译源码安装与使用 RPM 包安装的优缺点,帮助读者根据自身需求选择合适的方法。
19 3
|
9天前
|
应用服务中间件 Linux nginx
Mac os 安装 nginx 教程(success)
这篇文章是关于如何在Mac OS系统上使用Homebrew安装nginx及其依赖,并解决安装过程中可能出现的权限问题。
22 0
Mac os 安装 nginx 教程(success)
|
15天前
|
Kubernetes Linux 开发工具
centos7通过kubeadm安装k8s 1.27.1版本
centos7通过kubeadm安装k8s 1.27.1版本