1. 程式人生 > >基於CentOS 7配置Nginx自啟動

基於CentOS 7配置Nginx自啟動

Nginx是廣為流行的輕量級Web伺服器軟體。它開源,短小精悍,簡單易用,深受廣大網際網路企業以及IT運維人員所喜愛。很多時候,我們在生產環境基於編譯方式安裝Nginx後,Nginx需要手工配置自啟動服務,以確保伺服器異常宕機後自動重啟該服務。以下描述的是基於CentOS 7下來配置自啟動服務,供大家參考。

一、yum 安裝方式Nginx自啟動

當前環境
[[email protected] ~]# more /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 

檢視是否保護nginx rpm包
[[email protected]
~]# 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 [[email protected] ~]# systemctl list-unit-files |grep nginx nginx.service disabled 將其配置為自動 [[email protected] ~]# systemctl enable nginx.service 檢視nginx.service檔案 [[email protected]
~]# 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.

三、更多參考

相關推薦

基於CentOS 7配置Nginx啟動

Nginx是廣為流行的輕量級Web伺服器軟體。它開源,短小精悍,簡單易用,深受廣大網際網路企業以及IT運維人員所喜愛。很多時候,我們在生產環境基於編譯方式安裝Nginx後,Nginx需要手工配置自啟動服務,以確保伺服器異常宕機後自動重啟該服務。以下描述的是基於C

CentOS 7 配置 Nginx 正向代理 http、https 最詳解

手頭專案中有使用到 nginx,因為使用的三方雲伺服器,想上外網需要購買外網IP的,可是有些需要用到外網卻不常用的主機也掛個外網IP有點浪費了,便想使用nginx的反向代理來實現多臺內網伺服器使用一臺代理伺服器進行外網訪問。 查了很多資料,分享這個功能的人很多(都是好人啊),參考著實現還是費了大半天功夫才搞

Centos 7配置nginx反向代理負載均衡叢集

一,實驗介紹 利用三臺centos7虛擬機器搭建簡單的nginx反向代理負載叢集, 三臺虛擬機器地址及功能介紹 192.168.2.76    nginx負載均衡器 192.168.2.82    web01伺服器 192.168.2.78  &nb

Centos 7.x 配置PostgreSQL啟動

wal sql ron vendor ces systemd lease 其他 net PostgreSQL starts with the systemThe document describe how to configure automatic start Postg

CentOS 7 - 配置服務實現開機啟動

用戶名 centos 7 ring Language get syslog emc log tst 新建系統服務描述文件 cd /etc/systemd/system sudo vim myapp.service 添加以下配置: [Unit] # 這裏添加你的服務描述 D

Centos 7 安裝Nginx、設定為啟動服務、部署vue靜態打包檔案筆記

A.vue專案打包 npm run buildB.Centos 7.0 安裝nginx1.依賴安裝 yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel2.Nginx安裝 cd /usr/local wget ht

CentOS 7 安裝Nginx配置自動啟動

1、官網下載安裝包     http://nginx.org/en/download.html,選擇適合Linux的版本,這裡選擇最新的版本,下載到本地後上傳到伺服器或者centos下直接wget命令下載。     切換到/usr/local目錄,下載軟體包

CentOS 7Nginx安裝以及配置

一、Nginx介紹 Nginx(發音同 engine x)是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,並在一個BSD-like 協議下發行。由俄羅斯的程式設計師Igor Sysoev所開發,最初供俄國大型的入口網站及搜尋引擎R

CentOS 7nginx配置web伺服器

1,安裝過程 [[email protected]_1_14_centos ~]# cd /data/ [[email protected]_1_14_centos data]# wget http://nginx.org/download/nginx-1.15.7.tar.gz

linux新伺服器配置基於CentOS 7.0)

下面是配置一臺新伺服器(CentOS 7.0)的操作流程: 備註:黑色加粗字型為主要操作指令。 一、配置防火牆 CentOS 7.0預設使用的是firewall作為防火牆,這裡改為iptables防火牆。 1、關閉firewall: syste

基於CentOS 7 web服務環境搭建(包含JDK+Nginx+Tomcat+Mysql+Redis)

前言 安裝和配置防火牆 安裝telnet服務語言包 安裝JDK 安裝Nginx

Centos 7Nginx反向代理https配置

需求來源 一個Java開源部落格solo,內建jetty,同時也可以容器部署,之前是部署在Tomcat容器裡面的,今天看到騰訊雲賣證書廣告,突然想到https出來這麼久,自己還沒有嘗試過,還可以藉此

基於centos 7 nginx服務的搭建

閱讀本文的前提是已經安裝了虛擬機器,我使用的是centos7+vmware 網路下載過慢請留言或聯絡博主qq 759519274 首先安裝虛擬機器之後最頭疼的就是ip的配置,裡面太多坑,困擾了小編好幾個小時,接下來一一說來 1.虛擬機器ip的配置 安裝完虛擬機器之後會

CentOS下ELK開機啟動配置

前言 上篇文章描寫了 CentOS下ELK的搭建流程[ CentOS下ELK的搭建 ],做好相應配置後,我們往往會考慮服務自啟動的問題。筆者在網上找了很多相關資料,都沒有比較全面講解,真是一步一個

基於CentOS7開發之路 --- 第二章 : CentOS 7 配置Java環境變數

在windows上面,很多配置Java環境變數的時候,可能都沒有注意到是要配置到系統環境變數還是配置到使用者環境變數裡面。但是在CentOS下,配置環境變數的時候,尤其要注意是配置系統環境變數還是使用者環境變數,因為配置到系統環境變數的話,稍微不注意,可能就把系

CentOS 7安裝nginx 埠代理配置

# wget  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm # rpm -ivh nginx-release-centos-7-0.e

centos 7 配置靜態IP

linux切換root賬號,進入/etc/sysconfig/network-scripts/ 目錄 2.編輯網絡配置接口文件3.保存修改並重啟網絡服務centos 7 配置靜態IP

CentOS 7 配置IP地址以及出現的問題排查

centos 7 linux ip地址配置 當我們新建好一個新的CentOS系統後我們首先需要配置IP 地址,為的就是可以方便遠程連接和後續的正常使用!由於CentOS 7更新之後配置和CentOS 6還是有點小區別,讓我們開始吧~首先進入系統後我們先自動獲取一個IP地址:#dhclient查看獲

centos 7配置靜態IP,並配置DNS

emctl 文件 網卡 管理器 sco auto .html b2b 進行 centos 7配置靜態IP,並配置DNS cd /etc/sysconfig/network-scripts/找到對應的網卡配置並編輯 vim ifcfg-eno16777736配置eno-167

阿裏雲CentOS 7.2 MySQL服務啟動失敗的解決思路

阿裏雲 centos 7.2 mysql服務啟動失敗的解決思路阿裏雲 CentOS 7.2 MySQL服務啟動失敗的解決思路前言 :昨天剛剛搭建好的MySQL讓老大看了一下,經過測試已經完成任務。但是今天早晨來的時候發現服務器被關了,此時我的心情崩潰的,但是我非常冷靜的解決了MySQL問題。如下:啟動MySQ