Day48——Linux入门学习2

Linux初学2

进程管理

快捷键:Ctrl+Shift+T打开多个终端;Ctrl+D关闭终端

进程与程序 (process & program)

如何产生一个进程呢?其实很简单啦,就是『执行一个程序或指令』就可以触发一个事件而取得 一个 PID !系统应该是仅认识 binary file 的,那么当要让系统工作的时候,就是需要启动一个 binary file 啰,那个 binary file 就是程序 (program)

  • 程序 (program):通常为 binary program ,放置在储存媒体中 (如硬盘等), 为实体文 件的型态存在;
  • 进程 (process):程序被触发后,执行者的权限与属性、程序的程序代码与所需数据等都会被加载内存中, 操 作系统并给予这个内存内的单元一个标识符 (PID),可以说,进程就是一个正在运作中的程序
bash 环境下的工作管理

登入 bash 之后, 就是取得一 个名为 bash 的 PID 了,而在这个环境底下所执行的其他指令, 就几乎都是所谓的子进程了。在这个单一的 bash 接口下,可以同时进行d多个工作!

[root@server1 ~]# cp file1 file2 &

在这一串指令中,重点在那个 & 的功能,他表示将 file1 这个文件复制为 file2 ,且放置于背景中执行, 且在这个端口可以做其他的工作,它完成后会在终端接口限时完成的消息。

将“目前”的工作丢到背景中“暂停”:[ctrl]-z

想个情况:如果我正在使用 vim ,却发现我有个文件不知道放在哪里,需要到 bash 环境下进行搜寻,此时是否要结束 vim 呢?不需要,只要暂时将 vim 给他丢到背景当中等待即可

实验:

[root@study ~]# vim ~/.bashrc 
  在 vim 的一般模式下,按下 ctrl+z 这两个按键 
[1]+ Stopped         vim ~/.bashrc 
[root@server1 ~]#        取得了前景的操控权
[root@server1 ~]# find / -print
......按下 ctrl+z 
[2]+  Stopped                 find / -print

在 vim 的一般模式下,按下 ctrl 及 z 这两个按键,屏幕上会出现 [1] ,表示这是第一个工作, 而那个 + 代表最近一个被丢进背景的工作,且目前在背景下预设会被取用的那个工作,而那个 Stopped 则代表目前这个工作的状态。

观察目前的背景工作状态: jobs
[root@study ~]# jobs [-lrs]
选项与参数: -
l :除了列出 job number 与指令串之外,同时列出 PID 的号码; 
-r :仅列出正在背景 run 的工作; 
-s :仅列出正在背景当中暂停 (stop) 的工作。

实验:

观察目前bash当中所有的工作,对应的PID

[root@server1 ~]# jobs -l
[1]- 13165 Stopped                 vim ~/.bashrc
[2]+ 13167 Stopped                 find / -print

其实 + 代表最近被放到背景的工作号码, - 代表最近最后第二个被放置到背景中的工作号码

将背景工作拿到前景来处理:fg

把上面刚丢在背景中执行的工作,拿到前面来处理一下:

[root@study ~]# fg %jobnumber 
选项与参数: 
%jobnumber :jobnumber 为工作号码(数字)。注意,那个 % 是可有可无的!

实验:

[root@server1 ~]# jobs -l
[1]- 13165 Stopped                 vim ~/.bashrc
[2]+ 13167 Stopped                 find / -print

[root@server1 ~]# fg    立刻ctrl+z
[2]+  Stopped                 find / -print

[root@server1 ~]# fg %1  立刻ctrl+z
vim ~/.bashrc
[1]+  Stopped                 vim ~/.bashrc

[root@server1 ~]# jobs -l
[1]+ 13165 Stopped                 vim ~/.bashrc
[2]- 13167 Stopped                 find / -print
让工作在背景下的状态变成运作中: bg

ctrl+z 可以将目前的工作丢到背景底下去暂停, 那么如何让一个工作在背景底下Run

实验一,执行 find / -perm /7000 > /tmp/text.txt 后,立刻丢到背景去暂停

