安装nginx

  1. 官网下载对应的nginx包,推荐使用稳定版本。
  2. 上传下载好的包到服务器
  3. 安装依赖环境
    1. 安装gcc环境。
      yum install gcc-c++
    2. 安装PCRE库,用于解析正则表达式。
      yum install -y pcre pcre-devel
    3. zlib压缩和解压缩依赖。
      yum install -y zlib zlib-devel
    4. SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https。
      yum install -y openssl openssl-devel
  4. 解压缩,解压后得到的是源码,需要对源码进行编译后才可以安装
    tar -zxvf nginx-1.16.1.tar.gz
  5. 编译之前先创建临时目录,如果不创建,在启动过程中将会报错
    mkdir /var/temp/nginx -p
  6. 进入到nginx解压缩后的目录,输入如下命令进行配置,目的是为了创建makefile文件
    ./configure \ --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx/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

    配置命令参数详解:

    安装nginx
  7. make编译、安装

    make

    make install

  8. 进入sbin目录启动nginx

    ./nginx
    
    * 停止:./nginx -s stop
    * 重新加载:./nginx -s reload
  9. 打开浏览器,访问虚拟机所处内网ip即可打开nginx默认页面。
  10. 在本地虚拟机进行操作,记得关闭防火墙。在云服务器进行操作,记得开放80端口。
  11. nginx常用命令:
    查看nginx版本信息:
    #简略信息,只显示版本号
    [ sbin]# ./nginx -v
    nginx version: nginx/1.16.1
    
    #详细信息,包括版本号,编译版本/工具(GCC),配置参数(configure arguments)。
    [ sbin]# ./nginx -V
    nginx version: nginx/1.16.1
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
    configure arguments: --prefix=/usr/local/nginx --pid-path=/var/run/nginx/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
    测试nginx配置是否正确:
    [ 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
    
    #./nginx -T 将会显示详细的配置信息(include文件也会显示)
    启动、停止和重新启动nginx:
    #启动
    [ sbin]# ./nginx
    
    #停止-方式一,强制停止,当前有正在处理的请求也会被关闭掉。类似于饭店要关门,即使还有客人在吃饭,直接将其赶出去。
    [ sbin]# ./nginx -s stop
    
    #停止-方式二,不再接受新的请求,处理完当前正在执行的请求后关闭。类似于饭店要关门,会等到当前店内客人吃完饭之后关门,同时不再招待新的客人。
    [ sbin]# ./nginx -s quit
    
    #重新启动/加载。当修改了配置文件后,使用该命令进行重载。
    [ sbin]# ./nginx -s reload

相关推荐