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

    扫码领支付宝红包

相关文章
|
5天前
|
缓存 Ubuntu Linux
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
通过本文,我们详细了解了 `yum`、`rpm`、`apt-get`和 `wget`的区别、常用命令以及在CentOS和Ubuntu中安装 `wget`的方法。`yum`和 `apt-get`是高层次的包管理器,分别用于RPM系和Debian系发行版,能够自动解决依赖问题;而 `rpm`是低层次的包管理工具,适合处理单个包;`wget`则是一个功能强大的下载工具,适用于各种下载任务。在实际使用中,根据系统类型和任务需求选择合适的工具,可以大大提高工作效率和系统管理的便利性。
54 25
|
15天前
|
监控 Linux PHP
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
65 20
|
1月前
|
前端开发 应用服务中间件 nginx
docker安装nginx,前端项目运行
通过上述步骤,你可以轻松地在Docker中部署Nginx并运行前端项目。这种方法不仅简化了部署流程,还确保了环境的一致性,提高了开发和运维的效率。确保按步骤操作,并根据项目的具体需求进行相应的配置调整。
148 25
|
1月前
|
存储 分布式计算 Hadoop
Centos7.9安装kerberos
Centos7.9安装kerberos
86 25
|
1月前
|
存储 Shell 网络安全
Centos7.9安装openldap
Centos7.9安装openldap
58 16
|
1月前
|
数据可视化 Linux 应用服务中间件
Centos7.9安装phpldapadmin
Centos7.9安装phpldapadmin
62 15
|
1月前
|
网络协议 Java 应用服务中间件
centos7环境下tomcat8的安装与配置
本文介绍了在Linux环境下安装和配置Tomcat 8的详细步骤。首先,通过无网络条件下的文件交互软件(如Xftp 6或MobaXterm)下载并解压Tomcat安装包至指定路径,启动Tomcat服务并测试访问。接着,修改Tomcat端口号以避免冲突,并部署Java Web应用项目至Tomcat服务器。最后,调整Linux防火墙规则,确保外部可以正常访问部署的应用。关键步骤包括关闭或配置防火墙、添加必要的端口规则,确保Tomcat服务稳定运行。
|
2月前
|
负载均衡 Ubuntu 应用服务中间件
nginx修改网站默认根目录及发布(linux、centos、ubuntu)openEuler软件源repo站点
通过合理配置 Nginx,我们可以高效地管理和发布软件源,为用户提供稳定可靠的服务。
217 13
|
3月前
|
SQL 存储 Linux
从配置源到数据库初始化一步步教你在CentOS 7.9上安装SQL Server 2019
【11月更文挑战第16天】本文介绍了在 CentOS 7.9 上安装 SQL Server 2019 的详细步骤,包括配置系统源、安装 SQL Server 2019 软件包以及数据库初始化,确保 SQL Server 正常运行。
168 4
|
3月前
|
应用服务中间件 网络安全 nginx
轻松上手Nginx Proxy Manager:安装、配置与实战
Nginx Proxy Manager (NPM) 是一款基于 Nginx 的反向代理管理工具,提供直观的 Web 界面,方便用户配置和管理反向代理、SSL 证书等。本文档介绍了 NPM 的安装步骤,包括 Docker 和 Docker Compose 的安装、Docker Compose 文件的创建与配置、启动服务、访问 Web 管理界面、基本使用方法以及如何申请和配置 SSL 证书,帮助用户快速上手 NPM。
1272 1