源码安装Nginx以及用systemctl管理

一、源码安装Nginx:

  • 先安装gcc编译器(安装过的可以忽略)

     [root@localhost ~]# yum -y install gcc gcc-c++ wget
  • 进入src目录

     [root@localhost ~]# cd /usr/local/src/
  • 下载 nginx软件包

     [root@localhost src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
  • 解压

     [root@localhost src]# tar -zxvf nginx-1.14.0.tar.gz
  • 进入nginx-1.14.0目录

     [root@localhost src]# cd nginx-1.14.0/
  • 安装依赖

     [root@localhost nginx-1.14.0]# yum -y install openssl openssl-devel
  • ./configure软件配置与检查

     [root@localhostnginx-1.14.0]#./configure--prefix=/usr/local/nginx --with-http_ssl_module
  • 安装

     [root@localhost nginx-1.14.0]# make
     [root@localhost nginx-1.14.0]# make install
  • 启动nginx

     [root@localhost nginx-1.14.0]#cd /usr/local/nginx/sbin
     [root@localhost nginx-1.14.0]#./nginx

查看是否启动成功

    [root@localhost nginx-1.14.0]# ps aux |grep nginx    

二、systemctl管理:

  • 创建配置文件
    源码安装的nginx在/etc/systemd/system/multi-user.target.wants/目录下是没有nginx.service这个文件的,所以要新建

     [root@localhost nginx-1.14.0]#vim /usr/lib/systemd/system/nginx.service
  • 写入内容(全部复制进去,然后修改)

     [Unit]
     Description=nginx - high performance web server
     Documentation=http://nginx.org/en/docs/
     After=network-online.target remote-fs.target nss-lookup.target
     Wants=network-online.target
    
     [Service]
     Type=forking
     PIDFile=/var/run/nginx.pid
     ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
     ExecReload=/bin/kill -s HUP $MAINPID
     ExecStop=/bin/kill -s TERM $MAINPID
    
     [Install]
     WantedBy=multi-user.target
  • 修改 [Service]内容

      将:
          ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf,
      改为:
          ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  • 设置开机启动

        [root@localhost nginx-1.14.0]# systemctl enable nginx.service
  • 关闭之前启动的nginx服务

        [root@localhost nginx-1.14.0]# pkill -9 nginx
  • 重载修改过的所有配置文件

         [root@localhost nginx-1.14.0]#systemctl daemon-reload
  • 重新启动nginx服务

         [root@localhost nginx-1.14.0]#systemctl start nginx

    最后可以用浏览器访问自己虚拟机的IP:192.168.xxx.xx

转载于:https://www.cnblogs.com/MisterZZL/p/9609439.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值