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 root root Apr : /usr/local/nginx/sbin/nginx

编译安装nginx

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

短网址资料-nginx非root用户启动-systemctl启动脚本-分割root权限的更多相关文章

  1. 设置开机启动时指定非ROOT用户执行相应的脚本

    [root@MSJTVL-MJSP-A01 sm01]# vim /etc/rc.d/rc.local #!/bin/sh # # This script will be executed *afte ...

  2. linux非root用户执行开机启动程序

    问题 开机启动其他用户的程序或者说非root用户执行开机启动 编写开机启动脚本 编写开机启动脚本apple_tree,放到/etc/init.d,系统启动时会自动执行. 例如,/etc/init.d/ ...

  3. linux安装mongodb(设置非root用户和开机启动)

    官网地址:https://www.mongodb.com/ 在官网上选择不同的linux系统得到不同的下载地址,我们用的下载地址是:https://fastdl.mongodb.org/linux/m ...

  4. 完美解决ubuntu Desktop 16.04 中文版firefox在非root用户不能正常启动的问题

    ubuntu安装好后,默认安装有firefox浏览器,不过,非root的账户登录,双击firefox图标,居然出现如下提示:Your Firefox profile cannot be loaded. ...

  5. linux指定某非root用户执行开机启动项的方法(gogs git)

    以linux指定git用户在linux开机时执行启动gogs git为例: 以root登录linux 执行vi /etc/rc.d/rc.local 在文档末尾添加一行语句:su git -c &qu ...

  6. linux系统非ROOT用户80端口不能启动tomcat问题的变通办法——通过Iptables端口转发

    2010-07-17 13:21:42 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{ ...

  7. Centos6.3 下使用 Tomcat-6.0.43 非root用户 部署 生产环境 端口转发方式

    一.安装JDK环境 方法一. 官方下载链接 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260 ...

  8. [转载]Linux下非root用户如何安装软件

    [转载]Linux下非root用户如何安装软件 来源:https://tlanyan.me/work-with-linux-without-root-permission/ 这是本人遇到的实际问题,之 ...

  9. Ubuntu中root用户和user用户的相互切换

    转:Ubuntu是最近很流行的一款Linux系统,因为Ubuntu默认是不启动root用户,现在介绍如何进入root的方法. (1)从user用户切换到root用户 不管是用图形模式登录Ubuntu, ...

随机推荐

  1. LINQ---查询变量

    LINQ查询可以返回两种类型的结果----枚举和标量(scalar)的单一值 namespace ConsoleApplication46 { class Program { static void ...

  2. MyBatis源码部分简单地解析

    . 一.解析xml: > org.apache.ibatis.session.SqlSessionFactoryBuilder.build(java.io.InputStream, java.l ...

  3. Compile-kernel-module

    Compile-kernel-module 1. 内核模块编程1.1 简介1.2 加载内核模块1.3 最简单的模块1.4 模块必要信息1.4.1 内核模块必须至少包含的头文件:1.4.2 内核模块必须 ...

  4. HashMap之Hash碰撞源码解析

    转自:https://blog.csdn.net/luo_da/article/details/77507315 https://www.cnblogs.com/tongxuping/p/827619 ...

  5. STM32 MacOS开发

    CLion + STM32CubeMX + STLINK 安装CLion jetbrain官网 汉化补丁 安装homebrew ...略 安装STlink命令工具 $ brew install stl ...

  6. 020、Java中字母大小写转换

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  7. python实现二分法

    前言: 二分法主要是用来查找位置的id,每次能够排除掉一半的数据,查找的效率非常高,但是局限性比较大. 必须是有序序列才可以使用二分查找. 原理 首先,假设表中元素是按升序排列,将表中间位置记录的关键 ...

  8. 讨论(xia che ≖‿≖✧)magic number——1000000007

    为什么要对1000000007取模(取余) 来看这篇博客的基本上都是和我一样脑子有坑的人,要么就是看了我某篇大数阶乘,大数的排列组合等类似博客被忽悠过来的.我刚刚说到那些类型的题目一般都要求将输出结果 ...

  9. UVA - 1423 Guess (拓扑排序)

    题意:已知矩阵S,求序列a.已知矩阵Sij = “ + ” if ai + . . . + aj > 0; Sij = “ − ” if ai + . . . + aj < 0; and ...

  10. 微服务中springboot启动问题

    log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardS ...