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负载均衡

基于CentOS 7配置Nginx自启动的更多相关文章

  1. 详解如何通过H5(浏览器/WebView/其他)唤起本地app

    这篇文章主要介绍了详解如何通过H5(浏览器/WebView/其他)唤起本地app的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. H5混合开发app如何升级的方法

    本篇文章主要介绍了H5混合开发app如何升级的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  3. html5唤起app的方法

    这篇文章主要介绍了html5唤起app的方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  4. xcode – 上传到App Store时进行身份验证

    只需为现有安装/文件夹创建备份,这很重要,因为在(新)安装期间,Transporter将删除以前的安装:现在运行以下命令来更新Transporter:希望这有助于某人.

  5. App store拒绝应用程序在iOs 10上支持IPV6网络

    我收到苹果公司的app拒绝邮件,下面是我们在连接到IPv6网络的Wi-Fi上运行iOS10.0.2的iPad和iPhone上查看了应用中的一个或多个错误.具体来说,应用程序在启动时仍保留在启动屏根据他们的要求,我已经在我的Mac上创建了NAT64网络,并为iPhone5S设备10.0.2os版本共享了互联网,App工作正常,但苹果称其不与IPv6合作任何人都可以确认我需要检查其他什么吗?

  6. ios – 我如何从iPhone中提取IPA以从App Store下载以便我可以在IPA中查看资产?

    我最喜欢的应用程序之一已从应用程序商店中删除,我想因为它没有在太长时间内更新.我有一台旧设备,但没有下载到我的新手机上.如何获得IPA以便我可以查看应用程序包并查看应用程序中的资产?

  7. ios – – [Not A Type _cfTypeID]:发送到解除分配的实例的消息

    我正在使用代码为图像提供不同的效果,如对比度,色调,饱和度等;并使用了appleglImageProcessing代码,我从我的视图跳转到glimgeProcessing,并将结果图像保存到appDelegate文件中的uiimage属性.从Eagle视图返回后,我使用viewDidAppear函数将我的图像视图更改为更新的图像我的代码是我的日志响应是尝试将图像设置为我的imageView时出现问

  8. iOS扩展:是否需要增加其捆绑版本(CFBundleVersion)?

    我是否必须在我的扩展程序的Info.plist中增加CFBundLeversion以确保它覆盖现有的?或者,如果在主应用程序的Info.plist中这样做就足够了?

  9. ios – navigator.app undefined

    谢谢你的帮助.干杯,米格尔解决方法“navigator.app”对象仅适用于Android.幸运的是,在即将发布的PhoneGap2.3.0版本中你可以做到:做你想做的事.

  10. 验证Xcode安装时,“/Applications/Xcode.app:密封资源丢失或无效”错误

    你认为我需要重新安装Xcode以防万一?

随机推荐

  1. 在airgapped(离线)CentOS 6系统上安装yum软件包

    我有一个CentOS6系统,出于安全考虑,它已经被空气泄漏.它可能从未连接到互联网,如果有,它很长时间没有更新.我想将所有.rpm软件包放在一个驱动器上,这样它们就可以脱机安装而无需查询互联网.但是,我在测试VM上遇到的问题是,即使指定了本地路径,yum仍然会挂起并尝试从在线存储库进行更新.另外,有没有办法使用yum-utils/yumdownloader轻松获取该包的所有依赖项和所有依赖项?目前

  2. centos – 命名在日志旋转后停止记录到rsyslog

    CentOS6.2,绑定9.7.3,rsyslog4.6.2我最近设置了一个服务器,我注意到在日志轮换后,named已停止记录到/var/log/messages.我认为这很奇怪,因为所有日志记录都是通过rsyslog进行的,并且named不会直接写入日志文件.这更奇怪,因为我在更新区域文件后命名了HUPed,但它仍然没有记录.在我停止并重新启动命名后,记录恢复.这里发生了什么?

  3. centos – 显示错误的磁盘大小

    对于其中一个磁盘,Df-h在我的服务器上显示错误的空白区域:Cpanel表明它只有34GB免费,但还有更多.几分钟前,我删除了超过80GB的日志文件.所以,我确信它完全错了.fdisk-l/dev/sda2也显示错误:如果没有格式化,我该怎么做才能解决这个问题?并且打开文件描述符就是它需要使用才能做到这一点.所以…使用“lsof”并查找已删除的文件.重新启动写入日志文件的服务,你很可能会看到空间可用.

  4. 如何在centos 6.9上安装docker-ce 17?

    我目前正在尝试在centOS6.9服务器上安装docker-ce17,但是,当运行yuminstalldocker-ce时,我收到以下错误:如果我用跳过的标志运行它我仍然得到相同的消息,有没有人知道这方面的方法?

  5. centos – 闲置工作站的异常负载平均值

    我有一个新的工作站,具有不寻常的高负载平均值.机器规格是:>至强cpu>256GB的RAM>4x512GBSSD连接到LSI2108RAID控制器我从livecd安装了CentOS6.564位,配置了分区,网络,用户/组,并安装了一些软件,如开发工具和MATLAB.在启动几分钟后,工作站负载平均值的值介于0.5到0.9之间.但它没有做任何事情.因此我无法理解为什么负载平均值如此之高.你能帮我诊断一下这个问题吗?

  6. centos – Cryptsetup luks – 检查内核是否支持aes-xts-plain64密码

    我在CentOS5上使用cryptsetupluks加密加密了一堆硬盘.一切都很好,直到我将系统升级到CentOS6.现在我再也无法安装磁盘了.使用我的关键短语装载:我收到此错误:在/var/log/messages中:有关如何装载的任何想法?找到解决方案问题是驱动器使用大约512个字符长的交互式关键短语加密.出于某种原因,CentOS6中的新内核模块在由旧版本创建时无法正确读取512个字符的加密密钥.似乎只会影响内核或cryptsetup的不同版本,因为在同一系统上创建和打开时,512字符的密钥将起作用

  7. centos – 大量ssh登录尝试

    22个我今天登录CentOS盒找到以下内容这是过去3天内的11次登录尝试.WTF?请注意,这是我从我的提供商处获得的全新IP,该盒子是全新的.我还没有发布任何关于此框的内容.为什么我会进行如此大量的登录尝试?是某种IP/端口扫描?基本上有4名匪徒,其中2名来自中国,1名来自香港,1名来自Verizon.这只发生在SSH上.HTTP上没有问题.我应该将罪魁祸首子网路由吗?你们有什么建议?

  8. centos – kswap使用100%的CPU,即使有100GB的RAM也可用

    >Linux内核是否应该足够智能,只需从内存中清除旧缓存页而不是启动kswap?

  9. centos – Azure将VM从A2 / 3调整为DS2 v2

    我正在尝试调整前一段时间创建的几个AzureVM,从基本的A3和标准A3到标准的DS2v2.我似乎没有能力调整到这个大小的VM.必须从头开始重建服务器会有点痛苦.如果它有所不同我在VM中运行CentOS,每个都有一个带有应用程序和操作系统的磁盘.任何人都可以告诉我是否可以在不删除磁盘的情况下删除VM,创建新VM然后将磁盘附加到新VM?

  10. centos – 广泛使用RAM时服务器计算速度减慢

    我在非常具体的情况下遇到服务器速度下降的问题.事实是:>1)我使用计算应用WRF>2)我使用双XeonE5-2620v3和128GBRAM(NUMA架构–可能与问题有关!

返回
顶部