实验安装环境:CentOS7.6 Nginx1.15 关闭防火墙、核心防护、已配好yum本地源
一、安装依赖性环境包
[root@host ~]# yum -y install \
> gcc \
> gcc-c++ \
> make \
> pcre-devel \
> expat-devel \
> perl \
> zlib-devel
二、创建运行用户、组
[root@host ~]# useradd -M -s /sbin/nologin nginx
创建一个名为nginx的程序用户,-M不让它创建家目录,-s指定shell环境/sbin/nologin不允许登录shell环境
三、编译安装
把安装包放到/opt目录下解压
[root@host ~]# cd /opt
[root@host opt]# tar xzvf nginx-1.15.9.tar.gz
进入nginx文件里,配置参数,编译,安装
[root@host opt]# cd nginx-1.15.9
[root@host nginx-1.15.9]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@host nginx-1.15.9]# make -j3
[root@host nginx-1.15.9]# make install
四、优化路径
为了使 Nginx 服务器的运行更加方便, 可以为主程序 nginx 创建链接文件, 以便管理员
直接执行 “nginx” 命令就可以调用Nginx的主程序
[root@host nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
把命令放到/usr/local/sbin下面,这个目录是$PATH下的变量
[root@host nginx-1.15.9]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 Sep 16 15:31 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
五、检查配置文件
首先检查配置文件是否正确,只有出现is ok 和 successful,配置文件才正确
[root@host nginx-1.15.9]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
六、启动、停止nginx
直接运行Nginx即可启动Nginx服务器,这种方式将使用默认的配置文件
[root@host nginx-1.15.9]# nginx
[root@host nginx-1.15.9]# yum -y install lynx
[root@host nginx-1.15.9]# lynx 127.0.0.1
本地测试出现测试页,nginx服务开启成功
主程序Nginx支持标准的进程信号,通过kill或killall命令发送HUP信号表示重载配置,
QUIT信号表示退出进程, KILL信号表示杀死进程。例如,若使用killall 命令,重载配置、
停止服务的操作分别如下所示(通过"-s"选项指定信号种类)
七、添加nginx系统服务
为了使Nginx服务的启动、停止、重载等操作更加方便,可以编写基于CentOs 7.6的
Nginx服务控制文件使用systemctl工具来进行管理.
添加系统服务,到/lib/systemd/system/目录下添加service文件
[root@host nginx-1.15.9]# vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@host nginx-1.15.9]# chmod 754 /lib/systemd/system/nginx.service //更改权限
[root@host nginx-1.15.9]# systemctl enable nginx.service //开机自启动
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@host nginx-1.15.9]# netstat -anutp | grep nginx //先看一下,没有开
[root@host nginx-1.15.9]# systemctl start nginx //打开
[root@host nginx-1.15.9]# netstat -anutp | grep nginx //验证一下,开了
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23229/nginx: master
##配置参数解释##
[Unit]
Description=nginx ###描述
After=network.target ####描述服务类别
[Service]
Type=forking ###后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid ###PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx ###启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID ###根据PID重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID ###根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target