编译安装nginx和yum安装Nginx
Nginx 安装
Nginx版本和安装方式
Nginx版本
- Mainline version 主要开发版本,一般为奇数版本号,比如1.19
- Stable version 当前最新稳定版,一般为偶数版本,如:1.20
- Legacy versions 旧的稳定版,一般为偶数版本,如:1.18
Nginx安装可以使用yum或源码安装,但是推荐使用源码编译安装,原因如下:
- yum的版本比较旧
- 编译安装可以更方便自定义相关路径
- 使用源码编译可以自定义相关功能,更方便业务的上的使用
查看当前系统中的nginx版本
范例:查看系统镜像源和epel源的nginx版本
# centos7 已将nginx集成在系统镜像源
[root@centos7 ~]#dnf info nginx
Last metadata expiration check: 0:53:10 ago on Tue 22 Sep 2020 11:01:33 AM CST.
Available Packages
Name : nginx
Epoch : 1
Version : 1.14.1 #版本较老
Release : 9.module_el8.0.0+184+e34fea82
Architecture : x86_64
Size : 570 k
Source : nginx-1.14.1-9.module_el8.0.0+184+e34fea82.src.rpm
Repository : AppStream #系统镜像源
Summary : A high performance web server and reverse proxy server
URL : http://nginx.org/
License : BSD
Description : Nginx is a web server and a reverse proxy server for HTTP, SMTP,
POP3 and
: IMAP protocols, with a strong focus on high concurrency,
performance and low
: memory usage.
yum 安装nginx
#CentOS7 需提前配置epel源 # yum install -y epel-release
[root@centos7 ~]# yum info nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Packages
Name : nginx
Arch : x86_64
Epoch : 1
Version : 1.20.1 #版本较新
Release : 9.el7
Size : 587 k
Repo : epel/x86_64 #epel源
Summary : A high performance web server and reverse proxy server
URL : https://nginx.org
License : BSD
Description : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
: IMAP protocols, with a strong focus on high concurrency, performance and low
: memory usage.
官方包下载地址: http://nginx.org/en/linux_packages.html
官方yum源:http://nginx.org/en/linux_packages.html#RHEL-CentOS
范例:通过官方yum源安装nginx
#配置yum源
[root@centos7 ~]# cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
#查看所有nginx版本
[root@centos7 ~]# yum list nginx --showduplicates
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: hkg.mirror.rackspace.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Packages
nginx.x86_64 1:1.16.1-1.el7.ngx nginx-stable
nginx.x86_64 1:1.18.0-1.el7.ngx nginx-stable
nginx.x86_64 1:1.18.0-2.el7.ngx nginx-stable
nginx.x86_64 1:1.20.0-1.el7.ngx nginx-stable
nginx.x86_64 1:1.20.1-1.el7.ngx nginx-stable
nginx.x86_64 1:1.20.1-9.el7 epel
nginx.x86_64 1:1.20.2-1.el7.ngx nginx-stable
nginx.x86_64 1:1.22.0-1.el7.ngx nginx-stable
#查看版本信息(默认查看最新版本)
[root@centos7 ~]# yum info nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: hkg.mirror.rackspace.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Packages
Name : nginx
Arch : x86_64
Epoch : 1
Version : 1.22.0
Release : 1.el7.ngx
Size : 796 k
Repo : nginx-stable
Summary : High performance web server
URL : https://nginx.org/
License : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
: a mail proxy server.
#安装nginx
[root@centos7 ~]# yum install -y nginx
检查安装结果
[root@centos7 ~]# rpm -q nginx
nginx-1.22.0-1.el7.ngx.x86_64
#查看日志轮转配置文件路径
[root@centos7 ~]# rpm -ql nginx |grep log
/etc/logrotate.d/nginx
/var/log/nginx
#默认已配置日志轮转功能
[root@centos7 ~]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
查看nginx使用帮助
[root@centos7 ~]# nginx -h
nginx version: nginx/1.22.0
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit #显示详细版本信息和编译参数
-t : test configuration and exit #检测配置文件是否有语法错误
-T : test configuration, dump it and exit #检测配置文件语法并打印结果
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload #向nginx发送相关信号
-p prefix : set prefix path (default: /etc/nginx/)
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file#设置全局指令,注意和配置文件不要同时配置,否则冲突
范例:nginx命令的使用
[root@centos7 ~]# nginx -g "worker_processes 6;" # 启动并配置nginx进程数,与配置文件冲突
nginx: [emerg] "worker_processes" directive is duplicate in /etc/nginx/nginx.conf:3
[root@centos7 ~]# cat /etc/nginx/nginx.conf |grep processes #配置文件中已有配置项
worker_processes auto;
[root@centos7 ~]# cat /etc/nginx/nginx.conf |grep processes #注释此配置项
#worker_processes auto;
[root@centos7 ~]# nginx -g "worker_processes 6;" #启动nginx,开启6个worker进程。默认后台执行
[root@centos7 ~]# ps aux | grep nginx #验证结果
root 2233 0.0 0.1 49056 1156 ? Ss 17:23 0:00 nginx: master process nginx -g worker_processes 6;
nginx 2234 0.0 0.1 49444 1896 ? S 17:23 0:00 nginx: worker process
nginx 2235 0.0 0.1 49444 1896 ? S 17:23 0:00 nginx: worker process
nginx 2236 0.0 0.1 49444 1896 ? S 17:23 0:00 nginx: worker process
nginx 2237 0.0 0.1 49444 1896 ? S 17:23 0:00 nginx: worker process
nginx 2238 0.0 0.1 49444 1896 ? S 17:23 0:00 nginx: worker process
nginx 2239 0.0 0.1 49444 1896 ? S 17:23 0:00 nginx: worker process
root 2242 0.0 0.0 112812 980 pts/1 R+ 17:24 0:00 grep --color=auto nginx
[root@centos7 ~]# nginx -s quit #关闭nginx(进程结束当前任务后自动关闭)
[root@centos7 ~]# ps aux | grep nginx
root 2246 0.0 0.0 112812 980 pts/1 R+ 17:26 0:00 grep --color=auto nginx
[root@centos7 ~]# nginx -g 'daemon off;' #前台启动nginx,在docker中启动时有用。
^C[root@centos7 ~]#
[root@centos7 ~]# nginx -t #nginx语法检查成功
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx 自启动service文件
[root@centos7 ~]# rpm -ql nginx | grep service
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
[root@centos7 ~]# cat /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/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
WantedBy=multi-user.target
nginx 配置文件
[root@centos7 ~]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
#nginx安装目录结果
[root@centos7 ~]# tree /etc/nginx/
/etc/nginx/
├── conf.d
│ └── default.conf
├── fastcgi_params
├── mime.types
├── modules -> ../../usr/lib64/nginx/modules
├── nginx.conf
├── scgi_params
└── uwsgi_params
2 directories, 6 files
#nginx默认配置
[root@centos7 ~]# egrep -v "^ *#|^$" /etc/nginx/nginx.conf
user nginx;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
nginx 启动管理
[root@centos7 ~]# systemctl enable --now nginx #启动nginx并设置开机自启动 --利用service文件管理
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@centos7 ~]# systemctl status nginx #nginx成功运行
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2022-07-20 17:42:56 CST; 7s ago
Docs: http://nginx.org/en/docs/
Process: 2327 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2328 (nginx)
CGroup: /system.slice/nginx.service
├─2328 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─2329 nginx: worker process
Jul 20 17:42:56 centos7 systemd[1]: Starting nginx - high performance web server...
Jul 20 17:42:56 centos7 systemd[1]: Failed to parse PID from file /var/run/nginx.pid: Invalid argument
Jul 20 17:42:56 centos7 systemd[1]: Started nginx - high performance web server.
[root@centos7 ~]# nginx -s quit #使用nginx命令关闭
[root@centos7 ~]# ps aux | grep nginx #关闭成功
root 2338 0.0 0.0 112812 980 pts/1 R+ 17:43 0:00 grep --color=auto nginx
[root@centos7 ~]# systemctl status nginx #nginx状态为 failed
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Wed 2022-07-20 17:43:12 CST; 24s ago
Docs: http://nginx.org/en/docs/
Process: 2333 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=1/FAILURE)
Process: 2327 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2328 (code=exited, status=0/SUCCESS)
Jul 20 17:43:12 centos7 sh[2333]: -s, --signal <sig> send specified signal
Jul 20 17:43:12 centos7 sh[2333]: -q, --queue <sig> use sigqueue(2) rather than kill(2)
Jul 20 17:43:12 centos7 sh[2333]: -p, --pid print pids without signaling them
Jul 20 17:43:12 centos7 sh[2333]: -l, --list [=<signal>] list signal names, or convert one to a name
Jul 20 17:43:12 centos7 sh[2333]: -L, --table list signal names and numbers
Jul 20 17:43:12 centos7 sh[2333]: -h, --help display this help and exit
Jul 20 17:43:12 centos7 sh[2333]: -V, --version output version information and exit
Jul 20 17:43:12 centos7 sh[2333]: For more details see kill(1).
Jul 20 17:43:12 centos7 systemd[1]: Unit nginx.service entered failed state.
Jul 20 17:43:12 centos7 systemd[1]: nginx.service failed.
[root@centos7 ~]# systemctl stop nginx #使用systemctl 关闭nginx
[root@centos7 ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Wed 2022-07-20 17:47:47 CST; 1min 28s ago
Docs: http://nginx.org/en/docs/
Process: 2378 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)
Process: 2361 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2362 (code=exited, status=0/SUCCESS)
Jul 20 17:43:51 centos7 systemd[1]: Starting nginx - high performance web server...
Jul 20 17:43:51 centos7 systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory
Jul 20 17:43:51 centos7 systemd[1]: Started nginx - high performance web server.
Jul 20 17:47:47 centos7 systemd[1]: Stopping nginx - high performance web server...
Jul 20 17:47:47 centos7 systemd[1]: Stopped nginx - high performance web server.
[root@centos7 ~]# nginx #启动nginx
[root@centos7 ~]# ps -ef|grep nginx #查看nginx进程已启动
root 2403 1 0 17:49 ? 00:00:00 nginx: master process nginx
nginx 2404 2403 0 17:49 ? 00:00:00 nginx: worker process
root 2407 2204 0 17:49 pts/1 00:00:00 grep --color=auto nginx
[root@centos7 ~]# systemctl stop nginx #使用systemctl停止nginx
[root@centos7 ~]# ps -ef|grep nginx #发现并未成功停止nginx进程
root 2403 1 0 17:49 ? 00:00:00 nginx: master process nginx
nginx 2404 2403 0 17:49 ? 00:00:00 nginx: worker process
root 2416 2204 0 17:49 pts/1 00:00:00 grep --color=auto nginx
[root@centos7 ~]# systemctl status nginx #systemctl显示nginx进程并未启动
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Wed 2022-07-20 17:47:47 CST; 2min 15s ago
Docs: http://nginx.org/en/docs/
Process: 2378 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)
Process: 2361 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2362 (code=exited, status=0/SUCCESS)
Jul 20 17:43:51 centos7 systemd[1]: Starting nginx - high performance web server...
Jul 20 17:43:51 centos7 systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory
Jul 20 17:43:51 centos7 systemd[1]: Started nginx - high performance web server.
Jul 20 17:47:47 centos7 systemd[1]: Stopping nginx - high performance web server...
Jul 20 17:47:47 centos7 systemd[1]: Stopped nginx - high performance web server.
[root@centos7 ~]# curl localhost #访问nginx成功,nginx是正常运行状态
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
#原因分析
systemctl 使用service文件管理nginx进程。当执行systemctl start nginx时,会将nginx进程的pid记录并存放在指定路径下的pid文件中(具体由service文件定义)。当执行systemctl stop nginx时,通过pid文件向对应的nginx进程发送信号使nginx进程中止运行。如果并未通过执行systemctl start nginx启动nginx,那么systemctl stop nginx命令是无法关闭nginx进程的。因此上文执行nginx命令启动了nginx进程,是无法通过systemctl stop 关闭nginx进程的。
编译安装 nginx
编译器介绍
源码安装需要提前准备标准的编译器,GCC的全称是(GNU Compiler collection),其有GNU开发,并以GPL即LGPL许可,是自由的类UNIX即苹果电脑Mac OS X操作系统的标准编译器,因为GCC原本只能处理C语言,所以原名为GNU C语言编译器,后来得到快速发展,可以处理C++,Fortran,pascal,objectiveC,java以及Ada等其他语言,此外还需要Automake工具,以完成自动创建Makefile的工作,Nginx的一些模块需要依赖第三方库,比如: pcre(支持rewrite),zlib(支持gzip模块)和openssl(支持ssl模块)等。
使用官方源编译安装
官方源码包地址:https://nginx.org/en/download.html
范例:编译安装nginx
[root@centos7 ~]#yum -y install gcc pcre-devel openssl-devel zlib-devel #安装工具包和依赖
[root@centos7 ~]#useradd -s /sbin/nologin nginx #添加nginx账号并配置不可登录
[root@centos7 ~]#cd /usr/local/src/
[root@centos7 src]#wget http://nginx.org/download/nginx-1.18.0.tar.gz #下载源码包
[root@centos7 src]#tar xf nginx-1.18.0.tar.gz
[root@centos7 src]#cd nginx-1.18.0/
#配置编译参数并检查环境依赖(如上文提到的gcc,pcre等依赖未安装则会报错)
[root@centos7 nginx-1.18.0]#./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
[root@centos7 nginx-1.18.0]#make && make install
[root@centos7 nginx-1.18.0]#chown -R nginx.nginx /apps/nginx
nginx安装完成后,有四个主要目录
[root@centos7 ~]# tree /apps/nginx/
/apps/nginx/
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── html
│ ├── 50x.html
│ └── index.html
├── logs
└── sbin
└── nginx
conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有个模板配置文件,是文件名.default结尾,使用的时候将其复制为并将后缀default去掉即可。
html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。
logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。
验证版本及编译参数
[root@centos7 ~]# ls /apps/nginx/sbin/ #nginx二进制程序路径
nginx
[root@centos7 ~]# ln -s /apps/nginx/sbin/nginx /usr/sbin/ #创建软链接到PATH路径
ln: failed to create symbolic link ‘/usr/sbin/nginx’: File exists #注意之前yum安装nginx已生成该文件,因此软连接创建失败
[root@centos7 ~]# nginx -v #查看到的是yum安装的版本
nginx version: nginx/1.22.0
#使用绝对路径查看源码安装的nginx版本(如果要使用相对路径,可自行配置环境变量PATH)
[root@centos7 ~]# /apps/nginx/sbin/nginx -v
nginx version: nginx/1.18.0
[root@centos7 ~]# /apps/nginx/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@centos7 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#重新创建软连接到PATH变量指向的路径下(越靠左,优先级越高)
[root@centos7 ~]# ln -s /apps/nginx/sbin/nginx /usr/local/sbin/
[root@centos7 ~]# exec bash #重新加载bash程序(目的是删除上文执行nginx命令后生成的缓存)
[root@centos7 ~]# nginx -v #观察到已经是源码安装的nginx版本
nginx version: nginx/1.18.0
验证安装结果
[root@centos7 ~]# nginx #启动nginx
[root@centos7 ~]# ps -ef|grep nginx #进程已启动
root 5692 1 0 19:08 ? 00:00:00 nginx: master process nginx
nginx 5693 5692 0 19:08 ? 00:00:00 nginx: worker process
root 5695 2204 0 19:08 pts/1 00:00:00 grep --color=auto nginx
[root@centos7 ~]# curl localhost #访问测试
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
#关闭nginx
[root@centos7 ~]# nginx -s stop
[root@centos7 ~]# ps -ef|grep nginx
root 5701 2204 0 19:10 pts/1 00:00:00 grep --color=auto nginx
创建nginx的service文件以实现开机自启动
#注意ExecStart和ExecReload的路径要和实际安装路径匹配
[root@centos7 ~]# cat > /usr/lib/systemd/system/nginx.service <<EOF
[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=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
#创建缺少的目录
[root@centos7 ~]# mkdir /apps/nginx/run
#修改pid文件指向的路径
[root@centos7 ~]# cat /apps/nginx/conf/nginx.conf | grep pid
pid run/nginx.pid;
#重新加载service文件配置
[root@centos7 ~]# systemctl daemon-reload
#成功使用service文件管理nginx进程
[root@centos7 ~]# systemctl start nginx
[root@centos7 ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2022-07-20 19:22:30 CST; 1min 10s ago
Docs: http://nginx.org/en/docs/
Main PID: 5769 (nginx)
CGroup: /system.slice/nginx.service
├─5769 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─5770 nginx: worker process
Jul 20 19:22:30 centos7 systemd[1]: Starting nginx - high performance web server...
Jul 20 19:22:30 centos7 systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory #此报错可能是nginx自身的bug造成的,服务本身正常。
Jul 20 19:22:30 centos7 systemd[1]: Started nginx - high performance web server.
nginx服务正常,但查看状态报错:Can’t open PID file /var/run/nginx.pid (yet?) after start: No such file or directory
参考链接:https://blog.csdn.net/XY0918ZWQ/article/details/114165979