CentOS 7 Nginx 控制指令碼

2021-07-23 17:56:48 字數 4278 閱讀 5822

傳送門:

2、乾貨:

相信看了準備活動,基本上就會了,這個比起下面那個方法實在是簡單多了直接上指令:

$ sudo vi /usr/lib/systemd/system/nginx.service

#輸入下面內容,並儲存

[unit]

description=nginx - high performance web server

documentation=

/docs/

after=network.target remote-fs.target nss-lookup.target

[service]

type=forking

pidfile=/run/nginx.pid

execstartpre=/usr/sbin

/nginx -t -c /etc

/nginx/nginx.conf

execstart=/usr/sbin

/nginx -c /etc

/nginx/nginx.conf

execreload=/bin/kill -s hup $mainpid

execstop=/bin/kill -s quit $mainpid

privatetmp=true

[install]

wantedby=multi-user.target

#修改許可權

$ sudo chmod +x /usr/lib/systemd/system/nginx.service

#/etc/systemd/system/multi-user.target.wants/

$ sudo systemctl enable nginx.service

#現在可以使用下面的指令來控制nginx啦

$ sudo systemctl start nginx.service

$ sudo systemctl reload nginx.service

$ sudo systemctl restart nginx.service

$ sudo systemctl stop nginx.service

#日誌$ journalctl -f -u nginx.service

注意上面的幾個路徑

#下面這幾個路徑是你的nginx安裝的目錄,務必不要弄錯。 

pidfile=/run/nginx.pid

execstartpre=/usr/sbin

/nginx -t -c /etc

/nginx/nginx.conf

execstart=/usr/sbin

/nginx -c /etc

/nginx/nginx.conf

上面的方法是不是簡單非常多?操作簡單了,效率還高了,redhat/centos 果然還是走在了時代的前例,贊乙個。

$ vi /etc/init.d/nginx
輸入以下內容(下面這段指令碼網上很多,直接拷貝

注意幾個地方的配置,就是上在nginx中編譯時設定的那些目錄:

# config: /etc/nginx/nginx.conf
# pidfile: /run/nginx/nginx.pid
nginx="/usr/sbin/nginx"
nginx_conf_file="/etc/nginx/nginx.conf"
lockfile=/var/lock/nginx.lock

$ chmod a+x /etc/init.d/nginx

$ chkconfig --add nginx

$ chkconfig --list nginx

nginx 0

:off

1:off

2:off

3:off

4:off

5:off

6:off

$ cd /etc/rc.d/rc5.d/

$ ll |grep nginx

lrwxrwxrwx. 1 root root 15

6月 24

16:14 k15nginx -> ../init.d/nginx

$ service nginx start  

$ service nginx stop

$ service nginx restart

$ service nginx reload

$ /etc/init.d/nginx start

$ /etc/init.d/nginx stop

$ /etc/init.d/nginx restart

$ /etc/init.d/nginx reload

這樣的操作就簡單多了。

如果有下面的錯誤提示,去看看目錄是否在該檔案,如果實在是存在,且指令碼無錯,則考慮檔案的格式是不是正確,網上有用工具轉的比如:doc2unix 。

env: /etc/init.d/nginx: 沒有那個檔案或目錄
我的做法是直接複製下內容:

$ rm /etc/init.d/nginx

$ vi /etc/init.d/nginx

再把內容複製進去,儲存,
$ chmod a+x /etc/init.d/nginx
記得修改許可權。

CentOS7 Nginx基本操作

我初學nginx伺服器配置,有些操作記不住,所以總結一下放一起。根據學習使用的情況,內容會不定期更新。yum安裝nginx的配置檔案目錄 etc nginx 編譯安裝的nginx的配置檔案目錄 usr local nginx conf 無論是如何安裝的nginx,配置檔案的修改方式都是一樣的,只需要...

centos7 nginx 日誌清理

nginx 日誌檔案需要手動清理分割,因此寫個指令碼實現自動化 建立指令碼檔案clear log.shcd usr local nginx sbin vi clear log.sh bin bash 先複製原來的錯誤日誌檔案,請根據自己實際的日誌路徑填寫 cp usr local nginx log...

CentOS7 Nginx編譯安裝

需先安裝好編譯環境make,gcc和g 開發庫 yum y install gcc automake autoconf libtool make yum install gcc gcc c pcre perl compatible regular expressions perl 相容的正規表示式庫...