Centos 7 编译安装nginx 并使用unit文件管理启动

2018-05-24  本文已影响0人  华龙007

一、准备工作:

1、安装必备工具:

[root@localhost ~]# yum -y install gcc gcc-c++ autoconf automake
[root@localhost ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

2、安装之前,最好检查一下是否已经安装有nginx

[root@localhost ~]# find -name nginx

[root@localhost ~]# yum remove nginx    #如果安装了,先卸载

二、Nginx编译安装:

1、下载Nginx:http://nginx.org/en/download.html

[root@localhost ~]# cd /tmp
[root@localhost tmp]# wget http://nginx.org/download/nginx-1.14.0.tar.gz

2.解压编译

[root@localhost nginx-1.14.0]# useradd nginx
[root@localhost nginx-1.14.0]# passwd nginx

[root@localhost tmp]# tar -zxvf nginx-1.14.0.tar.gz
[root@localhost tmp]# cd nginx-1.14.0

[root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx

3.安装

[root@localhost nginx-1.14.0]# make && make install

4.启动、重启、关闭

[root@localhost ~]/usr/local/nginx/sbin/nginx  //启动
[root@localhost ~]/usr/local/nginx/sbin -s reload //重启
[root@localhost ~]/usr/local/nginx/sbin -s stop //关闭

5.实现systemd管理nginx

[root@localhost nginx]# vim /usr/lib/systemd/system/nginx.service
[unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

三、启动nginx服务

[root@localhost nginx]# systemctl daemon-reload
[root@localhost nginx]# systemctl start nginx
[root@localhost nginx]# systemctl status nginx
● nginx.service
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-05-24 04:31:17 EDT; 12s ago
  Process: 5882 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 5883 (nginx)
   CGroup: /system.slice/nginx.service
           ├─5883 nginx: master process /usr/local/nginx/sbin/nginx
           └─5884 nginx: worker process

May 24 04:31:17 localhost.localdomain systemd[1]: [/usr/lib/systemd/system/nginx.service:1] Unkn...ng.
May 24 04:31:17 localhost.localdomain systemd[1]: Starting nginx.service...
May 24 04:31:17 localhost.localdomain systemd[1]: Started nginx.service.
Hint: Some lines were ellipsized, use -l to show in full.
上一篇 下一篇

猜你喜欢

热点阅读