操作系统:CentOS release 6.10 (Final)
主机1:192.168.12.28
主机2:192.168.12.29
虚拟机:VMware WorkStation
任务:将主机1的nginx服务脚本传送到主机2,同一脚本使用多处主机,重复使用效率高。
主机1操作:
[root@Nginx init.d]# scp nginx [email protected]:/etc/init.d/
The authenticity of host '192.168.12.129 (192.168.12.129)' can't be established.
RSA key fingerprint is 7f:0e:90:ab:43:32:f4:43:43:fb:d5:ca:62:a7:d1:6a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.12.129' (RSA) to the list of known hosts.
[email protected]'s password:
bash: scp: command not found
lost connection
[root@Nginx init.d]# which scp
/usr/bin/scp
主机2操作:
[root@Nginx init.d]# which scp
/usr/bin/which: no scp in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@Nginx init.d]# yum install openssh-clients
分析原因是主机2没有安装openssh-clients软件包
主机1操作:
[root@Nginx init.d]# scp nginx [email protected]:/etc/init.d #传送文件成功
[email protected]'s password:
nginx 100% 571 0.6KB/s 00:00
主机2操作:
[root@Nginx init.d]# chmod +x /etc/init.d/nginx
[root@Nginx init.d]# chkconfig --add nginx
[root@Nginx init.d]#chkconfig --add /etc/init.d/nginx #建议采用此种方式添加nginx服务
[root@Nginx init.d]# chkconfig --level 2345 nginx on
[root@Nginx init.d]# chkconfig nginx on #建议采用此种方式自动启动nginx服务
nginx服务脚本如下:
[root@Nginx init.d]# cat nginx
#!/bin/bash
#chkconfig: 35 85 15
DAEMON=/usr/local/nginx/sbin/nginx
case "$1" in
start)
echo "Starting nginx daemon..."
$DAEMOM && echo "SUCCESS"
;;
stop)
echo "Stopping nginx daemon..."
$DAEMON -s quit && echo "SUCCESS"
;;
reload)
echo "Reloading nginx daemon...."
$DAEMON -s reload && echo "SUCCESS"
;;
restart)
echo "Restaring nginx daemon..."
$DAEMON -s quit
$DAEMON && echo "SUCESS"
;;
*)
echo "Usage:service nginx{start|stop|restart|reload}"
exit 2
;;
esac
实战小结:
1.服务器端、客户端,或者说传送文件两端必须安装openssh-clients软件包。
2.重用脚本有利于提升运维工作效率。
3.nginx脚本下载地址:http://down.51cto.com/data/2458026
4.解决下面两个小问题:
问题1:
[root@Nginx init.d]# service nginx start
Starting nginx daemon...
/etc/init.d/nginx: line 7: $: command not found
$ DAEMOM 改成$DAEMOM 即可解决上述报错问题
问题2:
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
[root@Nginx init.d]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#使用nginx -c的参数指定nginx.conf文件的位置
问题3:
chkconfig nginx on #实际上重新启动系统,nginx服务并没有自动启动,不知道问题出现在哪?
[root@Nginx ~]# vim /etc/rc.local #目前先用这种方式解决自动启动nginx服务
/usr/local/nginx/sbin/nginx