基于CentOS 7配置Nginx自启动

简介:

Nginx是广为流行的轻量级Web服务器软件。它开源,短小精悍,简单易用,深受广大互联网企业以及IT运维人员所喜爱。很多时候,我们在生产环境基于编译方式安装Nginx后,Nginx需要手工配置自启动服务,以确保服务器异常宕机后自动重启该服务。以下描述的是基于CentOS 7下来配置自启动服务,供大家参考。

一、yum 安装方式Nginx自启动

当前环境
[root@node142 ~]# more /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 

查看是否保护nginx rpm包
[root@node142 ~]# rpm -qa|grep nginx
nginx-mod-http-geoip-1.12.2-2.el7.x86_64
nginx-1.12.2-2.el7.x86_64
nginx-filesystem-1.12.2-2.el7.noarch
nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
nginx-mod-stream-1.12.2-2.el7.x86_64
nginx-mod-http-perl-1.12.2-2.el7.x86_64
nginx-mod-http-image-filter-1.12.2-2.el7.x86_64
nginx-all-modules-1.12.2-2.el7.noarch
nginx-mod-mail-1.12.2-2.el7.x86_64

查看是否存在相应的服务,如下,有nginx.service
[root@node142 ~]# systemctl list-unit-files |grep nginx
nginx.service                              disabled

将其配置为自动
[root@node142 ~]# systemctl enable nginx.service

查看nginx.service文件
[root@node142 ~]# more /lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

上述配置文件中的内容和官网提供的一模一样
https://www.nginx.com/resources/wiki/start/topics/examples/systemd/

二、编译安装后的自启动配置

由于是编译安装,因此,对于这个自启动的脚本我们需要自行配制。
具体则是参考上面的链接或者上面给出的nginx.service文件内容。
然后将其保存为 /lib/systemd/system/nginx.service。

看下面的例子

# more /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 

# ps -ef|grep nginx
root    10092 10014  0 16:23 pts/0    00:00:00 grep --color=auto nginx
root    20791    1  0 Mar20 ?        00:00:00 nginx: master process ./sbin/nginx -c /u01/app/nginx/nginx.conf
nobody  20792 20791  0 Mar20 ?        00:00:44 nginx: worker process
nobody  20793 20791  0 Mar20 ?        00:00:42 nginx: worker process
nobody  20794 20791  0 Mar20 ?        00:00:50 nginx: worker process
nobody  20795 20791  0 Mar20 ?        00:00:44 nginx: worker process
nobody  20796 20791  0 Mar20 ?        00:00:43 nginx: worker process
nobody  20797 20791  0 Mar20 ?        00:00:43 nginx: worker process
nobody  20798 20791  0 Mar20 ?        00:00:37 nginx: worker process
nobody  20799 20791  0 Mar20 ?        00:00:48 nginx: worker process
nobody  20800 20791  0 Mar20 ?        00:00:04 nginx: cache manager process
# 

无相应的rpm包,如下查询,此处为编译安装
# rpm -qa|grep nginx

也没有添加相应的自启动服务
# systemctl list-unit-files |grep nginx

nginx版本
# /u01/app/nginx/sbin/nginx -v
nginx version: nginx/1.8.1

获取nginx编译模块,然后查看诸如pid,二进制位置并记录以便修改启动文件
# /u01/app/nginx/sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/u01/app/nginx --sbin-path=/u01/app/nginx/sbin/nginx 
--conf-path=/u01/app/nginx/nginx.conf --error-log-path=/u01/app/nginx/log/error.log 
--http-log-path=/u01/app/nginx/log/access.log --pid-path=/u01/app/nginx/nginx.pid 
--lock-path=/u01/app/nginx/nginx.lock --http-client-body-temp-path=/u01/app/nginx/client_temp 
--http-proxy-temp-path=/u01/app/nginx/proxy_temp 
--http-fastcgi-temp-path=/u01/app/nginx/fastcgi_temp 
--http-uwsgi-temp-path=/u01/app/nginx/uwsgi_temp --http-scgi-temp-path=/u01/app/nginx/scgi_temp
--user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module 
--with-http_addition_module --with-http_sub_module --with-http_dav_module
--with-http_flv_module --with-http_mp4_module --with-http_gunzip_module 
--with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module 
--with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module
--with-file-aio --with-http_spdy_module --with-ipv6

下面我们生成一个新的nginx.service文件

# vim /lib/systemd/system/nginx.service

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/u01/app/nginx/nginx.pid
ExecStartPre=/u01/app/nginx/sbin/nginx  -t
ExecStart=/u01/app/nginx/sbin/nginx 
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

下面我们先手工停止nginx
# /u01/app/nginx/sbin/nginx -s stop

配置自启动
# systemctl enable nginx.service