[root@server1 ~]# find / -perm /7000 > /tmp/text.txt
^Z
[3]+  Stopped                 find / -perm /7000 > /tmp/text.txt

实验二,让该工作在背景下进行,并且观察

[root@server1 ~]# jobs ; bg %3 ; jobs
[1]-  Stopped                 vim ~/.bashrc
[2]   Stopped                 find / -print
[3]+  Stopped                 find / -perm /7000 > /tmp/text.txt
[3]+ find / -perm /7000 > /tmp/text.txt &
[1]+  Stopped                 vim ~/.bashrc
[2]   Stopped                 find / -print
[3]-  Running                 find / -perm /7000 > /tmp/text.txt &

指令列最后方多了一个 & 的符号! 代表该工作被启动在背景当中了

管理背景当中的工作: kill

想要将该工作直接移除?或者是将该工作重新启动?这个时候就得需要给予该工作一个信号 (signal),kill

[root@study ~]# kill -signal %jobnumber 
[root@study ~]# kill -l 
选项与参数: 
-l :这个是 L 的小写,列出目前 kill 能够使用的signal有哪些? 
signal :代表给予后面接的那个工作什么样的指示!用man 7 signal 可知: 
-1 :重新读取一次参数的配置文件 (类似 reload); 
-2 :代表与由键盘输入 ctrl+c 同样的动作; 
-9 :立刻强制删除一个工作; 
-15:以正常的进程方式终止一项工作。与 -9 是不一样的。

实验:

找出目前的 bash 环境下的背景工作,并将该工作强制删除

[root@server1 ~]# jobs
[1]+  Stopped                 vim ~/.bashrc
[2]   Stopped                 find / -print

[root@server1 ~]# kill -9 %2; jobs
[1]+  Stopped                 vim ~/.bashrc
[2]   Stopped                 find / -print
过一会再使用这个命令就发现2号就不见了

找出目前的 bash 环境下的背景工作,并将该工作正常终止

root@server1 ~]# jobs
[1]+  Stopped                 vim ~/.bashrc

[root@server1 ~]# kill -15 %1

-9 这个 signal 通常是用在,强制删除一个不正常的工作,时所使用的,

-15 则是以正常步骤结束一项工作(15 也是默认值)


进程的观察

进程如此重要,我们如何查阅系统上面正在运作当中的进程

ps :将某个时间点的进程运作情况撷取下来
[root@study ~]# ps aux    观察系统所有的进程数据 [root@study ~]# ps -lA    也是能够观察所有系统的数据 [root@study ~]# ps axjf   连同部分进程树状态

仅观察自己的 bash 相关进程

实验,查看自己bash有关的进程

[root@server1 ~]# ps -l
F S   UID    PID   PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  11528  10763  0  80   0 - 58163 do_wai pts/0    00:00:00 su
4 S     0  11539  11528  0  80   0 - 29089 do_wai pts/0    00:00:00 bash
4 S     0  12945  11539  0  80   0 - 29086 do_wai pts/0    00:00:00 bash
4 T     0  13165  12945  0  80   0 - 37340 do_sig pts/0    00:00:00 vim
0 R     0  14794  12945  0  80   0 - 38309 -      pts/0    00:00:00 ps

认识一下:

  • F:代表这个进程旗标 (process flags),说明这个进程的总结权限

    • 若为 4 表示此进程的权限为 root
    • 若为 1 则表示此子进程仅进行复制
  • S:代表这个进程的状态 (STAT)

    • R (Running):该程序正在运作中
    • S (Sleep):该程序目前正在睡眠状态,但可以被唤醒
    • D :不可被唤醒的睡眠状态,通常这支程序可能在等待 I/O 的情况
    • T :停止状态(stop)
    • Z (Zombie):僵尸状态,进程已经终止但却无法被移除至内存外
  • UID/PID/PPID:代表此进程被该 UID 所拥有/进程的 PID 号码/此进程的父进程 PID 号码

  • C:表示CPU使用率

  • NI:代表此进程被 CPU 所执行的优先级,越小越快被执行

  • ADDR/SZ/WCHAN:都与内存有关

  • TTY:登入者的终端机位置

  • TIME:此进程实际花费 CPU 运作的时间

  • CMD:造成此进程的触发程序之指令为什么

