Linux服务的简介与分类

服务的简介与分类

服务的分类

在这里插入图片描述

查询已安装的服务和区分服务

#列出所有rpm包默认安装服务的自启动状态
[root@localhost ~]# chkconfig --list atd
atd            	0:关闭	1:关闭	2:关闭	3:启用	4:启用	5:启用	6:关闭
[root@localhost ~]# chkconfig --list sshd
sshd           	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭

centos6服务管理

独立服务管理
#使用启动脚本启动服务
[root@localhost ~]# /etc/init.d/httpd start
正在启动 httpd:

#使用service命令来启动独立的服务
[root@localhost ~]# service httpd start
正在启动 httpd:

#使用chkconfig服务自启动管理命令
[root@localhost ~]# chkconfig --level 级别 httpd on
#修改rc.local文件设置服务自启动
[root@localhost ~]# vim /etc/rc.d/rc.local
#使用ntsysv管理自启动
ntsysv [-- level 运行级别]
- -level:可以指定设定自启动的运行级别
上下键:在不同服务之间移动。
空格键:选定或取消选定。*为自启动。
tab键:在不同项目之间切换。
F1键:服务详情。

运行级别
0:关机 1:单用户模式 2:某些网络功能没有开启
3:完全功能的字符界面 4:保留 5:图形化 6:重启系统
基于xinetd服务管理

早期的linux操作系统认为都是监听网络,就设置一个超级进程监听全部端口,那个端口有数据就监听那个程序,xinetd就此诞生了。xinetd管理所有端口,当端口有请求到达时就启动对应端口处理服务进程,导致结果是响应变慢。

#安装xinetd
[root@localhost ~]# yum -y install xinetd
#查看配置文件
[root@localhost ~]# vim /etc/xinetd.d/rsync
#服务名称
service rsync
{
        disable = yes   #服务不启动 修改为no就是服务启动
        flags           = REUSE  #设定TCP、IP socket可重用
        socket_type     = stream #套接字类型
        wait            = no     #允许多个连接同时连接
        user            = root   #启动用户为root
        server          = /usr/bin/rsync   #服务启动程序
        server_args     = --daemon    
        log_on_failure  += USERID  #登录失败后,记录用户的ID
}
#重启服务
service xinetd restart
源码包服务管理
启动管理
/usr/local/apache2/bin/apachectl start|stop|restart|...
自动管理
[root@localhost ~]# vim /etc/rc.d/rc.local
#内容
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
/usr/local/nginx/sbin/nginx
让源码包服务被服务管理命令识别

1.卸载apache服务,保证准确性

[root@localhost ~]# yum -y remove httpd

2.安装源码包服务,并启动

[root@localhost ~]# yum -y install gcc* pcre pcre-devel
[root@localhost ~]# cd /lamp/
[root@localhost lamp]# tar -xvf apr-1.4.6.tar.gz
[root@localhost lamp]# cd apr-1.4.6
[root@localhost apr]# ./configure
[root@localhost apr]#make && make install
[root@localhost apr]# cd ..
[root@localhost lamp]# tar -xvf apr-util-1.4.1.tar.gz
[root@localhost lamp]# cd apr-util-1.4.1
[root@localhost apr-util-1.4.1]# ./configure -with-apr=/usr/local/apr
[root@localhost apr-util-1.4.1]# make && make install
[root@localhost apr-util-1.4.1]# cd ..
[root@localhost lamp]# tar -xvf pcre-8.10.tar.gz
[root@localhost lamp]# cd pcre-8.10
[root@localhost pcre-8.10]# ./configure
[root@localhost pcre-8.10]# make && make install
[root@localhost pcre-8.10]# cd ..
[root@localhost lamp]# tar -xvf httpd-2.4.7.tar.gz
[root@localhost lamp]# cd httpd-2.4.7
[root@localhost httpd-2.4.7]# ./configure --prefix=/usr/local/apache2
[root@localhost httpd-2.4.7]# make && make install
[root@localhost httpd-2.4.7]# /usr/local/apache2/bin/apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain
name, using localhost.localdomain. Set the 'ServerName' directive globally to
suppress this message
[root@localhost httpd-2.4.7]# netstat -tlun | grep 80
tcp 0 0 :::80 :::*
LISTEN
#启动源码包apache,查看端口是否启动。
3)源码包apache服务被service命令管理启动
[root@localhost ~]# ln -s /usr/local/apache2/bin/apachectl /etc/init.d/apache
#service命令其实只是在/etc/init.d/目录中查找是否有服务启动脚本,所以我们只需要做个软连接把源码包的启动脚本链到/etc/init.d/目录中,就能被service命令管理了。
[root@localhost ~]# service apache restart
AH00558: httpd: Could not reliably determine the server's fully qualified domain
name, using localhost.localdomain. Set the 'ServerName' directive globally to
suppress this message
#测试service命令,restart和start生效

