一、服务的简介与分类 (C6版本)

1、服务的分类

image.png

2、查询已经安装的服务和区分服务

命令:chkconfig —list [服务名]
子选项:
—list:列出所有RPM包默认安装服务的自启动状态

二、RPM包默认安装的服务器管理(C6版本)

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
ONBOOT=yes #系统启动时激活网卡,后面会重复重启,方便远程连接
[root@localhost ~]# cat /etc/redhat-release #查看系统版本,6.8版本
CentOS release 6.8 (Final)
[root@localhost ~]# chkconfig —list #查看RPM包默认安装服务的自启动状态
[root@localhost ~]# chkconfig —list sshd #查看sshd服务的自启动状态
sshd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
※注:init可以切换级别,runlevel查看级别(看到的N代表系统开机后就直接进入到哪个级别)。
级别:0:关机 1:单用户,用于修复模式 2:缺失一些功能的 3:字符 4:保留级别,未使用 5:图形化 6:重启

1、独立服务器管理

(1)独立服务的启动管理
①使用/etc/init.d/目录中的启动脚本启动服务:
eg:[root@localhost ~]# /etc/init.d/httpd start #开启httpd服务
②使用service命令来启动独立的服务:
eg:[root@localhost ~]# service httpd start #开启httpd服务
[root@localhost ~]# netstat -anpt |grep :80 #查看是否有80端口
※注:rpm安装软件包后的脚本在 /etc/init.d/下,而源码包安装是自己指定路径,service命令找不到脚本,可以将源码包的脚本做个软链接放到 /etc/init.d/下,就可以用service命令命令开启停止源码包安装的服务了。

