Fedora17环境自编译安装nginx配置开机启动

天天开机手动启动nginx太繁琐,今天突然想把它配置成开机自启动,发现chkconfig命令添加不了自启动,网上搜索的启动脚本也是基于chkconfig的,没办法,本人不是用yum或rpm安装的,只能自己手动编写一个systemd的自启动脚本了,下面是经测试可用的脚本:

[Unit]
Description=The Nginx HTTP Server (prefork MPM)
# 随便填上,在网络之后启动
After=network.target

[Service]
Type=forking
# 进程文件目录
PIDFile=/usr/local/nginx/logs/nginx.pid
# 环境配置文件,感觉不指定也可以
EnvironmentFile=/usr/local/nginx/conf/nginx.conf
# 下面三个参数名称是systemd固定的
# 配置文件本来想用变量,发现用变量会报错,可能要加引号,没试过
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=kill -HUP `cat $PIDFILE`
ExecStop=kill -INT `cat $PIDFILE`
# true表示创建进程独立的临时目录
PrivateTmp=true

[Install]
#期望自启动的级别,也可以指定multi-user.target
WantedBy=default.target

 把以上内容保存在/usr/lib/systemd/system/nginx.service

执行命令:

systemctl --system daemon-reload  #重载systemd

systemctl enable nginx.service #配置自启动nginx 

命令执行成功,下次开机即可自动启动nginx

bad message问题,一般是service文件格式不对,如chekconfig启动的文件直接拿过来是不行的,肯定报这个错,必须按照systemd unit文件的格式编写

猜你喜欢

转载自yinhe2726.iteye.com/blog/2033080