centos7服务管理

systemd有哪些优点?
  1. 并行处理所有服务、加速开机流程;在init启动流程中,服务是一项一项启动的。在systemd可以所有服务同时启动

  2. 命令相对简单:和之前的SystemV相比,所有操作都是systemctl命令来控制。而之前的systemV中有init、service、chkconfig等。

  3. 服务依赖性检测:如果B服务是架构在A服务上启动的,当没有A服务时就先启动b服务的话,systemd会自动启动A服务

    systemd的配置文件位置
/usr/lib/systemd/system   #服务启动脚本存放位置
/run/systemd/system   #系统执行过程中产生的服务脚本
/etc/systemd/system   #管理员根据自己主机系统的需求所创建的执行脚本。
systemctl管理服务

systemctl:管理服务状态

格式:
	systemctl 选项 执行服务的守护进程名称
选项:	
	start、stop、restart、status
	reload(重新读取服务配置文件)、enable(开机启动)、disable(开机不启动)、is—enable(查看是否开机自启动)
	
常见后缀:
	.service:用来启动和控制守护进程和进程的服务单元
	.target:执行环境类型,也就是启动流程中窗口化和命令行
切换系统环境
centos7还是可以使用init*的命令。没有运行级别的概念,只有切换操作环境。
	1.图形化界面:graphical.target
	2.命令行模式:multi-user.target
	3.rescue.target:救援模式。 
	4.emergency.target:紧急处理系统的错误
命令格式:systemctl isolate + 操作系统环境
使用systemctl管理源码包nginx

安装依赖关系

[root@localhost nginx-1.6.2]# yum -y install gcc* zlib zlib-devel.pcre pcre-devel

编译执行安装

[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.6.2]# make && make install

启动

[root@localhost nginx-1.6.2]# /usr/local/nginx/sbin/nginx

在这里我们将nginx暂停,会发现失败

[root@localhost nginx-1.6.2]# systemctl stop nginx
Failed to stop nginx.service: Unit nginx.service not loaded.

使用vim打开一个文件

[root@localhost nginx-1.6.2]# vim /usr/lib/systemd/system/nginx.service
#输入配置文件
[Unit]
Description=nginx   #服务描述
After=network.target  #表示服务在network服务启动后启动

[Service]
Type=forking     #表示后台运行模式
ExecStart=/usr/local/nginx/sbin/nginx  #服务启动脚本位置
ExecReload=/usr/local/nginx/sbin/nginx -s reload  #重新加载配置文件
ExecStop=/usr/local/nginx/sbin/nginx -s stop   #停止服务
PrivateTmp=true

[Install]
WantedBy=multi-user.target   #表示此unity依附于multi模式

#系统后台服务重新读取
[root@localhost nginx-1.6.2]# systemctl daemon-reload 
[root@localhost nginx-1.6.2]# systemctl stop nginx
[root@localhost nginx-1.6.2]# ps -aux | grep nginx
root      23910  0.0  0.0  24864   756 ?        Ss   19:08   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    23911  0.0  0.1  27372  1508 ?        S    19:08   0:00 nginx: worker process
root      24105  0.0  0.0 112824   988 pts/2    R+   19:14   0:00 grep --color=auto nginx

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值