动态观察进程的变化

top 可以持续侦测进程运作的状态

实验:

每两秒钟更新一次 top ,观察整体信息

[root@server1 ~]# top -d 2
top - 21:48:49 up  4:09,  2 users,  load average: 0.02, 0.04, 0.05
Tasks: 275 total,   1 running, 272 sleeping,   2 stopped,   0 zombie
%Cpu(s):  0.8 us,  1.0 sy,  0.0 ni, 98.2 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2028116 total,    77972 free,   885468 used,  1064676 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   845384 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                 
  7647 root      20   0  345108  35224  15944 S   2.5  1.7   1:07.70 X  
.........

按下P :以 CPU 的使用资源排序显示; 
按下M :以 Memory 的使用资源排序显示;
离开按下q

实验:

将 top 的信息进行 2 次,然后将结果输出到 /tmp/top.txt

[root@server1 ~]# top -b -n 2 > /tmp/top.txt
[root@server1 ~]# cat /tmp/top.txt 

仅观察单一进程!

实验:

自己的 bash PID 可由 $$ 变量取得,请使用 top 持续观察该 PID

server1 ~]# echo $$
12945

查看一个进程
[root@server1 ~]# top -d 2 -p 12945

top - 21:55:29 up  4:16,  2 users,  load average: 0.07, 0.04, 0.05
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.8 us,  3.1 sy,  0.0 ni, 96.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2028116 total,    78272 free,   885088 used,  1064756 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   845768 avail Mem 
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND 
 12945 root      20   0  116344   3160   1812 S   0.0  0.2   0:00.16 bash     

网络管理

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P6qs9Neu-1584951250683)(D:\environment\45.20-3-22\linux2.assets\1584844566350.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7trgDwMf-1584951250684)(D:\environment\45.20-3-22\linux2.assets\1584847093438.png)]

网络设定 (手动设定与 DHCP 自动取得)
手动设定固定 IP

需要参数:

  • IP
  • 子网掩码(netmask,perfix)
  • 网关(gateway)
  • DNS(最多3个)
网络参数可自动取得

这里用手动设置固定IP

实验:

因为自己是在WIN10上安装虚拟机运行Linux,以及使用桥接模式连接Linux网络

首先在cmd中ipconfig,查看自己的网络信息,需要根据这个信息来分配给Linux的IP地址,

如:电脑IP是在192.168.43.1,我就试试192.168.43.100分给linux,在此之前需要保证这个地址,主机ping不通

接着在linux中进行配置

[root@server1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

ens33为网卡,对他进行配置

[root@server1 ~]# cd /etc/sysconfig/network-scripts
[root@server1 network-scripts]# ls
ifcfg-ens33  ifdown-isdn      ifup          ifup-plip      ifup-tunnel

ifcfg-ens33这个文件对它进行Vim编辑,和下面的文件内容保持类似

TYPE=Ethernet
PROXY_METHOD=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.43.100  根据自己情况写
PREFIX=24              255.255.255.0
GATEWAY=192.168.43.1   根据自己情况写
DNS1=114.114.114.114

保存退出,重启网络 service network restart

验证:

查看IP地址

[root@server1 ~]# ip addr
....
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:84:b0:a0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.43.100/24 brd 192.168.43.255 scope global noprefixroute ens33
......

查看网关

[root@server1 network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.43.1    0.0.0.0         UG    100    0        0 ens33
192.168.43.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

ping 通百度

[root@server1 network-scripts]# ping baidu.com
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=48 time=54.3 ms
....

通了就可以上网了~

显示具体的网络接口信息

[root@server1 ~]# nmcli connection show ens33 
connection.id:                          ens33
connection.uuid:                        a9b1b94c-3fcf-4431-ba17-012c9ac336fd
connection.stable-id:                   --
connection.type:                        802-3-ethernet
.......

显示所有设配状态

[root@server1 ~]# nmcli device status 
DEVICE      TYPE      STATE      CONNECTION 
ens33       ethernet  connected  ens33      
virbr0      bridge    connected  virbr0     
lo          loopback  unmanaged  --         
virbr0-nic  tun       unmanaged  -- 

网络接口的启用与停用:

停用:

# nmcli connection down ens33

启用:

# nmcli connection up ens33


软件管理

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-u0JopB5J-1584951250684)(D:\environment\45.20-3-22\linux2.assets\1584847125889.png)]

