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

参考

相关文章
相关标签/搜索