使用systemctl工具启动nginx服务
# systemctl start nginx.service

# systemctl status nginx.service
● nginx.service - The NGINX HTTP and reverse proxy server
  Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
  Active: active (running) since Thu 2018-03-29 16:37:47 CST; 6s ago
  Process: 10588 ExecStart=/u01/app/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 10586 ExecStartPre=/u01/app/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 10590 (nginx)
  CGroup: /system.slice/nginx.service
          ├─10590 nginx: master process /u01/app/nginx/sbin/nginx
          ├─10591 nginx: worker process # Author : Leshami
          ├─10592 nginx: worker process # Blog : https://blog.csdn.net/leshami
          ├─10593 nginx: worker process
          ├─10594 nginx: worker process
          ├─10595 nginx: worker process
          ├─10596 nginx: worker process
          ├─10597 nginx: worker process
          ├─10598 nginx: worker process
          ├─10599 nginx: cache manager process
          └─10600 nginx: cache loader process

Mar 29 16:37:47 ydq-std systemd[1]: Starting The NGINX HTTP and reverse proxy server...
Mar 29 16:37:47 ydq-std nginx[10586]: nginx: the configuration file /u01/app/nginx/nginx.conf syntax is ok
Mar 29 16:37:47 ydq-std nginx[10586]: nginx: configuration file /u01/app/nginx/nginx.conf test is successful
Mar 29 16:37:47 ydq-std systemd[1]: Started The NGINX HTTP and reverse proxy server.

三、更多参考

Linux 6下安装编译安装Nginx
CentOS 7下 yum方式安装Nginx
基于CentOS 7配置Nginx反向代理
基于CentOS 7配置Nginx负载均衡

目录
相关文章
|
2月前
|
编解码 应用服务中间件 Linux
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
288 1
|
2月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
257 18
|
2月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
316 17
|
3月前
|
数据建模 应用服务中间件 PHP
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
本示例演示如何通过Docker挂载同一宿主目录至Nginx与PHP容器,实现PHP项目运行环境配置。需注意PHP容器中监听地址修改为0.0.0.0:9000,并调整Nginx配置中fastcgi_pass指向正确的IP与端口。同时确保Nginx容器中/var/www/html权限正确,以避免访问问题。
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
|
4月前
|
应用服务中间件 网络安全 nginx
配置Nginx以支持Websocket连接的方法。
通过上述配置,Nginx将能够理解WebSocket协议的特殊要求,代理Websocket流量到合适的后端服务器。注意,Websocket并不是HTTP,尽管它最初是通过HTTP请求启动的连接升级,因此保证Nginx了解并能够妥善处理这种升级流程是关键。
1049 10
|
3月前
|
Ubuntu 应用服务中间件 Linux
在Ubuntu上配置Nginx实现开机自启功能
至此,Nginx应该已经被正确地设置为开机自启。在Ubuntu中利用 `systemd`对服务进行管理是一种高效的方式,为系统管理员提供了强大的服务管理能力,包括但不限于启动、停止、重启服务,以及配置服务的开机自启动。通过这些简洁的命令,即使是对Linux不太熟悉的用户也能轻松地进行配置。
176 0
|
Linux 虚拟化
CentOS 7.X配置连接网络
应用场景 Linux虚拟机,系统安装完毕后,无法连接网络,由于是最小化安装,很多命令无法直接yum安装,无法连接外网wget下载资源等等,造成很大的不便,因此需要进行配置连接外网! 操作指南 1. 开启VMware NAT Service 右击“计算机”,选择“管理”,在“服务和应用程序”中,选择“服务”,在右边找到“VMware NAT Service”服务,进行开启。
1311 0
|
Linux 虚拟化 网络协议
CentOS 6.X配置连接网络
应用场景 Linux虚拟机,系统安装完毕后,无法连接网络,由于是最小化安装,很多命令无法直接yum安装,无法连接外网wget下载资源等等,造成很大的不便,因此需要进行配置连接外网! 操作指南 1. 开启VMware NAT Service 右击“计算机”,选择“管理”,在“服务和应用程序”中,选择“服务”,在右边找到“VMware NAT Service”服务,进行开启。
908 0
|
3月前
|
存储 Ubuntu Linux
VMware-安装CentOS系统教程及安装包
虚拟机相当于是一个独立于你电脑的环境,在这个环境上面,你可以安装Linux、Windows、Ubuntu等各个类型各个版本的系统,在这个系统里面你不用担心有病读等,不用担心文件误删导致系统崩溃。 虚拟机也和正常的电脑系统是一样的,也可以开关机,不用的时候,你关机就可以了,也不会占用你的系统资源,使用起来还是比较方便 这里也有已经做好的CentOS 7系统,下载下来解压后直接用VMware打开就可以使用
753 69