RPM

RPM 全名是『 RedHat Package Manager 』简称则为 RPM

RPM 是以一种数据库记录的方式来将你所需要的软件安装到 你的 Linux 系统的一套管理机制

一般来说,RPM 类型的文件在安装的时候,会先去读取文件内记载的设定参数内容,然后将该数据 用来比对 Linux 系统的环境,以找出是否有属性相依的软件尚未安装的问题

若环境检查合格了,那么 RPM 文件就开始被安装到你的 Linux 系统上。安装完毕后,该软件相关 的信息就会被写入 /var/lib/rpm/ 目录下的数据库文件中了。

RPM 的优点

  • RPM 内含已经编译过的程序与配置文件等数据,可以让用户免除重新编译的困扰;
  • RPM 在被安装之前,会先检查系统的硬盘容量、操作系统版本等,可避免文件被错误安装;
  • RPM 文件本身提供软件版本信息、相依属性软件名称、软件用途说明、软件所含文件等信息,便于了解软 件;
  • RPM 管理的方式使用数据库记录 RPM 文件的相关参数,便于升级、移除、查询与验证。

目录的意义

/etc一些配置文件放置的目录,例如 /etc/crontab
/usr/bin一些可执行文件案
/usr/lib一些程序使用的动态函式库
/usr/share/doc一些基本的软件使用手册与说明文件
/usr/share/man一些 man page 文件
RPM 安装 (install)

因为安装软件是 root 的工作,因此得要是 root 的身份才能够操作 rpm 指令的

RPM 查询 (query)

RPM 在查询的时候,其实查询的地方是在 /var/lib/rpm/ 这个目录下的数据库文件

RPM 也 可以查询未安装的 RPM 文件内的信息

[root@study ~]# rpm -qa                             已安装软件
[root@study ~]# rpm -q[licdR] 已安装的软件名称         已安装软件

[root@study ~]# rpm -qf 存在于系统上面的某个文件名       已安装软件 
[root@study ~]# rpm -qp[licdR] 未安装的某个文件名      查阅 RPM 文件

实验:

  1. 找出你的 Linux 是否有安装 logrotate 这个软件
[root@server1 sbin]# cd /root
[root@server1 ~]# rpm -q logrotate
logrotate-3.8.6-17.el7.x86_64
[root@server1 ~]# rpm -q logrotating
package logrotating is not installed
  1. 列出上题当中,属于该软件所提供的所有目录与文件
[root@server1 ~]# rpm -ql logrotate
/etc/cron.daily/logrotate
/etc/logrotate.conf
/etc/logrotate.d
/etc/rwtab.d/logrotate
/usr/sbin/logrotate
/usr/share/doc/logrotate-3.8.6
/usr/share/doc/logrotate-3.8.6/CHANGES
/usr/share/doc/logrotate-3.8.6/COPYING
........
可以看出该软件到底提供了多少的文件与目录,也可以追踪软件的数据
  1. 列出 logrotate 这个软件的相关说明数据
[root@server1 ~]# rpm -qi logrotate
Name        : logrotate
Version     : 3.8.6
Release     : 17.el7
Architecture: x86_64
Install Date: Thu 19 Mar 2020 08:04:16 PM CST
Group       : System Environment/Base
Size        : 107156
License     : GPL+
Signature   : RSA/SHA256, Fri 15 Jun 2018 08:45:44 PM CST, Key ID 199e2f91fd431d51
Source RPM  : logrotate-3.8.6-17.el7.src.rpm
Build Date  : Fri 15 Jun 2018 07:53:37 PM CST
Build Host  : x86-017.build.eng.bos.redhat.com
Relocations : (not relocatable)
Packager    : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
Vendor      : Red Hat, Inc.
URL         : https://github.com/logrotate/logrotate
Summary     : Rotates, compresses, removes and mails system log files
Description :
...........

