源码安装Nginx以及用systemctl管理


一、源码安装Nginx:

先安装gcc编译器(安装过的可以忽略)
[root@localhost ~]# yum -y install gcc gcc-c++ wget

下载 nginx软件包

[root@localhost src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz

解压

[root@localhost src]# tar -zxvf nginx-1.20.1.tar.gz

进入nginx-1.20.1目录

[root@localhost src]# cd nginx-1.20.1/

安装依赖

[root@localhost nginx-1.14.0]# yum -y install openssl openssl-devel

/configure软件检查(  ./configure--prefix=/usr/local/nginx --with-http_ssl_module 可以省略自定义安装路径)

[root@localhostnginx-1.14.0]#./configure 

安装

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

启动

[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]内容

 将:
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf,
改为:
PIDFile=usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
 

关闭之前启动的nginx服务

[root@localhost nginx-1.14.0]# pkill -9 nginx

重载修改过的所有配置文件

[root@localhost nginx-1.14.0]#systemctl daemon-reload

设置开机启动

[root@localhost nginx-1.14.0]# systemctl enable nginx.service

重新启动nginx服务

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