目录
一、Linux程序与进程
1、程序,进程,线程的概念
-
程序:是一组指令及参数的集合,按照既定的逻辑控制计算机运行用来完成特定任务,是静态的;
-
进程:是运行着的程序,是操作系统执行的基本单位,是程序运行的过程, 是动态的,是有生命周期及运行状态的。是操作系统分配内存、CPU时间片等资源的基本单位。
-
线程:是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位。
2、程序和进程的区别
-
程序是静态的,它只是一组指令的集合,不具有任何的运行意义。而进程是程序运行的动态过程;
-
进程和程序并不是一一对应的关系,相同的程序运行在不同的数据集上就是不同的进程;
-
进程还具有并发性和交往性,而程序却是封闭的。
3、进程和线程的区别
-
一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发拥有多个线程,而一个线程同时只能被一个进程所拥有;
-
线程不能单独执行,但是每一个线程都有程序的入口、执行序列以及程序出口,它必须组成进程才能被执行。
二、Linux进程基础(生命周期)
1、进程生命周期
-
父进程复制自己的地址空间(fork)创建一个新的(子)进程结构。每个新进程分配一个唯一的进程 ID(PID),PID和父进程ID(PPID)是子进程环境的元素,任何进程都可以创建子进程。
-
所有进程都是第一个系统进程的后代:在centos6中,第一个系统进程是init,而在centos7中,第一个系统进程是systemd。
2、父子进程的关系
-
子进程由父进程产生,在linux系统中,使用系统调用fork创建进程。fork复制的内容包括父进程的数据和堆栈段以及父进程的进程环境。子进程继承父进程的安全性身份、过去和当前的文件描述符、端口和资源特权、环境变量,以及程序代码。
-
运行过程:子进程运行时父进程休眠。当子进程完成时发出(exit)信号请求,在退出时,子进程已经关闭或丢弃了其资源环境后,剩余释放不掉的资源称之为僵尸进程。父进程在子进程退出时收到信号而被唤醒,清理剩余的结构,然后继续执行其自己的程序代码。
三、程序管理
1、学习目标
-
掌握Linux系统安装软件的方法
-
掌握Linux系统软件管理命令
-
熟练配置yum源
-
掌握systemd管理机制
2、常见的软件包类型
1)rpm包:.rpm为后缀,红帽系列操作系统(RedHat,CentOS,OpenSUSE)主要的软件包封装格式,已经做好默认设置,如安装路径,配置文件存放路径,需要手动的解决依赖关系。(相关命令:rpm,yum,dnf)
2)deb包:.deb为后缀,debian系列操作系统(debian,Ubuntu,kali)主要的软件包封装格式,已经做好默认设置,如安装路径,配置文件存放路径等,需要手动的解决依赖关系。(相关命令:dpkg,apt[-get])
依赖关系:安装软件包所需的基础环境
3)二进制包:一般以.bin为后缀,直接被计算机执行安装。
4)源码包:一般C语言(高级语言中最底层的语言,一般结合汇编进行驱动编写)编写,安装前需要编译为二进制包;配置【如,安装路径,配置文件路径,运行用户,运行组,功能模块】--->编译--->安装;文件比较集中,便于后期进行维护与管理。
3、安装方法
-
使用独立的rpm包安装
需要下载对应的rpm包,一般在centos系统镜像中存在大量的rpm包
-
rpm包的命名方法
zip-0-11.el7.x86_64.rpm
软件名.版本号.安装平台.安装架构平台的位数(32位,x86或者64位,x86_64).rpm
-
rpm命令使用方法
已安装软件包查看选项
选项 | 作用 | 使用方式 | |
-q | 查看指定rpm包是否安装; | rpm -q 软件名 | |
-qa 【all】 | 查看系统中所有已安装的软件包; | rpm -qa [| grep 软件名] | |
-qi 【infomation】 | 查看已安装软件包的开发信息; | rpm -qi 软件名 | |
-ql 【list】 | 查看已安装软件包的安装路径及生成的文件; | rpm -ql 软件名 | |
-qf 【find】 | 查看命令由哪个软件包安装; | rpm -qf 命令的绝对路径 | |
-qc | 查看已安装软件的配置文件; | rpm -qc 软件名 | |
-qd | 查看已安装软件的帮助文档信息; | rpm -qd 软件名 |
未安装软件包查看选项
选项 | 作用 | 使用方式 |
-qpi | 查看未安装的软件包开发信息; | rpm -pqi 软件包名称(全格式) |
-qpl | 查看未安装软件包的安装路径及生成文件; | rpm -pql 软件包名称(全格式) |
安装、升级、卸载选项
选项 | 作用 |
-i 【install】 | 安装软件包 |
-v:【verify】 | 详细显示安装过程 |
-h:【human】 | 人性化显示 |
-U:【update】 | 升级软件包 |
-e:【exclude】 | 卸载已安装软件包 |
--nodeps | 忽略依赖关系的进行卸载、升级或安装 |
特殊安装
#将所有相关软件包放到同一目录下
rpm -ivh *
4、yum(dnf)安装
可以自动解决依赖关系
-
yum相关配置文件
yum源配置文件
指定yum程序运行时查找软件包的存储路径
/etc/yum.repos.d
yum程序配置文件
yum程序运行时的运行配置
/etc/dnf/dnf.conf
缓存保存路径
/var/cache/dnf
-
yum源类型
本地yum源
利用本地存在的软件包路径
file://localPath(绝对路径)【路径下要包含“repodata(包含rpm包的元数据)”目录】
网络yum源
利用网络存储的软件包路径
http://域名/Path #不常用
https://域名/Path
ftp://域名/Path #不常用
-
配置本地yum源
cd /etc/yum.repos.d
[root@localhost yum.repos.d]# rm -rf *
vim local.repo
[local]
name=local #描述信息
baseurl=file:///mnt/AppStream # 指定软件包及其元数据存储路径
enabled=1 #是否使用该源,0表示不使用,1表示使用
gpgcheck=0 #(一般使用不验证的方式) [gpgcheck=1]
gpgkey=keyPath
- #先清除一下yum包缓存
yum clean all
- 导入本地源到本地仓库
yum makecache
- 挂载镜像
mount /dev/sr0 /mnt
5、yum命令语法
yum [options] command [package ...]
-
子命令(command)
install [软件包名] [--downloadonly] #安装【只下载】
remove [软件包名] #卸载
list #查看yum源中的软件包列表
update [软件包名] #更新所有软件包
yum clean all #清空元数据缓存
yum makecache #生成元数据缓存
-
命令选项
-y:非交互
交互式安装:yum install 软件名
Is this ok?
y #安装
n #不下载不安装
非交互式安装:yum -y install 软件名
6、源码包(编译)安装
##配置:cd 源码包的解压路径;./configure --prefix=安装路径(/usr/local/nginx)
[root@localhost nginx-1.28.0]# ./configure --prefix=/usr/local/nginx
checking for OS
+ Linux 5.14.0-427.13.1.el9_4.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 11.5.0 20240719 (Red Hat 11.5.0-5) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... found
checking for eventfd() ... found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for prctl(PR_SET_KEEPCAPS) ... found
checking for capabilities ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... found
checking for IP_RECVDSTADDR ... not found
checking for IP_SENDSRCADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for IP_MTU_DISCOVER ... found
checking for IPV6_MTU_DISCOVER ... found
checking for IP_DONTFRAG ... not found
checking for IPV6_DONTFRAG ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for strerrordesc_np() ... found
checking for localtime_r() ... found
checking for clock_gettime(CLOCK_MONOTONIC) ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found
checking for PCRE library in /opt/homebrew/ ... not found./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
#需要依赖gcc和make环境
#解决依赖
#yum install -y pcre-devel或者untils
#yum install -y zlib-devel
[root@localhost nginx-1.28.0]# yum install -y gcc make
Rocky Linux 9 - BaseOS 1.2 MB/s | 2.3 MB 00:02
Rocky Linux 9 - AppStream 3.1 MB/s | 8.4 MB 00:02
Rocky Linux 9 - Extras 10 kB/s | 16 kB 00:01
依赖关系解决。
=========================================================================
软件包 架构 版本 仓库 大小
=========================================================================
安装:
gcc x86_64 11.5.0-5.el9_5 appstream 32 M
make x86_64 1:4.3-8.el9 baseos 529 k
升级:
cpp x86_64 11.5.0-5.el9_5 appstream 11 M
libgcc x86_64 11.5.0-5.el9_5 baseos 84 k
libgomp x86_64 11.5.0-5.el9_5 baseos 260 k
安装依赖关系:
glibc-devel x86_64 2.34-100.el9 local 47 k
glibc-headers x86_64 2.34-100.el9 local 447 k
kernel-headers x86_64 5.14.0-503.40.1.el9_5 appstream 3.5 M
libxcrypt-devel x86_64 4.4.18-3.el9 local 28 k事务概要
=========================================================================
安装 6 软件包
升级 3 软件包总计:48 M
总下载:47 M
下载软件包:
(1/6): make-4.3-8.el9.x86_64.rpm 635 kB/s | 529 kB 00:00
(2/6): kernel-headers-5.14.0-503.40.1.el 3.3 MB/s | 3.5 MB 00:01
(3/6): libgcc-11.5.0-5.el9_5.x86_64.rpm 341 kB/s | 84 kB 00:00
(4/6): libgomp-11.5.0-5.el9_5.x86_64.rpm 474 kB/s | 260 kB 00:00
(5/6): cpp-11.5.0-5.el9_5.x86_64.rpm 7.9 MB/s | 11 MB 00:01
(6/6): gcc-11.5.0-5.el9_5.x86_64.rpm 8.1 MB/s | 32 MB 00:03
-------------------------------------------------------------------------
总计 7.8 MB/s | 47 MB 00:06
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
准备中 : 1/1
升级 : cpp-11.5.0-5.el9_5.x86_64 1/12
升级 : libgcc-11.5.0-5.el9_5.x86_64 2/12
运行脚本: libgcc-11.5.0-5.el9_5.x86_64 2/12
升级 : libgomp-11.5.0-5.el9_5.x86_64 3/12
安装 : kernel-headers-5.14.0-503.40.1.el9_5.x86_64 4/12
安装 : make-1:4.3-8.el9.x86_64 5/12
安装 : glibc-headers-2.34-100.el9.x86_64 6/12
安装 : glibc-devel-2.34-100.el9.x86_64 7/12
安装 : libxcrypt-devel-4.4.18-3.el9.x86_64 8/12
安装 : gcc-11.5.0-5.el9_5.x86_64 9/12
清理 : cpp-11.4.1-3.el9.x86_64 10/12
清理 : libgcc-11.4.1-3.el9.x86_64 11/12
运行脚本: libgcc-11.4.1-3.el9.x86_64 11/12
清理 : libgomp-11.4.1-3.el9.x86_64 12/12
运行脚本: libgomp-11.4.1-3.el9.x86_64 12/12
验证 : libxcrypt-devel-4.4.18-3.el9.x86_64 1/12
验证 : glibc-headers-2.34-100.el9.x86_64 2/12
验证 : glibc-devel-2.34-100.el9.x86_64 3/12
验证 : make-1:4.3-8.el9.x86_64 4/12
验证 : gcc-11.5.0-5.el9_5.x86_64 5/12
验证 : kernel-headers-5.14.0-503.40.1.el9_5.x86_64 6/12
验证 : libgomp-11.5.0-5.el9_5.x86_64 7/12
验证 : libgomp-11.4.1-3.el9.x86_64 8/12
验证 : libgcc-11.5.0-5.el9_5.x86_64 9/12
验证 : libgcc-11.4.1-3.el9.x86_64 10/12
验证 : cpp-11.5.0-5.el9_5.x86_64 11/12
验证 : cpp-11.4.1-3.el9.x86_64 12/12已升级:
cpp-11.5.0-5.el9_5.x86_64 libgcc-11.5.0-5.el9_5.x86_64
libgomp-11.5.0-5.el9_5.x86_64
已安装:
gcc-11.5.0-5.el9_5.x86_64
glibc-devel-2.34-100.el9.x86_64
glibc-headers-2.34-100.el9.x86_64
kernel-headers-5.14.0-503.40.1.el9_5.x86_64
libxcrypt-devel-4.4.18-3.el9.x86_64
make-1:4.3-8.el9.x86_64完毕!
##编译:make
[root@localhost nginx-1.28.0]# make
make -f objs/Makefile
make[1]: 进入目录“/usr/local/nginx-1.28.0”
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs \
-o objs/src/core/ngx_log.o \
src/core/ngx_log.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs \
-o objs/src/core/ngx_palloc.o \
src/core/ngx_palloc.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs \
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs \
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs \
-o objs/src/core/ngx_rwlock.o \
src/core/ngx_rwlock.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs \
-o objs/src/core/ngx_cpuinfo.o \
src/core/ngx_cpuinfo.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs \
-o objs/src/core/ngx_conf_file.o \
src/core/ngx_conf_file.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs \
-o objs/src/core/ngx_module.o \
src/core/ngx_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs \
-o objs/src/core/ngx_resolver.o \
src/core/ngx_resolver.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I
src/http/ngx_http_write_filter_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/ngx_http_header_filter_module.o \
src/http/ngx_http_header_filter_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_chunked_filter_module.o \
src/http/modules/ngx_http_chunked_filter_module.c
cc -c -pipe -O -W -Wall
src/http/ngx_http_postpone_filter_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs -I src/http -I src/http/modules \
src/http/modules \
-o objs/src/http/modu
-o objs/src/http/modules/ngx_http_split_clients_module.o \
src/http/modules/ngx_http_split_clients_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs -I src/http -I src/http/modules \
src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_fastcgi_module.o \
src/http/modules/ngx_http_fastcgi_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_uwsgi_module.o \
src/http/modules/ngx_http_uwsgi_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_scgi_module.o \
src/http/modules/ngx_http_scgi_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_upstream_ip_hash_module.o \
src/http/modules/ngx_http_upstream_ip_hash_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/http/modules/ngx_http_upstream_least_conn_module.o \
src/http/modules/ngx_http_upstream_least_conn_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/event/quic -I src/os/unix -I objs -I src/http -I src/http/modules \
-o objs/src/ht
objs/src/http/ngx_http_upstream.o \
objs/src/http/ngx_http_upstream_round_robin.o \
objs/src/http/ngx_http_file_cache.o \
objs/src/http/ngx_http_write_filter_module.o \
objs/src/http/ngx_http_header_filter_module.o \
objs/src/http/modules/ngx_http_chunked_filter_module.o \
objs/src/http/modules/ngx_http_range_filter_module.o \
objs/src/http/modules/ngx_http_gzip_filter_module.o \
objs/src/http/ngx_http_postpone_filter_module.o \
objs/src/http/modules/ngx_http_mirror_module.o \
objs/src/http/modules/ngx_http_try_files_module.o \
objs/src/http/modules/ngx_http_auth_basic_module.o \
objs/src/http/modules/ngx_http_access_module.o \
objs/src/http/modules/ngx_http_limit_conn_module.o \
objs/src/http/modules/ngx_http_limit_req_module.o \
objs/src/http/modules/ngx_http_geo_module.o \
objs/src/http/modules/ngx_http_map_module.o \
objs/src/http/modules/ngx_http_split_clients_module.o \
objs/src/http/modules/ngx_http_referer_module.o \
objs/src/http/modules/ngx_http_rewrite_module.o \
objs/src/http/modules/ngx_http_proxy_module.o \
objs/src/http/modules/ngx_http_fastcgi_module.o \
objs/src/http/modules/ngx_http_uwsgi_module.o \
objs/src/http/modules/ngx_http_scgi_module.o \
objs/src/http/modules/ngx_http_memcached_module.o \
objs/src/http/modules/ngx_http_empty_gif_module.o \
objs/src/http/modules/ngx_http_browser_module.o \
objs/src/http/modules/ngx_http_upstream_hash_module.o \
objs/src/http/modules/ngx_http_upstream_ip_hash_module.o \
objs/src/http/modules/ngx_http_upstream_least_conn_module.o \
objs/src/http/modules/ngx_http_upstream_random_module.o \
objs/src/http/modules/ngx_http_upstream_keepalive_module.o \
objs/src/http/modules/ngx_http_upstream_zone_module.o \
objs/ngx_modules.o \
-lcrypt -lpcre -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
< man/nginx.8 > objs/nginx.8
make[1]: 离开目录“/usr/local/nginx-1.28.0”
#安装:make install
[root@localhost nginx-1.28.0]# make install
make -f objs/Makefile install
make[1]: 进入目录“/usr/local/nginx-1.28.0”
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
|| mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' \
|| mv '/usr/local/nginx/sbin/nginx' \
'/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf' \
|| mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
test -f '/usr/local/nginx/conf/mime.types' \
|| cp conf/mime.types '/usr/local/nginx/conf'
cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
test -f '/usr/local/nginx/conf/fastcgi_params' \
|| cp conf/fastcgi_params '/usr/local/nginx/conf'
cp conf/fastcgi_params \
'/usr/local/nginx/conf/fastcgi_params.default'
test -f '/usr/local/nginx/conf/fastcgi.conf' \
|| cp conf/fastcgi.conf '/usr/local/nginx/conf'
cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
test -f '/usr/local/nginx/conf/uwsgi_params' \
|| cp conf/uwsgi_params '/usr/local/nginx/conf'
cp conf/uwsgi_params \
'/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' \
|| cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params \
'/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
|| cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/usr/local/nginx-1.28.0”
命令合并
./configure --prefix=/usr/local/nginx && make && make install
验证
cd /usr/local/nginx-1.28.0
###命令优化####
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
注意
编译安装完成的软件默认不被systemd管理
四、Linux操作系统启动流程详解
1、概述
Linux启动流程是从按下电源键到用户登录的完整过程,分为传统BIOS+MBR和现代UEFI+GPT两种模式。主流系统(如CentOS 7+/Ubuntu 18.04+)通常使用 UEFI + GRUB2 + systemd 组合。
2、启动流程核心阶段
1. 电源与固件阶段
-
UEFI/BIOS初始化
-
硬件自检(POST, Power-On Self-Test)
-
UEFI特点:图形化界面、支持GPT分区、独立于OS的Shell环境
-
定位启动设备:读取NVRAM中的启动项(
efibootmgr
命令可管理)
-
2. 引导加载程序(Bootloader)
-
GRUB2 核心作用
-
配置文件路径:
/boot/grub2/grub.cfg
(由grub2-mkconfig
生成) -
关键配置参数:
set root=(hd0,gpt1) # 指定/boot所在分区 linux /vmlinuz-5.4.0 root=/dev/sda2 ro # 内核路径及挂载参数 initrd /initramfs-5.4.0.img # 初始化内存文件系统
-
-
用户交互
-
按
e
临时编辑启动参数(如单用户模式systemd.unit=rescue.target
) -
按
c
进入GRUB命令行
-
3. 内核初始化
-
内核解压与初始化
-
加载
vmlinuz
:压缩的内核镜像 -
initramfs
作用:-
包含根文件系统挂载前的驱动(如LVM、RAID)
-
提供
dracut
工具集(可通过dmesg | grep initramfs
查看日志)
-
-
挂载根文件系统(
/
)为只读模式
-
4. systemd 初始化进程
-
首个用户态进程
-
PID=1,替代传统
init(init是centos6及以下版本的父进程)
-
核心任务:
-
解析
/etc/systemd/system/default.target
(默认运行级别) -
并行启动服务单元(Unit)
-
激活
sysinit.target
(系统初始化)、basic.target
(基础服务)
-
-
-
关键操作命令
systemctl get-default # 查看默认target systemctl set-default multi-user.target # 设置默认为命令行模式 journalctl -b # 查看本次启动日志
5. 用户登录阶段
-
终端登录
-
getty
服务生成终端(/etc/systemd/system/getty.target.wants/
) -
调用
login
验证用户身份
-
-
图形化登录
-
Display Manager(如GDM、LightDM)启动X/Wayland会话
-
3、故障排查与调试技巧
1. 常见启动问题
故障现象 | 可能原因 | 解决方法 |
---|---|---|
GRUB Rescue> 提示符 | 引导分区损坏/配置丢失 | 使用Live CD重建grub |
Kernel panic | 驱动缺失/根文件系统错误 | 检查initramfs是否包含所需驱动 |
systemd启动卡死 | 服务依赖死锁 | 添加systemd.unit=emergency.target 内核参数 |
文件系统只读 | fsck未通过或磁盘错误 | 手动运行fsck /dev/sda1 |
2. 救援模式操作
-
在GRUB界面添加启动参数:
systemd.unit=rescue.target # 单用户模式(需root密码) systemd.unit=emergency.target # 紧急模式(只挂载根文件系统)
-
重新挂载根文件系统为读写:
mount -o remount,rw /
3. 关键文件修复
-
重建GRUB(适用于MBR损坏):
grub2-install /dev/sda # 安装引导到磁盘 grub2-mkconfig -o /boot/grub2/grub.cfg # 生成配置文件
-
重建initramfs(CentOS/RHEL):
dracut -f /boot/initramfs-$(uname -r).img $(uname -r)
4、启动流程示意图(文本版)
[UEFI固件] → 加载ESP分区的GRUB → 载入内核和initramfs →
| |
| v
←←←←←←←←←←←←←←←←← systemd初始化 → 启动target → 用户登录
五、systemd管理机制
init进程是所有进程的父进程,systemd实际上是一种管理init的软件。只能管理rpm安装的
units存储路径
/usr/lib/systemd/system
-
基于unit概念实现,常见的unit类型
单元类型 | 扩展名 | 说明 |
---|---|---|
Service | .service | 描述一个系统服务 |
Target | .target | 描述一组systemd的单元 |
Socket | .socket | 描述一个进程间通信的套接字 |
Device | .device | 描述一个内核识别的设备文件 |
Mount | .mount | 描述一个文件系统的挂载点 |
Automount | .automount | 描述一个文件系统的自动挂载点 |
Swap | .swap | 描述一个内存交换设备或交换文件 |
Path | .path | 描述一个文件系统中文件或目录 |
Timer | .timer | 描述一个定时器(用于实现类似cron的调度任务) |
Snapshot | .snapshot | 用于保存一个systemd的状态 |
Scope | .scope | 使用systemd的总线接口以编程的方式创建外部进程 |
Slice | .slice | 描述居于Cgroup的一组通过层次组织的管理系统进程 |
1、运行级别
-
运行级别与target对应关系
运行级别 | Systemd的target | 说明 |
---|---|---|
0 | poweroff.target | 关机状态,使用该级别时将会关闭主机 |
1 | rescue.target | 单用户模式,不需要密码验证即可登录系统,多用于系统维护 |
2 | multi-user.target | 用户定义/域特定运行级别。默认等同于3 |
3 | multi-user.target | 字符界面的完整多用户模式,大多数服务器主机运行在此级别 |
4 | multi-user.target | 用户定义/域特定运行级别。默认等同于3 |
5 | graphical.target | 图形界面的多用户模式,提供了图形桌面操作环境 |
6 | reboot.target | 重新启动,使用该级别时将会重启主机 |
查看运行级别
runlevel
[root@localhost local]# runlevel
N 5 #N|运行级别数字 当前运行级别数字
切换运行级别
init 运行级别数字
[root@localhost local]# init 3
[root@localhost local]# runlevel
5 3
查看默认运行级别
systemctl get-default
设置默认运行级别
systemctl set-default 运行级别target
[root@localhost local]# systemctl get-default
graphical.target
2、systemctl命令的使用
服务控制类型
start:启动 stop:停止 restart:重启;会停止服务 reload:重载;不停止服务,加载配置文件;优雅的重启 status:查看状态
Active: active (running) #正常运行
Active: inactive (dead) #正常停止
Active: failed (Result:exit-code) #错误退出
enable:开机自启动
is-enabled:查看是否开机自启动
[root@localhost ~]# systemctl is-enabled httpd
disabled #关闭开机自启动
[root@localhost ~]# systemctl is-enabled httpd
enabled #开启开机自启动
disable:开机不进行自启动
--now 配合enable与disable使用;在进行开机自启或不自启的同时进行打开或关闭指定服务
-
选项
-t
指定unit类型
Unit Commands
list-units
列出所有unit,一般与-t结合
案例
systemctl list-units -t service
systemctl daemon-reload #当改变unit的脚本文件后,需要进行重新识别