学习该课程之前先学习linux的软件安装管理
1、linux的运行级别有下面几种类型
在后面的服务启动管理之中会被使用到
[root@weiyuan httpd-2.4.20]# runlevel
N 5
如果使用 init 0 就会让电脑关机,但是不一定可靠,最好使用shutdown -r now命令
linux的服务管理分为下面的两种形式
基于rpm默认安装的的服务管理
基于源代码安装的服务管理
[weiyuan@weiyuan 下载]$ chkconfig --list 是不能查看源码包的安装的服务的
![](https://images2018.cnblogs.com/blog/1151107/201805/1151107-20180520223826393-206947217.png)
[weiyuan@weiyuan 下载]$ netstat -tlunp
(No info could be read for "-p": geteuid()=500 but you should be root.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:10000 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:36763 0.0.0.0:* LISTEN -
tcp 0 0 :::111 :::* LISTEN -
tcp 0 0 :::59892 :::* LISTEN -
tcp 0 0 :::22 :::* LISTEN -
tcp 0 0 ::1:631 :::* LISTEN -
tcp 0 0 ::1:25 :::* LISTEN -
udp 0 0 0.0.0.0:10000 0.0.0.0:* -
udp 0 0 0.0.0.0:685 0.0.0.0:* -
udp 0 0 0.0.0.0:958 0.0.0.0:* -
udp 0 0 0.0.0.0:68 0.0.0.0:* -
udp 0 0 0.0.0.0:34886 0.0.0.0:* -
udp 0 0 0.0.0.0:111 0.0.0.0:* -
udp 0 0 0.0.0.0:631 0.0.0.0:* -
udp 0 0 :::50442 :::* -
udp 0 0 :::958 :::* -
udp 0 0 :::111 :::* -
[weiyuan@weiyuan 下载]$
接下来重点讲解下rpm包安装的服务如何启动和重启电脑之后自启动
rpm安装的软件服务可以使用下面的命令来进行启动
[weiyuan@weiyuan 下载]$ cd /etc/rc.d/init.d/
[weiyuan@weiyuan init.d]$ ls
abrt-ccpp dnsmasq lvm2-monitor postfix single
abrtd firstboot mdmonitor psacct smartd
abrt-oops functions messagebus quota_nld snmpd
acpid haldaemon netconsole rdisc snmptrapd
atd halt netfs restorecond spice-vdagentd
auditd htcacheclean network rngd sshd
autofs httpd NetworkManager rpcbind sssd
blk-availability ip6tables nfs rpcgssd sysstat
bluetooth iptables nfslock rpcidmapd udev-post
certmonger irqbalance ntpd rpcsvcgssd wdaemon
cpuspeed kdump ntpdate rsyslog winbind
crond killall oddjobd sandbox wpa_supplicant
cups lvm2-lvmetad portreserve saslauthd ypbind
[weiyuan@weiyuan init.d]$
[weiyuan@weiyuan init.d]$ cd /etc/init.d/
[weiyuan@weiyuan init.d]$ ls
abrt-ccpp dnsmasq lvm2-monitor postfix single
abrtd firstboot mdmonitor psacct smartd
abrt-oops functions messagebus quota_nld snmpd
acpid haldaemon netconsole rdisc snmptrapd
atd halt netfs restorecond spice-vdagentd
auditd htcacheclean network rngd sshd
autofs httpd NetworkManager rpcbind sssd
blk-availability ip6tables nfs rpcgssd sysstat
bluetooth iptables nfslock rpcidmapd udev-post
certmonger irqbalance ntpd rpcsvcgssd wdaemon
cpuspeed kdump ntpdate rsyslog winbind
crond killall oddjobd sandbox wpa_supplicant
cups lvm2-lvmetad portreserve saslauthd ypbind
[weiyuan@weiyuan init.d]$
我们可以看到
/etc/rc.d/init.d/ 和/etc/init.d/ 内容都是一样,使用了软连接的方式我们启动httpd apache服务
使用命令
[root@weiyuan init.d]# /etc/rc.d/init.d/httpd start
正在启动 httpd:httpd: Could not reliably determine the server's fully qualified domain name, using 183.221.250.11 for ServerName
[确定]
[root@weiyuan init.d]# /etc/init.d/httpd start
正在启动 httpd:
两种方式启动httpd是一样的
第三种方式也可以使用,但是主要service不是linxu的常用命令,只有redhat相关的操作系统采可以使用service启动服务,需要注意,所以最好情况下使用上面的绝对路径来启动服务,使用serive命令启动服务的时候
实际上执行的也是 /etc/rc.d/init.d/路径下的文件,源码包的默认情况下都是安装在/usr/local/下的,所以service是无法启动源码包安装的,如果要让service启动源码包的,例如httpd源码包安装在
/usr/local/apache2/bin/apactrl1 start启动,如果将/usr/local/apache2/bin/apactrl1复制到/etc/rc.d/init.d/路径下,也可以使用service管理源码包
[root@weiyuan init.d]# service httpd start
正在启动 httpd:
查看服务运行的状态
[root@weiyuan init.d]# service httpd status
httpd (pid 2361) 正在运行...
[root@weiyuan init.d]#
接下来我们讲解rpm包如何实现重启电脑之后服务能够自动启动
有下面的三种方式
[root@weiyuan weiyuan]# chkconfig --level 2345 httpd on
[root@weiyuan weiyuan]# chkconfig --list | grep httpd
httpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@weiyuan weiyuan]#
chkconfig --level 2345 httpd on这个命令重启电脑之后就能够保证httpd能够自启动
chkconfig 默认只对rpm安装的服务有效果,对二进制安装包没有效果
第二种方式:电脑重启之后每次都会读取/etc/rc.d/rc.local里面的任何命令,所以我们可以把启动httpd的命令放在该文件下面
[root@weiyuan rc.d]# vim rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/etc/init.d/httpd start
注意第一种方法和第二种方法只能二选一,否则httpd要被自启动两次,最好选择第二种方法,第二种方法对源代码安装的服务自启动是有效的
第三种:
[root@weiyuan rc.d]# vim ntsysv 会弹出图形化窗口开启rpm对应的服务,这种方式只有redhat的rpm有效,其他操作系统无效一定要注意
![](https://images2018.cnblogs.com/blog/1151107/201805/1151107-20180521185247472-242355136.png)
上面讲解了rpm包的服务管理,下面我们重点分析下源码包的服务管理的方法
上面这几个命令默认情况是不能管理源代码的安装服务的,默认管理的是rpm包的,因为rpm包默认安装在/etc/init.d目录下,而源码包不是安装在/etc/init.d目录下
[root@weiyuan rc.d]# /usr/local/apache2/bin/apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 183.221.250.11. Set the 'ServerName' directive globally to suppress this message
上面这个命令就是源码包启动服务的命令
如何让源码包服务自启动了
![](https://images2018.cnblogs.com/blog/1151107/201805/1151107-20180521194723085-1610580659.png)
接下来我们如何让service命令能够启动源码包的服务了,因为我们知道service 启动服务的时候,是去查找/etc/init.d下面的文件进行启动,我们可以把/usr/local/apache2/bin/
apachectl 复制到/etc/init.d目录下,也可以使用下面的软连接
![](https://images2018.cnblogs.com/blog/1151107/201805/1151107-20180521195507983-1810190131.png)
[root@weiyuan rc.d]# ln -s /usr/local/apache2/bin/apachectl /etc/init.d/apache
[root@weiyuan rc.d]# service apache start
![](https://images2018.cnblogs.com/blog/1151107/201805/1151107-20180521195844935-899835322.png)