Install the logrotate package if you need a utility to deal with the
log files on your system.
信息,包括了软件名称、版本、开发商、SRPM 文件名、打包次数、简单说明信息、软件打包者、安装日期等等
  1. 分别仅找出 logrotate 的配置文件与说明档
[root@server1 ~]# rpm -gc logrotate

[root@study ~]# rpm -qc logrotate [root@study ~]# rpm -qd logrotate
  1. 若要成功安装 logrotate ,他还需要什么文件的帮忙
[root@server1 ~]# rpm -qR logrotate
/bin/sh
/bin/sh
config(logrotate) = 3.8.6-17.el7
......
  1. 由上面的范例五,找出 /bin/sh 是哪个软件提供的
[root@server1 ~]# rpm -qf /bin/sh
bash-4.2.46-31.el7.x86_64
  1. 假设我有下载一个 RPM 文件,想要知道该文件的需求文件,该怎么办

yum

利用进行查询、安装、升级与移除功能

查询功能

yum [list|info|search|provides|whatprovides] 参数

实验:

  1. 搜寻磁盘阵列 (tomcat) 相关的软件有哪些
[root@server1 ~]# yum search tomcat
.....
  1. 找出 tomcat这个软件的功能为何
[root@server1 ~]# yum info tomcat
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Available Packages
Name        : tomcat
Arch        : noarch
Version     : 7.0.76
Release     : 7.el7_5
Size        : 91 k
Repo        : dvd
...............
  1. 列出 yum 服务器上面提供的所有软件名称
[root@server1 ~]# yum list
  1. 列出目前服务器上可供本机进行升级的软件有哪些
[root@server1 ~]# yum list updates
  1. 列出提供 passwd 这个文件的软件有哪些
[root@server1 ~]# yum provides passwd
....
....
安装/升级功能

就利用 install 与 update 这两项

  1. 尝试安装tomcat
yum -y install tomcat
  1. 安装目录在 /usr/share/tomcat , 用命令切到这个目录
[root@server1 ~]# cd /usr/share/tomcat
[root@server1 tomcat]# ls
bin  conf  lib  logs  temp  webapps  work
  1. 已经安装好了,查看一下 tomcat的状态
[root@server1 tomcat]# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
  1. 运行tomcat
[root@server1 tomcat]# systemctl start tomcat
[root@server1 tomcat]# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-03-23 18:29:43 CST; 5s ago
  1. 访问发现404

  2. 安装管理界面

[root@server1 tomcat]# cd webapps
[root@server1 webapps]# ls
[root@server1 webapps]#   空的
安装管理界面
[root@server1 webapps]# yum install tomcat-webapps tomcat-admin-webapps
  1. 安装成功,查看目录
[root@server1 webapps]# ls
examples  host-manager  manager  ROOT  sample
  1. 再次访问

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AKXgl5FR-1584951250686)(D:\environment\45.20-3-22\linux2.assets\1584930833669.png)]

  1. 停止tomcat 服务
[root@server1 webapps]# systemctl stop tomcat
[root@server1 webapps]# 
  1. 重启 tomcat服务
[root@server1 webapps]# systemctl restart tomcat

本地yum源配置

1) 准备iso文件

**[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tLfJSAWd-1584951250687)(D:\environment\45.20-3-22\linux2.assets\1584866287212.png)]**

2)编辑dvd.repo文件

[root@server1 run]# vim /etc/yum.repos.d/dvd.repo

内容为:

[dvd]
name=rhel7.6
baseurl=file:///run/media/feng/RHEL-7.6\ Server.x86_64
enabled=1
gpgcheck=0
保存退出
尝试安装 Nginx

在Linux下,下载 Nginx,下载地址:http://nginx.org/download/nginx-1.6.2.tar.gz

  1. 将下载好的文件包放入/usr/src下 ,然后查看一下
[root@server1 ~]# mv /home/feng/Downloads/nginx-1.6.2.tar.gz /usr/src/nginx-1.6.2.tar.gz
[root@server1 ~]# cd /usr/src
[root@server1 src]# ls
debug  kernels  nginx-1.6.2.tar.gz
  1. 解压安装包
