源码安装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软件配置与检查

    [[email protected]]#./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

猜你喜欢

转载自blog.csdn.net/qq_21437451/article/details/82530057