1.创建服务文件文件路径vim/usr/lib/systemd/system/nginx.service文件内容[Unit]Description=nginx-highperformancewebserverAfter=network.targetremote-fs.targetnss-lookup.target[Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx-sreloadExecStop=/usr/local/nginx/sbin/nginx-sstop[Install]WantedBy=multi-user.targetfiledescription[Unit]:服务的描述Description:描述服务After:dependon,whendependon服务启动后,启动自定义服务[Service]服务运行参数的设置Type=forking是后台运行的形式ExecStart是服务的具体运行命令ExecReload是重启命令ExecStop是停止命令PrivateTmp=True表示给服务分配一个独立的服务暂存空间注意:启动、重启、停止命令都需要使用绝对路径[Install]服??务安装的相关设置,可以设置为多用户TypeType=simple(默认值):systemd认为服务会立即启动。服务进程不会分叉。如果该服务要启动其他服务,请不要使用该类型启动,除非该服务是套接字激活的。Type=forking:systemd认为当服务进程fork,父进程退出时,服务启动成功。对于常规守护进程,除非你确定这种启动方式不能满足你的需要,否则你可以使用这种启动方式。使用此启动类型还应指定PIDFile=,以便systemd可以跟踪服务的主进程。Type=oneshot:该选项适用于只执行一个任务然后立即退出的服务。可能需要同时设置RemainAfterExit=yes,这样在服务进程退出后systemd仍然认为服务是活跃的。Type=notify:与Type=simple相同,但约定服务准备就绪时会向systemd发送信号。此通知的实现由libsystemd-daemon.so提供。Type=dbus:如果以这种方式启动,当指定的BusName出现在DBus系统总线上时,systemd认为服务就绪。PIDFile:pid文件路径ExecStartPre:启动前做什么,上面是测试配置文件-t2。保存目录/usr/lib/systemd/system3.设置系统开机自启动后在任意目录下执行systemctlenablenginx.service4.使用命令启动nginx服务systemctlstartnginx.service设置开机自动启动systemctlenablenginx.servicestopboottostartautomaticallysystemctldisablenginx.service查看状态systemctlstatusnginx.servicerestartservicesystemctlrestartnginx.service查看所有服务systemctllist-units--type=service
