短網址資料-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.html


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

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/nginx

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

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

 

[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 process

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)

 

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

 

然后啟動nginx

 

-r是清除附加權限:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM