2017-02-19 224 views
2

我使用Ansible安裝了Nginx。爲了在Centos7上安裝,我使用了yum軟件包,所以默認情況下運行的是root用戶。我希望它在Centos框中以不同的用戶身份啓動並運行(例如 - nginx用戶)。當我嘗試用不同的用戶運行它時,出現以下錯誤:以非root用戶身份運行Nginx

Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

我知道不建議以root身份運行。那麼我該如何解決這個問題,並以非root用戶身份運行nginx。由於

+0

堆棧溢出nginx的運行是用於編程和發展問題的站點。這個問題似乎與題目無關,因爲它不涉及編程或開發。請參閱幫助中心的[我可以詢問哪些主題](http://stackoverflow.com/help/on-topic)。也許[超級用戶](http://superuser.com/)或[Unix&Linux堆棧交換](http://unix.stackexchange.com/)會是一個更好的地方。另請參閱[我在哪裏發佈有關Dev Ops的問題?](http://meta.stackexchange.com/q/134306) – jww

+0

也許[允許非root進程綁定到端口80和443?](http:// superuser.com/q/710253/173513)和[綁定到沒有root訪問權的小於1024的端口](https://serverfault.com/questions/268099/bind-to-ports-less-than-1024-without-root - 訪問)將有所幫助。另請參閱[是否有非root進程綁定到Linux上「特權」端口(<1024)的方式?](http://stackoverflow.com/q/413807/608639)。 – jww

回答

5

添加/更改您的/etc/nginx/nginx.conf如下:

user nginx; 

你應該在根目錄目錄創建用戶和授予權限遞歸。

這種方式只有主進程運行爲root因爲:只有根進程可以偵聽1024以下的端口。網絡服務器通常運行在端口80和/或443上。這意味着它需要以root身份啓動。

要運行的主進程作爲非根用戶:

更改以下的所有權:

  • 的error_log
  • 的access_log
  • PID
  • client_body_temp_path
  • fastcgi_temp_path
  • proxy_temp_path
  • scgi_temp_path
  • uwsgi_temp_path

更改聽指令到端口上方1024,登錄爲所需的用戶,並通過nginx -c /path/to/nginx.conf

+0

嗨Farhad。這是在conf文件中默認設置給這個用戶的。我試過了 - 對於nginx並啓動它,仍然是根。 – Sarith

+0

'ps aux | grep nginx'顯示root? –

+0

是的。見下文。 root 5830 0.0 0.1 122232 2216? Ss 17:07 0:00 nginx:主進程/ usr/sbin/nginx nginx 5831 0.0 0.1 122664 3292? S 17:07 0:00 nginx:工作進程 nginx 5832 0.0 0.1 122664 3088?S 17:07 0:00 nginx:工作進程 – Sarith

相關問題