[root@server1 src]# tar zxvf nginx-1.6.2.tar.gz 
  1. 进入安装目录
[root@server1 src]# ls
debug  kernels  nginx-1.6.2  nginx-1.6.2.tar.gz
[root@server1 src]# cd nginx-1.6.2/
  1. ./configure是源代码安装的第一步,主要的作用是对即将安装的软件进行配置,检查当前的环境是否满足要安装软件的依赖关系
[root@server1 nginx-1.6.2]# ./configure
checking for OS
 + Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found
  1. 安装缺少的编译器
[root@server1 nginx-1.6.2]# yum install -y gcc
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
dvd                                                                       | 4.3 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package gcc.x86_64 0:4.8.5-36.el7 will be installed
.....
Complete!
  1. 再./configure检查
[root@server1 nginx-1.6.2]# ./configure
......
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
  1. 针对上面错误进行解决
[root@server1 nginx-1.6.2]# yum list pcre-devel    列一下相关的
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Available Packages
pcre-devel.i686                                   8.32-17.el7                                 dvd
pcre-devel.x86_64                                 8.32-17.el7                                 dvd

进行安装

[root@server1 nginx-1.6.2]# yum install -y pcre-devel
.....
Complete!
  1. 再./configure检查
[root@server1 nginx-1.6.2]# ./configure
......
./configure: error: the HTTP gzip module requires the zlib library.
  1. 解决上面错误
[root@server1 nginx-1.6.2]# yum install -y zlib-devel

....
Complete!
  1. ./configure 再次检查

没错,检查通过~

进行参数指定,指定安装路径;默认编译时没有增加https模块的

