Centos7安装nginx(一)

6 篇文章 0 订阅
6 篇文章 0 订阅

一、简介

  • 需求:本人最近做一些实验,需要在宿主机和虚拟机之间进行通信,因此选择nginx来做反向代理。
  • 内容:本人尝试了在虚拟机中安装nginx、在虚拟机的docker容器中安装nginx、使用shell脚本自动化安装nginx。因此,将分为三篇文章记录和分享在安装nginx过程中的感想以及遇到的问题。
  • 环境:虚拟机 + centos

二 、安装过程

安装过程主要分为三部分:

  • 初始化安装环境
  • 安装nginx
  • 配置systemctl,使用systemctl启动、停止nginx
1、初始化安装环境

注意:为了避免不必要的麻烦安装前先关闭防火墙systemctl stop firewalld

  1. yum install gcc-c++
  2. yum install -y pcre pcre-devel
  3. yum install -y zlib zlib-devel
  4. yum install -y openssl openssl-devel
2、安装nginx
  1. wget http://nginx.org/download/nginx-1.14.0.tar.gz (下载)
  2. tar -zxvf nginx-1.14.0.tar.gz (解压)
  3. cd到文件路径(cd nginx-1.14.0)(切换路径)
  4. ./configure --prefix=/usr --sbin-path=/usr/local/sbin/nginx --conf-path=/usr/local/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/usr/local/run/nginx/nginx.pid --lock-path=/usr/local/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tem/nginx/client --http-proxy-temp-path=/var/tem/nginx/proxy --http-fastcgi-temp-path=/var/tem/nginx/fcgi --with-http_stub_status_module (配置nginx相关参数,以下是参数的具体意思)
	nginx path prefix: "/usr"
	nginx binary file: "/usr/local/sbin/nginx"
	nginx modules path: "/usr/modules"
	nginx configuration prefix: "/usr/local/nginx"
	nginx configuration file: "/usr/local/nginx/nginx.conf"
	nginx pid file: "/usr/local/run/nginx/nginx.pid"
	nginx error log file: "/var/log/nginx/error.log"
	nginx http access log file: "/var/log/nginx/access.log"
	nginx http client request body temporary files: "/var/tem/nginx/client"
	nginx http proxy temporary files: "/var/tem/nginx/proxy"
	nginx http fastcgi temporary files: "/var/tem/nginx/fcgi"
	nginx http uwsgi temporary files: "uwsgi_temp"
	nginx http scgi temporary files: "scgi_temp"
  1. 安装

    • make
    • make install
  2. 启动\关闭

    • nginx -c + 配置文件路径(nginx -c /usr/local/nginx/nginx.conf
    • 启动nginx
      • /usr/local/sbin/nginx -c /usr/local/nginx/nginx.conf
    • 快速停止nginx
      • /usr/local/nginx/sbin/nginx -s stop
    • 完整有序的停止nginx
      • /usr/local/nginx/sbin/nginx -s quit
    • 注:stop和quit的区别在于
      • quit是一个优雅的关闭方式,Nginx在退出前完成已经接受的连接请求
      • Stop 是快速关闭,不管有没有正在处理的请求。
    • 进程的方式
      • pkill -9 nginx
      • ps -ef | grep nginx
      • kill -QUIT 主进程号 :从容停止Nginx
      • kill -TERM 主进程号 :快速停止Nginx
      • pkill -9 nginx :强制停止Nginx
      • 平滑重启nginx:
      • kill -HUP 主进程号
3、配置systemctl,使用systemctl启动、停止nginx
  1. 将nginx服务添加到systemctl
    • 修改nginx配置文件,开启pid(vi /usr/local/nginx/nginx.conf)

      • 在配置文件中加入pid /usr/local/run/nginx/nginx.pid;
      • 注意:该路径与步骤8中设置的配置文件路径要保持一致,如果不知道自己的配置文件路径可以通过命令find / -name nginx.conf查找
    • 关闭nginx服务

      • ps aux|grep nginx
      • kill -9 进程id
    • 配置systemctl服务:

      • systemd的service文件都在/usr/lib/systemd/system/路径下,需要在该路径下新建一个nginx.service文件
      • vi /usr/lib/systemd/system/nginx.service,在其中添加以下内容:
      [Unit]
      Description=nginx - high performance web server
      After=network.target remote-fs.target nss-lookup.target
      	
      /usr/local/sbin/nginx -c /usr/local/nginx/nginx.conf
      	
      [Service]
      Type=forking
      PIDFile=/usr/local/run/nginx/nginx.pid
      ExecStartPre=/usr/local/sbin/nginx -t -c /usr/local/nginx/nginx.conf
      ExecStart=/usr/local/sbin/nginx -c /usr/local/nginx/nginx.conf
      ExecReload=/usr/local/sbin/nginx -s reload
      ExecStop=/usr/local/sbin/nginx -s stop
      ExecQuit=/usr/local/sbin/nginx -s quit
      PrivateTmp=true
      	
      [Install]
      WantedBy=multi-user.target
      
    • systemctl daemon-reload使nginx.service文件生效

    • systemctl start/stop/reload/quit nginx.service 启动、停止、重启、退出

    • systemctl enable nginx.service开机自启动
      注意:nginx.service文件中nginx的路径(/usr/local/sbin/nginx)要与步骤8中配置的一致、配置文件路径(/usr/local/nginx/nginx.conf)和配置文件中设置的pid路径尽量与步骤8中配置的一致
      原因:

      • 首先来看nginx的启动命令/usr/local/sbin/nginx -c /usr/local/nginx/nginx.conf,从中可以看出/usr/local/sbin/nginx为nginx的可执行路径,-c /usr/local/nginx/nginx.conf为启动的参数,也可以通过该参数来改变启动时使用的nginx配置文件。
      • 通过systemctl来管理nginx进程时,需要读取nginx配置文件中的pid,通过pid管理nginx进程。

三、 总结

在虚拟机+centos7环境下安装nginx的过程已经展示完毕,下一篇文章将介绍在docker中安装nginx。文章可能在编辑过程中,由于个人疏忽、不同版本markdown解析器不兼容等原因导致字符书写错误,导致安装失败。本文属于原创,若有引用请注明出处。若有疑问或错误,欢迎各位指出,可以评论或者跟本人联系。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值