zoukankan      html  css  js  c++  java
  • Linux下nginx安装

    1.下载源码程序安装包

    下载地址:http://nginx.org
    C:UsersAdministratorAppDataRoamingTypora	ypora-user-images1562812844503.png)]

    • 版本分类

    在这里插入图片描述

    2.安装步骤

    • 在opt目录下新建tools,apps文件夹
    mkdir /opt/tools      #存放以后要安装的程序
    mkdir /opt/apps       #存放程序以后解压的目录
    
    • 上传nginx源码安装包

      通过 rz 或者 xshell 进行上传
      在这里插入图片描述

    • 解压

     tar -xzvf /opt/toolsnginx-1.16.0.tar.gz  -C  /opt/apps/
    
    • 安装gcc

    nginx是用c语言写,对于c/c++需要编译器使用对多的就是gcc与gcc-c++

    如果没有安装gcc直接编译会报

    ./configure: error: C compiler cc is not found

    yum install -y gcc  gcc-c++
    
    • 安装依赖库
    yum install -y pcre-devel openssl-devel
    

    pcre-devel: Perl脚本语言兼容正则表达式,为nginx提供正则表达式
    openssl-devel:为nginx提供SSL(安全套接字)

    • 生成makefile文件

    编译命令make需要根据编译文件makefile进行编译,所以在编译之前需要先生成编译文件makefile。使用configure命令可以生成该文件。

    执行如下命令,查看相关基本配置

    [root@localhost nginx-1.16.0]# ./configure --help
    

    A:基本配置类

    在这里插入图片描述

    其中 --prefix为执行nginx安装目录

    ​B:默认没有安装,可以指定安装模块,都是--with开头的
    在这里插入图片描述

    C:默认已经安装的,可以卸载指定模块,---without开头的
    在这里插入图片描述

    生成makefile文件

    [root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
    

    在以上命令中,指定了nginx的安装目录,同时安装了ssl模块。生成makefile文件成功会显示以下信息

    (nginx安装目录)nginx path prefix: "/usr/local/nginx"              
    (nginx命令文件)nginx binary file: "/usr/local/nginx/sbin/nginx"
    (nginx模块存放路径)nginx modules path: "/usr/local/nginx/modules"
    (nginx配置存放路径)nginx configuration prefix: "/usr/local/nginx/conf"
    (nginx配置文件存放路径)nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
    (nginx进行id文件)nginx pid file: "/usr/local/nginx/logs/nginx.pid"
    (nginx错误日志)nginx error log file: "/usr/local/nginx/logs/error.log"
    (nginx中Http访问路径)nginx http access log file: "/usr/local/nginx/logs/access.log"
    (nginx http请求相关文件)nginx http client request body temporary files: "client_body_temp"
    (nginx http请求相关文件))nginx http proxy temporary files: "proxy_temp"
    (nginx http请求相关文件)nginx http fastcgi temporary files: "fastcgi_temp"
    (nginx http请求相关文件)nginx http uwsgi temporary files: "uwsgi_temp"
    (nginx http请求相关文件)nginx http scgi temporary files: "scgi_temp"
    

    执行完成上面之后会发现解压目录下多了一个makefile文件

    在这里插入图片描述

    • 执行编译和安装

      [root@localhost nginx-1.16.0]# make && make install
      
    • 使nginx命令随处可用

    在nginx安装目录/usr/local/nginx中有一个sbin目录,存放着nginx的命令程序nginx

    [root@localhost sbin]# ll /usr/local/nginx/sbin/
    总用量 5736
    -rwxr-xr-x. 1 root root 5872632 7月  11 10:46 nginx
    

    在这里插入图片描述

    默认情况下执行nginx相关命令需要在/usr/local/nginx/sbin/目录中进行,很不方便。

    以后我们采用将nginx命令的软连接文件添加到系统文件变量PATH的路径中

    [root@localhost nginx-1.16.0]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    

    添加nginx软连接

    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
    

    查看刚才添加的软连接

    [root@localhost sbin]# ll /usr/local/sbin/
    总用量 0
    lrwxrwxrwx. 1 root root 27 7月  11 10:48 nginx -> /usr/local/nginx/sbin/nginx
    

    通过以上一系列操作nginx已经安装成功了。

    3.nginx命令

    • 查看命令选项 nginx -h
    [root@localhost sbin]# nginx -h
    nginx version: nginx/1.16.0
    Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
    
    Options:
      -?,-h         : this help             (帮助)
      -v            : show version and exit  
      -V            : show version and configure options then exit
      -t            : test configuration and exit
      -T            : test configuration, dump it and exit
      -q            : suppress non-error messages during configuration testing(在配置测试期间禁止显示非错误消息,意思就是只显示错误信息)
      -s signal     : send signal to a master process: stop, quit, reopen, reload
      -p prefix     : set prefix path (default: /usr/local/nginx/)
      -c filename   : set configuration file (default: conf/nginx.conf)
      -g directives : set global directives out of configuration file
    
    • nginx -c file
    nginx
    

    启动nginx,启动成功不会显示任何信息,如果不使用-c file 默认使用conf/nginx.conf

    [root@localhost sbin]# ps -aux|grep nginx
    root      21558  0.0  0.0  45952  1128 ?        Ss   10:50   0:00 nginx: master process nginx -c /usr/local/nginx/conf/nginx.conf
    nobody    21559  0.0  0.1  48492  1972 ?        S    10:50   0:00 nginx: worker process
    root      22176  0.0  0.0 112724   992 pts/0    S+   11:35   0:00 grep --color=auto nginx
    

    在这里插入图片描述

    有两个进程,一个主进程,一个工作进程

    • nginx -s stop/quit

      nginx -s stop 强制停止nginx,比较暴力的方式,不管当前工作进程是否完成(不推荐的方式)

      nginx -s quit 优雅的停止nginx,比较温和的方式,使当前工作进程执行完成。

    • nginx -s reload

    在不重启nginx的前提下重新下载nginx配置

    • nginx -v 查询nginx安装版本
      [root@localhost sbin]# nginx -v
      nginx version: nginx/1.16.0
    
    • nginx -t 测试配置文件是否正确,默认只测试默认的配置文件conf/ngin.conf
      [root@localhost sbin]# nginx -t
      nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
      nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    

    可以结合-c指定要测试的配置文件,但其不会启动nginx

    [root@localhost sbin]# nginx -c /usr/local/nginx/conf/nginx.conf -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    

    4.初始化测试

    nginx启动成功,通过浏览器器访问。

    在这里插入图片描述
    注意:必须关闭防火墙

    • 查询防火墙状态
      [root@localhost sbin]# firewall-cmd --state
      running   #防火墙运行中
    
    • 关闭防火墙
    [root@localhost sbin]# systemctl stop  firewalld.service 
    
    [root@localhost sbin]# firewall-cmd --state
    not running   #防火墙没有运行
    

    微信公众号

    JAVA程序猿成长之路
    JAVA程序猿成长之路
    分享资源,记录程序猿成长点滴。专注于Java,Spring,SpringBoot,SpringCloud,分布式,微服务。

  • 相关阅读:
    python 网页cookie的使用
    python PIL 图像处理操作
    numpy linalg模块
    Robot Framework自动化测试Selenium2Library库详细用法
    Numpy 基本除法运算和模运算
    Java基础系列--基础排序算法
    java基础系列--SecurityManager入门(转)
    Java基础系列--ArrayList集合
    Java基础系列--instanceof关键字
    Java基础系列--throw、throws关键字
  • 原文地址:https://www.cnblogs.com/niugang0920/p/12186332.html
Copyright © 2011-2022 走看看