zoukankan      html  css  js  c++  java
  • CentOS7 安装 nginx 及基本配置

    Ø  简介

    本文记录 CentOS7 环境下安装 nginx 的具体步骤,以及相关配置和使用。

    1.   安装所需环境

    2.   安装 nginx

    3.   nginx 管理命令

    4.   使用 systemctl 命令管理 nginx

     

    n  注意事项

    为了避免麻烦,建议使用 root 用户进行安装。因为 nginx 默认监听80端口,但是在是 Linux 中只有 root 才能使用 1024 以下的端口。

     

    1.   安装所需环境

    1)   安装 gcc

    安装的 nginx C 语言开发的源码包,所以需要一个 C 编译器才能安装。

    yum install gcc-c++

     

    2)   安装 pcre pcre-devel

    PCRE(Perl Compatible Regular Expressions)是一个 perl 库,包括 perl 兼容的正则表达式库。nginx http 模块使用 pcre 来解析正则表达式,所以需要先安装 pcre 库。

    pcre-devel 是使用 pcre 开发的一个二次开发库,nginx 也需要此库。

    yum install -y pcre pcre-devel

     

    3)   安装 zlib

    zlib 库提供了很多种压缩和解压缩的方式,nginx 使用 zlib http 包的内容进行 gzip

    yum install -y zlib zlib-devel

     

    4)   安装 OpenSSL

    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。

    nginx 不仅支持 http 协议,还支持 https(即在 ssl 协议上传输 http),但是需要在 OpenSSL 库的支持。

    yum install -y openssl openssl-devel

     

    2.   安装 nginx

    1)   下载源码包

    wget -P /opt/soft https://nginx.org/download/nginx-1.18.0.tar.gz

    官网下载地址:https://nginx.org/en/download.html

    目前的稳定版本:nginx-1.18.0

     

    2)   解压

    cd /opt/soft

    # 指定 --no-same-owner 参数防止改变所有者、所属组

    tar --no-same-owner -zxvf nginx-1.18.0.tar.gz

     

    3)   配置 nginx

    1.   使用默认配置安装(root 用户安装)【推荐】

    cd nginx-1.18.0

    ./configure

    ./configure --with-http_ssl_module     #如果需要使用 HTTPS Server

    #默认的安装配置信息

    nginx path prefix: "/usr/local/nginx"

    nginx binary file: "/usr/local/nginx/sbin/nginx"

    nginx modules path: "/usr/local/nginx/modules"

    nginx configuration prefix: "/usr/local/nginx/conf"

    nginx configuration file: "/usr/local/nginx/conf/nginx.conf"

    nginx pid file: "/usr/local/nginx/logs/nginx.pid"

    nginx error log file: "/usr/local/nginx/logs/error.log"

    nginx http access log file: "/usr/local/nginx/logs/access.log"

    nginx http client request body temporary files: "client_body_temp"

    nginx http proxy temporary files: "proxy_temp"

    nginx http fastcgi temporary files: "fastcgi_temp"

    nginx http uwsgi temporary files: "uwsgi_temp"

    nginx http scgi temporary files: "scgi_temp"

     

    2.   或者指定配置参数(应该对应于普通用户安装)

    mkdir -p /opt/nginx

    cd nginx-1.18.0

    ./configure

    --prefix=/usr/local/nginx      #指定安装路径

    --conf-path=/usr/local/nginx/conf/nginx.conf

    --pid-path=/usr/local/nginx/conf/nginx.pid

    --lock-path=/var/lock/nginx.lock

    --error-log-path=/var/log/nginx/error.log

    --http-log-path=/var/log/nginx/access.log

    --with-http_gzip_static_module

    --http-client-body-temp-path=/var/temp/nginx/client

    --http-proxy-temp-path=/var/temp/nginx/proxy

    --http-fastcgi-temp-path=/var/temp/nginx/fastcgi

    --http-uwsgi-temp-path=/var/temp/nginx/uwsgi

    --http-scgi-temp-path=/var/temp/nginx/scgi

     

    4)   编译安装

    make

    make install

     

    /usr/local/nginx/sbin/nginx     #安装成功,启动 ngixn

    就可以正常被访问了,端口采用默认80端口

    clip_image002

     

    5)   设置命令别名

    设置别名是为了方便使用,否则每次执行命令必须输入绝对路径,比如:

    /usr/local/nginx/sbin/nginx     #启动 ngixn

     

    vi ~/.bash_profile             #编辑 .bash_profile 文件

    alias nginx='/usr/local/nginx/sbin/nginx'   #加入别名

    source ~/.bash_profile         #使之生效

     

    6)   检查 nginx 配置文件

    nginx -t

    clip_image004

     

    7)   设置 nginx 配置文件的路径

    nginx -t /usr/local/nginx/conf/nginx.conf

     

    3.   nginx 管理命令

    nginx                  #启动 nginx

    nginx -s quit           #停止(待任务处理完毕后停止)

    nginx -s stop           #停止(相当于 pkill nginx

    nginx -s reload         #重启(重新载入配置文件)

     

    1)   查看 nginx 进程

    ps -aux | grep nginx

    clip_image006

     

    2)   查看 nginx 版本

    nginx -v

    nginx version: nginx/1.18.0

     

    3)   查看/编辑 nginx 配置文件

    vi /usr/local/nginx/conf/nginx.conf

    clip_image008

     

    4.   使用 systemctl 命令管理 nginx

    1)   首先,在系统服务目录中创建 nginx.service 文件

    vi /usr/lib/systemd/system/nginx.service    #写入以下配置

    [Unit]

    Description=nginx service

    After=network.target

     

    [Service]

    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

     

    配置说明:

    [Unit]          服务的说明

    Description     描述服务

    After           描述服务类别

    [Service]       服务运行参数的设置

    Type=forking    是后台运行的形式

    ExecStart       服务的运行命令(绝对路径)

    ExecReload      服务的重启命令(绝对路径)

    ExecStop        服务的停止命令(绝对路径)

    PrivateTmp      是否给服务分配独立的临时空间

    [Install]       运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

     

    2)   设置开机自启动

    systemctl enable nginx          #开启自启动

    systemctl disable nginx         #禁止自启动

     

    3)   systemctl 相关命令

    systemctl status nginx          #查看服务状态

    clip_image010

    systemctl start nginx           #启动服务

    systemctl stop nginx            #停止服务

    systemctl restart nginx         #重启服务

    systemctl list-units --type=service     #查看所有已启动的服务

     

    n  注意事项

    发现一个问题,如果使用 nginx 命令去启动了 nginx,就不能使用 systemctl 命令去管理,否则可能出现以下错误:

    clip_image012

    但此时,nginx 是正常运行的,且可以访问的。

    结论:要么使用 nginx 命令,要么使用 systemctl 命令去管理,最好不要混用。

  • 相关阅读:
    JavaWeb网上商城项目中用户注册,使用MailServer和FoxMail搭建本地邮件服务器
    myeclipse编码问题
    Date日期类型的绑定
    springmvc学习之jdk版本,tomcat版本,spring版本
    mybatis-ehcache整合中出现的异常 ibatis处理器异常(executor.ExecutorException)解决方法
    .net里面<app.config>中value值不能填写特殊符号问题
    sqldeveloper中Excel数据的导入与导出
    IntelliJ IDEA 2019.2最新版本免费激活码(转)
    sqlserver 的一些小总结
    SQL 跨数据库同步数据 、跨数据库跨更新数据
  • 原文地址:https://www.cnblogs.com/abeam/p/13694056.html
Copyright © 2011-2022 走看看