短網址資料-nginx非root用戶啓動-systemctl啓動腳本-分割root權限


https://www.cnblogs.com/aspnethot/articles/3492191.html
https://www.cnblogs.com/aspnethot/articles/3492253.html
http://www.cnblogs.com/sunli/archive/2010/03/25/1696183.html
https://c7sky.com/yourls.htmlhtml


wget http://nginx.org/download/nginx-1.8.1.tar.gzjava

tar xvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
[root@Server1 nginx-1.8.1]$ ls
mkdir /var/tmp/nginx/client -p
/usr/local/nginx/sbin/nginxnode

/usr/local/nginx/sbin/nginx -s reload
useradd zsl2
passwd zsl2
123456
su zsl2nginx

chmod -R 777 /var/log/nginx
chmod -R 777 /var/run/nginx #注意給普通用戶寫入權限tomcat

 

[zsl2@node62 nginx-1.8.1]$ /usr/local/nginx/sbin/nginx #雖然有報警可是執行成功了
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/nginx/conf/nginx.conf:2測試

root@node62 ~]# ps -aux | grep nginx
zsl2 13060 0.0 0.0 43656 1100 ? Ss 09:55 0:00 nginx: master process /usr/local/nginx/sbin/nginx
zsl2 13061 0.0 0.0 44104 1568 ? S 09:55 0:00 nginx: worker processurl

80端口啓動報錯
[zsl2@node62 nginx-1.8.1]$ /usr/local/nginx/sbin/nginx
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/nginx/conf/nginx.conf:2
nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)spa

 

setcap cap_net_bind_service=+eip /usr/local/nginx/sbin/nginx #使命令在非root用戶下支持1024如下端口啓動 注意在root下執行此命令

 

而後啓動nginxrest

 

-r是清除附加權限:code

setcap -r nginx

 分割權限參考

https://www.cnblogs.com/nf01/articles/10418141.html

手寫nginx以systemclt命令啓動 腳本

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

[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
User=nginx
Group=nginx
PIDFile=/var/run/nginx/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false #注意這裏爲false否則報錯建立文件夾也不行

[Install]
WantedBy=multi-user.target

 

CentOS7自定義系統服務

CentOS7的服務systemctl腳本存放在:/usr/lib/systemd/,有系統(system)和用戶(user)之分,須要開機不登錄就能運行的程序,存在系統服務裏,即:/usr/lib/systemd/system目錄下.
CentOS7的每個服務以.service結尾,通常會分爲3部分:[Unit]、[Service]和[Install]

[Unit]部分主要是對這個服務的說明,內容包括Description和After,Description 用於描述服務,After用於描述服務類別

[Service]部分是服務的關鍵,是服務的一些具體運行參數的設置.
Type=forking是後臺運行的形式,
User=users是設置服務運行的用戶,
Group=users是設置服務運行的用戶組,
PIDFile爲存放PID的文件路徑,
ExecStart爲服務的具體運行命令,
ExecReload爲重啓命令,
ExecStop爲中止命令,
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:[Service]部分的啓動、重啓、中止命令所有要求使用絕對路徑,使用相對路徑則會報錯!
[Install]部分是服務安裝的相關設置,可設置爲多用戶的
首先,使用systemctl start [ 服務名(也是文件名) ] 可測試服務是否能夠成功運行,若是不能運行則可使用systemctl status [ 服務名(也是文件名) ]查看錯誤信息和其餘服務信息,而後根據報錯進行修改,直到能夠start,若是不放心還能夠測試restart和stop命令。

接着,只要使用systemctl enable xxxxx就能夠將所編寫的服務添加至開機啓動便可。

實例:服務用於開機運行tomcat項目:

#vi /usr/lib/systemd/system/tomcat.service

[Unit]
Description=java tomcat project
After=tomcat.service
[Service]
Type=forking
User=users
Group=users
PIDFile=/usr/local/tomcat/tomcat.pid
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecReload=
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target
添加可執行權限:
chmod 754 /usr/lib/systemd/system/tomcat.service
設置爲開機自啓動:
#systemctl enable tomcat.service
經常使用指令(以tomcat服務爲例):
啓動某服務
systemctl start tomcat.service
中止某服務
systemctl stop tomcat.service
systemctl restart tomcat.service
使某服務自動啓動(如tomcat服務)
systemctl enable tomcat.service
使某服務不自動啓動
systemctl disable tomcat.service
檢查服務狀態
systemctl status tomcat.service (服務詳細信息)
systemctl is-active tomcat.service(僅顯示是否Active)
顯示全部已啓動的服務
systemctl list-units --type=service

另外一種方式傳遞權限可是守護進程仍是root

[root@node62 ~]# chmod u+s /usr/local/nginx/sbin/nginx
[root@node62 ~]# ll /usr/local/nginx/sbin/nginx
-rwsr-xr-x 1 root root 5250016 Apr 23 09:15 /usr/local/nginx/sbin/nginx

 

編譯安裝nginx

https://www.cnblogs.com/zhang-shijie/p/5294162.html

相關文章
相關標籤/搜索