[root@server1 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module

....
....
出错:(因为增加了https模块,Linux是通过OpenSSl实现的)
./configure: error: SSL modules require the OpenSSL library.

解决:

[root@server1 nginx-1.6.2]# yum install -y openssl-devel

....
Complete!

再次检测
[root@server1 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module

不报错
  1. 安装第二步,编译!make
[root@server1 nginx-1.6.2]# make 

.....
make[1]: Entering directory `/usr/src/nginx-1.6.2'
......
make[1]: Leaving directory `/usr/src/nginx-1.6.2'
没错,
  1. 接着make install
[root@server1 nginx-1.6.2]# make install

.........
make[1]: Leaving directory `/usr/src/nginx-1.6.2'

安装完了~,查看下

[root@server1 local]# ls
bin  etc  games  include  lib  lib64  libexec  nginx  sbin  share  src
oot@server1 local]# cd nginx/
[root@server1 nginx]# ls
conf  html  logs  sbin
[root@server1 nginx]# pwd
/usr/local/nginx

[root@server1 nginx]# du -sh .   查看大小
4.9M	.
  1. 启动Nginx!
[root@server1 conf]# cd ..
[root@server1 nginx]# ls
conf  html  logs  sbin
[root@server1 nginx]# cd sbin/
[root@server1 sbin]# ls
nginx
[root@server1 sbin]# ./nginx -t  运行前测试有没有语法错误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@server1 sbin]# ./nginx    运行
[root@server1 sbin]# netstat -antlp   查看占用的端口
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
.....
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      22051/nginx: mast
......

打开浏览器可以测试访问一下:

有个防火墙问题,使用Windows下浏览器访问不到:

在linux终端输入:
vim /etc/sysconfig/iptables
进入编辑界面 ,要增加
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

 sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

按 :wq! 退出并保存。 在使用 /etc/init.d/iptables restart**
**重启防火墙配置生效即可。

windows浏览器访问:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-i58KueWi-1584951250687)(D:\environment\45.20-3-22\linux2.assets\1584888438728.png)]

从容停止进程:

[root@server1 sbin]# ps -ef|grep nginx 
root       9417      1  0 17:40 ?        00:00:00 nginx: master process ./nginx
nobody     9418   9417  0 17:40 ?        00:00:00 nginx: worker process
root       9486   9296  0 17:46 pts/0    00:00:00 grep --color=auto nginx
[root@server1 sbin]# kill -QUIT 9417
[root@server1 sbin]# ps -ef|grep nginx 
root       9502   9296  0 17:47 pts/0    00:00:00 grep --color=auto nginx

服务管理

**[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QhluGKKS-1584951250689)(D:\environment\45.20-3-22\linux2.assets\1584842858307.png)]**

systemctl 管理服务

一般来说,服务的启动有两个阶段,一 个是"开机的时候设定要不要启动这个服务", 以及"你现在要不要启动这个服务"

假如我想立马取消一个服务,不要用kill如何处理?

[root@study ~]# systemctl [command] [unit] 
command 主要有: 
start:立刻启动后面接的 unit 
stop:立刻关闭后面接的 unit
restart :立刻关闭后启动后面接的 unit,亦即执行 stop 再 start 的意思 
reload :不关闭后面接的 unit 的情况下,重载配置文件,让设定生效 
enable :设定下次开机时,后面接的 unit 会被启动 
disable :设定下次开机时,后面接的 unit 不会被启动 
status :目前后面接的这个 unit 的状态,会列出有没有正在执行、开机预设执行否、登录等信息等! 
is-active :目前有没有正在运作中 
is-enable :开机时有没有预设要启用这个 unit

实验:

  1. 康康目前atd这个服务的状态为什么样?
[root@server1 ~]# systemctl status atd.service
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-03-23 17:37:13 CST; 1h 16min ago
...
  1. 正常关闭这个atd服务
[root@server1 ~]# systemctl stop atd.service
[root@server1 ~]# systemctl status atd.service
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Mon 2020-03-23 18:55:33 CST; 2s ago
.....

变成了inactive (dead),因为在Loaded处是enabled,所以未来重新启动后,这个服务会启动;这就是现在的状态和开机时预设状态的差别。

练习:

没有打印机安装在服务器上,目前也没有网络打印机,因此我想要将 cups 服务整个关闭

  1. 先看看 cups 的服务是开还是关?
[root@server1 ~]# systemctl status cups.service 
● cups.service - CUPS Printing Service
   Loaded: loaded (/usr/lib/systemd/system/cups.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-03-23 17:37:13 CST; 1h 23min ago
....
  1. 直接关闭,同时确认没有启动
[root@server1 ~]# systemctl stop cups.service
Warning: Stopping cups.service, but it can still be activated by:
  cups.socket
  cups.path
[root@server1 ~]# systemctl disable cups.service
Removed symlink /etc/systemd/system/multi-user.target.wants/cups.path.
Removed symlink /etc/systemd/system/multi-user.target.wants/cups.service.
Removed symlink /etc/systemd/system/sockets.target.wants/cups.socket.
Removed symlink /etc/systemd/system/printer.target.wants/cups.service.

取消掉4个连结档,也就是说,这4个文件可能是有相依性的问题
  1. 查看
[root@server1 ~]# netstat -tlunp | grep cups
[root@server1 ~]# 
现在应该不会出现任何数据!因为根本没有 cups 的任务在执行当中
  1. 尝试启动 cups.socket 监听客户端的需求
[root@server1 ~]# systemctl start cups.socket
[root@server1 ~]# systemctl status cups.service cups.socket cups.path

● cups.service - CUPS Printing Service
   Loaded: loaded (/usr/lib/systemd/system/cups.service; disabled; vendor preset: enabled)
   Active: inactive (dead) since Mon 2020-03-23 19:06:52 CST; 19s ago
 Main PID: 11063 (code=exited, status=0/SUCCESS)

Mar 23 19:06:15 server1 systemd[1]: Started CUPS Printing Service.
Mar 23 19:06:52 server1 systemd[1]: Stopping CUPS Printing Service...
Mar 23 19:06:52 server1 systemd[1]: Stopped CUPS Printing Service.

● cups.socket - CUPS Printing Service Sockets
   Loaded: loaded (/usr/lib/systemd/system/cups.socket; disabled; vendor preset: enabled)
   Active: active (listening) since Mon 2020-03-23 17:37:10 CST; 1h 30min ago
   Listen: /var/run/cups/cups.sock (Stream)

Mar 23 17:37:10 server1 systemd[1]: Listening on CUPS Printing Service Sockets.

● cups.path - CUPS Printer Service Spool
   Loaded: loaded (/usr/lib/systemd/system/cups.path; disabled; vendor preset: enabled)
   Active: active (waiting) since Mon 2020-03-23 17:37:10 CST; 1h 30min ago

  1. 尝试使用 lp 这个指令来打印看看
[root@server1 ~]# echo "testing" | lp
lp: Error - no default destination available.

[root@server1 ~]# systemctl status cups.service
● cups.service - CUPS Printing Service
   Loaded: loaded (/usr/lib/systemd/system/cups.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-03-23 19:08:32 CST; 54s ago
.....

[root@server1 ~]# netstat -tlunp | grep cups
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      11162/cupsd         
tcp6       0      0 ::1:631                 :::*                    LISTEN      11162/cupsd      

cups 自动被启动了

很多服务彼此之间是有相依性的!cups 是一种打印服务,这个打印服务会启用 port 631 来提供网络打印机的打印功能。 但是其实我们无须一直启动 631端口吧?因此,多了一个名为 cups.socket 的服务,这个服务可以在『用户有需要打印时,才会主动唤醒 cups.service 』的意思!

强迫服务注销

上面的实验,比较正规的作法是,要关闭 cups.service 时,连同其他两个会唤醒 service 的 cups.socket 与 cups.path 通通关闭,那就没事了

透过 mask 的方式来将这个服务注销

实验:

  1. 保持刚刚的状态,关闭 cups.service,启动 cups.socket,然后注销 cups.servcie
[root@server1 ~]# systemctl stop cups.service 

[root@server1 ~]# systemctl mask cups.service 
Created symlink from /etc/systemd/system/cups.service to /dev/null.
这个 mask 注销的动作,只是让启动的脚本变成空的装置

[root@server1 ~]# systemctl status cups.service 
● cups.service
   Loaded: masked (/dev/null; bad)
   Active: inactive (dead) since Mon 2020-03-23 19:14:19 CST; 57s ago

[root@server1 ~]# systemctl start cups.service
Failed to start cups.service: Unit is masked.
无法唤醒了
  1. 取消注销,unmask
[root@server1 ~]# systemctl unmask cups.service 
Removed symlink /etc/systemd/system/cups.service.

[root@server1 ~]# systemctl status cups.service 
● cups.service - CUPS Printing Service
   Loaded: loaded (/usr/lib/systemd/system/cups.service; disabled; vendor preset: enabled)
   Active: inactive (dead) since Mon 2020-03-23 19:14:19 CST; 3min 4s ago
systemctl 管理不同的操作环境

列出跟操作界面比较有关的 target 项目

[root@server1 ~]# systemctl list-units --type=target --all
  UNIT                   LOAD      ACTIVE   SUB    DESCRIPTION
..............

而跟操作界面相关性比较高的 target 主要有:

  • graphical.target:就是文字加上图形界面,这个项目已经包含了底下的 multi-user.target 项目
  • multi-user.target:纯文本模式
  • rescue.target:在无法使用 root 登入的情况下,systemd 在开机时会多加一个额外的暂时系统,与你原本的 系统无关
  • emergency.target:紧急处理系统的错误,还是需要使用 root 登入的情况
  • shutdown.target:就是关机的流程
  • getty.target:可以设定你需要几个 tty 之类的

实验:

  1. 默认是图形界面,先观察是否真为图形模式,再将默认模式转为文字界面
[root@server1 ~]# systemctl get-default 
graphical.target

[root@server1 ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
  1. 不重新启动的情况下,将目前的操作环境改为纯文本模式,关掉图形界面
[root@server1 ~]# systemctl isolate multi-user.target
  1. 要重新取得图形界面
[root@server1 ~]# systemctl isolate graphical.target

其他模式切换:

[root@study ~]# systemctl poweroff 系统关机 
[root@study ~]# systemctl reboot 重新启动 
[root@study ~]# systemctl suspend 进入暂停模式 
[root@study ~]# systemctl hibernate 进入休眠模式 
[root@study ~]# systemctl rescue 强制进入救援模式 
[root@study ~]# systemctl emergency 强制进入紧急救援模式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值