CentOS7配置Nginx服務

目標

把Nginx配置中CentOS的隨機服務,相似於《Spring boot部署CentOS7》中的配置。該文接着《CentOS7編譯安裝nginx》以後。html

建立用戶

sudo useradd --system --home /var/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx

受權文件夾

chown -R nginx /var/nginx
chgrp -R nginx /var/nginx

systemd

vim /usr/lib/systemd/system/nginx.service
[Service]
User=nginx
Group=nginx
Type=forking
ExecStartPre=/var/nginx/sbin/nginx -t -c /var/nginx/conf/nginx.conf
ExecStart=/var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
ExecReload=/var/nginx/sbin/nginx -s reload
ExecStop=/var/nginx/sbin/nginx -s stop
ExecQuit=/var/nginx/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target

firewall

firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --reload

驗證

# 開機啓動
systemctl enable nginx.service 
systemctl daemon-reload
systemctl start nginx.service
[root@zyl nginx]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2018-12-23 14:29:48 CST; 9min ago
     Docs: https://nginx.org/en/docs/
  Process: 62130 ExecStart=/var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 62126 ExecStartPre=/var/nginx/sbin/nginx -t -c /var/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 62132 (nginx)
    Tasks: 2
   CGroup: /system.slice/nginx.service
           ├─62132 nginx: master process /var/nginx/sbin/nginx -c /var/nginx/conf/nginx.conf
           └─62133 nginx: worker process

12月 23 14:29:48 BPC systemd[1]: Starting nginx - high performance web server...
12月 23 14:29:48 BPC nginx[62126]: nginx: the configuration file /var/nginx/conf/nginx.conf syntax is ok
12月 23 14:29:48 BPC nginx[62126]: nginx: configuration file /var/nginx/conf/nginx.conf test is successful
12月 23 14:29:48 BPC systemd[1]: Started nginx - high performance web server.

瀏覽器效果:linux

nginx效果

非root用戶1024如下端口

setcap cap_net_bind_service=+eip /var/nginx/sbin/nginx

setcap將1024如下的端口網絡權限,給非root用戶使用,只給網絡權限。nginx

參考

相關文章
相關標籤/搜索