(2)独立服务的自启动管理
①使用chkconfig服务自启动管理命令:
eg:[root@localhost ~]# chkconfig [—level 运行级别] [独立服务名] [on/off]
[root@localhost ~]# chkconfig —level 35 httpd on #开启httpd服务3和5级别的自启动
[root@localhost ~]# chkconfig —list httpd
httpd 0:关闭 1:关闭 2:关闭 3:启用 4:关闭 5:启用 6:关闭
[root@localhost ~]# reboot #重启,然后再查看是否有80端口来判断httpd服务的开机自启动是否开启
②修改/etc/rc.d/rc.local文件设置服务自启动:
[root@localhost ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 244 8月 2 23:54 /etc/rc.d/rc.local #6版本有执行权限,7版本中默认没有执行权限
eg:[root@localhost ~]# vim /etc/rc.d/rc.local #修改/etc/rc.d/rc.local 配置文件
1.JPG
※注:默认的一行:开机创建一个文件,如果文件不存在,则生成这个文件;如果文件存在,更新时间,由更新时间对比当前时间date,就能得到系统启动了多长时间。如下图:
捕获.JPG

③使用ntsysv命令管理自启动(ntsysv 在字符界面也可以用,因为图形界面就是字符界面加一些图形显示功能来的)
ntsysv [— level 运行级别]
—level:可以指定设定自启动的运行级别
上下键:在不同服务之间移动。
空格键:选定或取消选定。
*为自启动。
tab键:在不同项目之间切换。
F1键:服务详情。

2、基于xinetd服务的管理

(1)基于xinetd服务的启动
[root@localhost ~]# yum -y install xinetd
用rsync服务来举例,rsync服务是用来进行数据备份的工具,使用873端口。
eg:[root@localhost ~]# vim /etc/xinetd.d/rsync #配置文件中disable = yes 将yes修改为no
2.JPG
[root@localhost ~]# service xinetd restart #重启 xinetd服务即可
(2)基于xinetd服务的自启动使用chkconfig命令管理自启动
使用ntsysv命令管理自启动
eg:[root@localhost ~]# chkconfig rsync [服务名] [on/off] #不用指定级别,依赖于xinetd的级别,先有xinetd,xinetd才能管理其他服务。

三、源码包安装的服务管理(C6版本)

1、源码包服务启动管理

/usr/local/apache2/bin/apachectl [start/stop/restart/…] #[开启/停止/重启/..]httpd服务

2、源码包服务的自动管理

eg:[root@localhost ~]# vim /etc/rc.d/rc.local #修改/etc/rc.d/rc.local 配置文件
/usr/local/apache2/bin/apachectl start #添加httpd服务开机自启动

3、让源码包服务被服务管理命令识别

接下来我们试着让源码包安装的apache变为和RPM包安装的apache一样,可以被service、chkconfig、ntsysv命令识别。
(1)卸载RPM包的apache服务,确保实验准确性。
[root@localhost ~]# yum -y remove httpd #卸载RPM包apache避免影响实验效果
[root@localhost ~]# service httpd restart
httpd: 未被识别的服务 #确认已经卸载完成。
(2)安装源码包apache服务,并启动
[root@localhost ~]# yum -y install gcc gcc-c++ #先安装编译工具(yum本地源)
[root@localhost ~]# yum -y install lrzsz #6版本没有rz命令,先安装
[root@localhost ~]# rz 上传LAMP.iso #上传LAMP.iso 文件
[root@localhost ~]# mkdir /disk /lamp #创建目录 /disk 和/lamp
[root@localhost ~]# mount -o loop /root/LAMP-64.iso /disk #将文件以设备方式挂载

[root@localhost ~]# cp /disk/lamp/ /lamp #因为挂载的iso文件只能读,不能写,所以把里面的东西先复制出来再解压
[root@localhost lamp]# tar -xvf ./httpd-2.4.7.tar.gz #解压httpd-2.4.7.tar.gz
[root@localhost lamp]# cd ./httpd-2.4.7 #进入httpd-2.4.7目录
[root@localhost httpd-2.4.7]# ls #查看有configure
[root@localhost httpd-2.4.7]# ./configure —prefix=/usr/local/apache2 #执行configure,定义安装路径
configure: error: APR not found. Please read the documentation. #报错,缺少apr(后续还缺少apr-util、pcre,下面先安装依赖)
[root@localhost httpd-2.4.7]# cd .. #切换至上一级目录
[root@localhost lamp]# tar -xvf ./apr-1.4.6.tar.gz #解压apr-1.4.6.tar.gz
[root@localhost lamp]# cd ./apr-1.4.6 #进入apr-1.4.6目录
[root@localhost apr-1.4.6]# ./configure #执行configure
[root@localhost apr-1.4.6]# make && make install #编译并且安装
[root@localhost apr-1.4.6]# cd .. #切换至上一级目录
[root@localhost lamp]# tar -xvf ./apr-util-1.4.1.tar.gz #解压apr-util-1.4.1.tar.gz
[root@localhost lamp]# cd apr-util-1.4.1 #进入apr-util-1.4.1目录
[root@localhostapr-util-1.4.1]# ./configure -with-apr=/usr/local/apr #执行configure,与apr路径相同
[root@localhostapr-util-1.4.1]# make && make install #编译并且安装
[root@localhostapr-util-1.4.1]# cd .. #切换至上一级目录
[root@localhost lamp]# tar -xvf pcre-8.34.tar.gz #解压pcre-8.34.tar.gz
[root@localhost lamp]# cd pcre-8.34 #进入pcre-8.34目录
[root@localhost pcre-8.10]# ./configure #执行configure
[root@localhost pcre-8.10]# make && make install #编译并且安装
[root@localhost pcre-8.10]# cd /lamp/httpd-2.4.7 #进入httpd-2.4.7目录
[root@localhost httpd-2.4.7]# ./configure —prefix=/usr/local/apache2 #执行configure,定义安装路径
[root@localhost httpd-2.4.7]# make -j 4 #编译比较慢,-j后的数字是核心数量为4,4个CPU同时编译
[root@localhost httpd-2.4.7]# make install #安装
[root@localhost httpd-2.4.7]# /usr/local/apache2/bin/apachectl start #开启httpd服务
[root@localhost ~]# netstat -anpt |grep :80 #查看显示有80端口,httpd服务开启成功
(3)源码包apache服务被service命令管理启动
[root@localhost httpd-2.4.7]# vim /etc/rc.d/rc.local #设置httpd服务开机自启动
/usr/local/apache2/bin/apachectl start
[root@localhost httpd-2.4.7]# reboot #重启电脑
[root@localhost ~]# netstat -anpt |grep :80 #查看是否有80端口
※注:现在service和chkconfig还不能用,service命令其实只是在/etc/init.d/目录中查找是否有服务启动脚本,所以我们只需要做个软连接把源码包的启动脚本链到/etc/init.d/目录中,就能被 service命令管理了
[root@localhost ~]# ln -s /usr/local/apache2/bin/apachectl /etc/init.d/apache2 #软链接
[root@localhost ~]# service apache2 stop #测试service命令,stop生效
[root@localhost ~]# service apache2 start #service命令开启httpd服务
[root@localhost ~]# netstat -anpt |grep :80 #查看是否有80端口
[root@localhost ~]# /etc/init.d/apache2 stop #也可用软链接停止httpd服务
*(4)chkconfig命令管理自启动

[root@localhost ~]# vim /etc/init.d/apache2 #修改源码包apache启动脚本(此文件是软连接,所以修改的还是源码包的启动脚本。)
#chkconfig:35 86 76 #86 76是启动和停止的启动顺序(去/etc/rc5.d里面看没有占用的启动停止顺序数字)
#description:source pack
※注:加上这两行就在chkconfig命令里有启动停止顺序了。
[root@localhost ~]# chkconfig —add apache2 #将apache2服务添加到chkconfig命令
[root@localhost ~]# chkconfig —list apache2 #用chkconfig命令列出apache2的自启动信息
(5)ntsysv命令管理源码包apache2
ntsysv命令其实是和chkconfig命令使用同样的管理机制,也就是说ntsysv已经可以对源码包的apache2进行启动管理了。
image.png

四、服务管理流程图(C6版本)

捕获1.JPG

五、C7服务管理

1、systemd优势

从centos7开始,systemd替代了之前版本中的init。使用systemd机制来管理服务。那么systemd又有什么优势呢?
(1)并行处理所有服务,加速开机流程。
在init启动流程中,服务是一项一项启动的。在systemd可以所有服务同时启动。
(2)命令相对简单。
和之前的SystemV相比,所有操作都是systemctl命令来控制。而之前的systemV中有init、service、chkconfig等。
(3)服务依赖性检测。
如果B服务是架构在A服务上启动的,当没有启动A服务时就先启动B服务的话,systemd会自动启动A服务。

2、systemd的配置文件位置

/usr/lib/systemd/system/:服务启动脚本存放位置。
/run/systemd/system/:系统执行过程中产生的服务脚本。
/etc/systemd/system/:管理员根据自己主机系统的需求所创建的执行脚本。操作系统启动后到底会不会执行某些服务其实是看/etc/systemd/system/目录的。

3、systemctl管理服务

基本上服务的管理都是通过systemctl命令来完成的。
(1)systemctl:管理服务状态,开机是否启动等。
格式:
systemctl 选项 执行服务的守护进程名称
选项:
start:启动服务
stop:停止服务
restart:重启
reload:不关闭服务的情况下,重新读取服务程序配置文件
enable:开机启动
disable:开机不启动
status:查看指定服务状态
is-enabled:查看指定服务是否为开机启动。enabled启动/disable不启动
eg:[root@localhost ~]# systemctl status sshd #查看sshd服务状态(最后七八行是某个日志信息)
Active: active (running) since 一 2021-08-02 21:05:45 CST; 3min 58s ago #正在运行
[root@localhost ~]# systemctl is-enabled sshd #查看服务是否是开机自启动
[root@localhost ~]# systemctl disable sshd
Removed symlink /etc/systemd/system/multi-user.target.wants/sshd.service. #删除启动脚本软链接
[root@localhost ~]# systemctl enable sshd
Created symlink from /etc/systemd/system/multi-user.target.wants/sshd.service to /usr/lib/systemd/system/sshd.service. #创建启动脚本软链接
(2)后缀有以下几种:
.service:用来启动和控制守护进程和进程的服务单元。 (加不加都可以)
.target:执行环境类型,也就是启动流程中的multi-user.target和graphical.target。相当于对服务和启动事项的一个分组。

(3)查看系统上所有服务:
命令:systemctl [选项]
选项:
list-units:显示当前启动unit,添加—all选项则列出启动和未启动的所有unit。
list-unit-files:显示/usr/lib/systemd/system/的unit状态。
eg:systemctl list-unit-files
(4)切换操作环境:
在centos7中虽然还是可以使用init * 的命令。但是已经没有了运行级别的概念。这种启动/运行级别的概念转变成了切换操作环境。那么我们可以切换的操作环境有:(不用下面方式太麻烦,用init命令切换即可)
①graphical.target:图形化界面。 (5级别)
②multi-user.target:命令行模式。(3级别)
③rescue.target:救援模式。
④emergency.target:紧急处理系统的错误,需要使用root登录,再无法使用rescue.target的情况下可以尝试使用此模式。
⑤shutdown.target:关机。

4、使用systemctl管理源码包nginx:(启动、停止、重启、开机自启动)

(1)源码包安装Nginx:
[root@localhost ~]# yum -y install gcc* #yum本地源安装编译工具gcc、gcc-c++
[root@localhost ~]# rz #rz上传nginx-1.18.0.tar.gz
[root@localhost ~]# tar -xvf ./nginx-1.18.0.tar.gz #解压nginx-1.18.0.tar.gz
[root@localhost ~]# cd ./nginx-1.18.0/ #进入nginx-1.18.0目录
[root@localhost nginx-1.18.0]# ./configure —prefix=/usr/local/nginx #执行configure,提示缺少函数库
[root@localhost nginx-1.18.0]# yum -y install pcre pcre-devel zlib zlib-devel #yum本地源安装函数库
[root@localhost nginx-1.18.0]# ./configure —prefix=/usr/local/nginx #执行configure
[root@localhost nginx-1.18.0]# make && make install #编译并安装
[root@localhost nginx-1.18.0]# netstat -anpt |grep :80 #查看是否有80端口(无)
[root@localhost nginx-1.18.0]# /usr/local/nginx/sbin/nginx #开启nginx服务
[root@localhost nginx-1.18.0]# netstat -anpt |grep :80 #查看是否有80端口(有)
[root@localhost nginx-1.18.0]# /usr/local/nginx/sbin/nginx -s stop #停止nginx服务
(2)使用systemctl管理源码包nginx:
[root@localhost nginx-1.18.0]# vim /usr/lib/systemd/system/nginx.service #编辑启动脚本
捕获.JPG
[root@localhost ~]# systemctl daemon-reload #重新读取加载服务配置文件
[root@localhost nginx-1.18.0]# systemctl start nginx.service #systemctl命令启动nginx服务
[root@localhost nginx-1.18.0]# netstat -anpt |grep :80 #查看是否有80端口(有)
[root@localhost ~]# systemctl enable nginx.service #systemctl命令设置nginx服务开机自启动
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. #创建启动脚本软链接
[root@localhost nginx-1.18.0]# reboot #重启电脑
[root@localhost nginx-1.18.0]# netstat -anpt |grep :80 #查看是否有80端口(无)
(3)修改/etc/rc.d/rc.local文件设置服务自启动:
如果源码包安装的服务没有被systemctl命令管理的需求,那就去写rc.local 文件就可以了。写之前要看它有没有可执行权限。
[root@localhost ~]# ll /etc/rc.d/rc.local #查看rc.local 文件权限
-rw-r—r—. 1 root root 473 10月 31 2018 /etc/rc.d/rc.local #7版本默认没有可执行权限
[root@localhost ~]# chmod +x /etc/rc.d/rc.local #给文件加可执行权限
[root@localhost ~]# vim /etc/rc.d/rc.local #编辑rc.local 文件设置服务自启动