当前位置: 首页 > news >正文

Linux程序与进程

目录

一、Linux程序与进程

1、程序,进程,线程的概念

2、程序和进程的区别

3、进程和线程的区别

二、Linux进程基础(生命周期)

1、进程生命周期

2、父子进程的关系

三、程序管理

1、课程目标

2、常见的软件包类型

3、安装方法

使用独立的rpm包安装

rpm包的命名方法

rpm命令使用方法

4、yum(dnf)安装

yum相关配置文件

yum源类型

配置本地yum源

5、yum命令语法

子命令(command)

命令选项

6.建立源库且配置本地源实验

7、源码包(编译)安装

案例

1. 检查基础编译工具

2. 安装编译依赖

3. 配置编译选项

5. 编译并安装

问题总结:

一、依赖缺失问题

1. 缺少编译器或构建工具

2. 缺少 PCRE 库

3. 缺少 zlib 库

4. 缺少 OpenSSL 库

二、配置参数问题

1. 模块选择错误

2. 路径冲突

三、编译过程错误

1. 权限不足

2. 代码编译失败

四、安装后启动问题

1. 配置文件语法错误

2. 端口权限问题

3. 进程冲突

五、其他常见问题

1. 系统兼容性问题

2. 模块编译失败

8、二进制安装

案例

四、Linux操作系统启动流程详解

1、概述

2、启动流程核心阶段

1. 电源与固件阶段

2. 引导加载程序(Bootloader)

3. 内核初始化

4. systemd 初始化进程

5. 用户登录阶段

3、故障排查与调试技巧

1. 常见启动问题

五、systemd管理机制

1、运行级别

运行级别与target对应关系

2、systemctl命令的使用

选项


一、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

  1. 软件名称

  • zip:表示这是用于创建和管理 ZIP 压缩文件的工具软件,适用于 Linux 系统。

  1. 版本信息

  • 0-11:版本号(主版本号 0,次版本号 11),不同发行版可能会对上游版本进行二次打包,此处版本号可能包含发行版的修改版本信息。

  1. 系统平台

  • el7:表示适用于 CentOS/RHEL 7 操作系统(elEnterprise Linux 的缩写)。

  • x86_64:架构为 64 位 x86 处理器(即常见的 64 位 PC 服务器 / 工作站)。

  1. 文件类型

  • .rpm:Red Hat 系 Linux 系统的软件包格式,可通过 rpmyum 工具安装

软件名.版本号.安装平台.安装架构平台的位数(32位,x86或者64位,x86_64).rpm

软件包名称:如 httpd-2.4.53-1.el9.x86_64.rpm(完整文件名)

  • “httpd” 表明该软件包与 Apache HTTP 服务器相关,“httpd” 是 Apache HTTP 服务器在这些系统中的常见服务名。

  • “2.4.53” 代表软件版本号,表明这是 Apache HTTP 服务器的 2.4.53 版本,版本号能反映软件的功能特性及更新情况。

  • “1” 是软件包的发行号,用于标识同一版本软件包的不同发行阶段,有助于区分修复了某些问题或有轻微调整的同一版本软件包。

  • “el9” 指该软件包适用于基于 Red Hat Enterprise Linux 9(或其衍生版本)的系统,“el” 是 “Enterprise Linux” 的缩写。

  • “x86_64” 表示该软件包适用于 64 位的 x86 架构的计算机,表明软件可在对应的硬件平台上安装运行。

  • “.rpm” 是文件扩展名,代表这是一个 RPM 格式的软件包,用于在支持 RPM 的 Linux 系统中方便地进行软件的安装、升级、卸载等操作。

  • rpm命令使用方法

已安装软件包查看选项

-q:查看指定rpm包是否安装;rpm -q 软件名
​
-qa 【all】:查看系统中所有已安装的软件包;#rpm -qa [| grep 软件名]
​
[root@bogon ~]# rpm -qa | wc -l
1278
​
-qi 【infomation】:查看已安装软件包的开发信息:#rpm -qi 软件名
​
-ql  【list】:查看已安装软件包的安装路径及生成的文件;#rpm -ql 软件名
​
-qf 【find】:查看命令由哪个软件包安装;#which 命令字  rpm -qf 命令的绝对路径;常用于:系统中没有对应指令且不知道由哪个软件包安装生成
​
-qc:查看已安装软件的配置文件;rpm -qc 软件名;一般情况下,使用rpm安装的应用程序的配置文件都存储在/etc/[应用程序的名称]
​
-qd:查看已安装软件的帮助文档信息;rpm -qd 软件名

未安装软件包查看选项

-qpi:查看未安装的软件包开发信息;rpm -pqi 软件包名称(全格式)
​
-qpl:查看未安装软件包的安装路径及生成文件;
rpm -pql 软件包名称(全格式)

安装、升级、卸载选项

-i 【install】:安装软件包;rpm -ivh /path/软件包名称(全格式)# i安装单个 RPM 包
rpm -ivh package.rpm     # -v:显示详细信息,-h:显示安装进度(#号)
rpm -ivh /mnt/cdrom/Packages/httpd-2.4.53-1.el9.x86_64.rpm
​
-v:【verify】详细显示安装过程
​
-h:【human】人性化显示
​
-U:【update】升级软件包;rpm -Uvh 软件包名称
rpm -Uvh package.rpm  # 升级或安装软件包
​
-e:【exclude】卸载已安装软件包;
rpm -e package_name    # 删除已安装的软件包(需指定包名,非文件名)
rpm -e --nodeps package_name  # 强制删除(忽略依赖关系,谨慎使用!)
​
--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://域名/Pathftp://域名/Path  #不常用

常见网络yum源

#网易yum源
​
#阿里云yum源tee /etc/yum.repos.d/alicloud.repo << 'EOF'
[alicloud-os]
name=Alibaba Cloud OS
baseurl=https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/
enabled=1
gpgcheck=0
gpgkey=https://mirrors.aliyun.com/rockylinux/RPM-GPG-KEY-rockylinux[alicloud-appstream]
name=Alibaba Cloud AppStream
baseurl=https://mirrors.aliyun.com/rockylinux/8/AppStream/x86_64/os/
enabled=1
gpgcheck=0
gpgkey=https://mirrors.aliyun.com/rockylinux/RPM-GPG-KEY-rockylinux
EOF
​
#epel源
​
yum -y install epel-release

常见开源镜像站

清华大学https://mirrors.tuna.tsinghua.edu.cn/help/centos/网易开源镜像站http://mirrors.16com/阿里云
  • 配置本地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

5、yum命令语法

yum [options] command [package ...]
  • 子命令(command)
install [软件包名] [--downloadonly]
#安装【只下载】
remove [软件包名]
#卸载
list
#查看yum源中的软件包列表
update [软件包名]
#更新所有软件包
yum clean all
#清空元数据缓存
yum makecache 
#生成元数据缓存
  • 命令选项
-y:非交互

*交互式安装:yum install 软件名

6.建立源库且配置本地源实验

[root@bogon ~]# mount /dev/sr0 /mnt/
mount: /mnt: WARNING: source write-protected, mounted read-only.
​
[root@bogon ~]# cd /mnt/AppStream
[root@bogon AppStream]# ls
Packages  repodata
​
[root@bogon AppStream]# cd /etc/yum.repos.d/
[root@bogon yum.repos.d]# ls
rocky-addons.repo  rocky-devel.repo  rocky-extras.repo  rocky.repo
​
[root@bogon yum.repos.d]# mkdir backup
​
[root@bogon yum.repos.d]# mv rock* backup/
[root@bogon yum.repos.d]# ls
backup
​
[root@bogon yum.repos.d]# vim local.repo
[local]
name=local
baseurl=file:///mnt/AppStream
enabled=1
gpgcheck=0
​
[root@bogon yum.repos.d]# vim local.repo
[root@bogon yum.repos.d]# yum clean all
25 个文件已删除
[root@bogon yum.repos.d]# yum makecache
local                                            57 MB/s | 7.9 MB     00:00    
上次元数据过期检查:0:00:03 前,执行于 2025年05月27日 星期二 20时57分22秒。
元数据缓存已建立。
[root@bogon yum.repos.d]# yum search httpd
上次元数据过期检查:0:00:28 前,执行于 2025年05月27日 星期二 20时57分22秒。
============================= 名称 精准匹配:httpd =============================
httpd.x86_64 : Apache HTTP Server
=========================== 名称 和 概况 匹配:httpd ===========================
httpd-core.x86_64 : httpd minimal core
keycloak-httpd-client-install.noarch : Tools to configure Apache HTTPD as: Keycloak client
python3-keycloak-httpd-client-install.noarch : Tools to configure Apache HTTPD: as Keycloak client
rocky-logos-httpd.noarch : Rocky Linux related icons and pictures used by httpd
=============================== 名称 匹配:httpd ===============================
httpd-devel.x86_64 : Development interfaces for the Apache HTTP Server
httpd-filesystem.noarch : The basic directory layout for the Apache HTTP Server
httpd-manual.noarch : Documentation for the Apache HTTP Server
httpd-tools.x86_64 : Tools for use with the Apache HTTP Server
libmicrohttpd.x86_64 : Lightweight library for embedding a webserver in: applications
libmicrohttpd.i686 : Lightweight library for embedding a webserver in: applications
=============================== 概况 匹配:httpd ===============================
mod_auth_mellon.x86_64 : A SAML 2.0 authentication module for the Apache Httpd: Server
mod_dav_svn.x86_64 : Apache httpd module for Subversion server
mod_proxy_cluster.x86_64 : JBoss mod_proxy_cluster for Apache httpd
​
[root@bogon yum.repos.d]# cd /mnt/AppStream
[root@bogon AppStream]# ls
Packages  repodata
​
报错实验:
[root@bogon AppStream]# cd /etc/yum.repos.d/
[root@bogon yum.repos.d]# vim local.repo
[local]
name=local
baseurl=file:///mnt/AppStream1
enabled=1
gpgcheck=0
[root@bogon yum.repos.d]# yum clean all
9 个文件已删除
[root@bogon yum.repos.d]# yum makecache
local                                           0.0  B/s |   0  B     00:00    
Errors during downloading metadata for repository 'local':- Curl error (37): Couldn't read a file:// file for file:///mnt/AppStream1/repodata/repomd.xml [Couldn't open file /mnt/AppStream1/repodata/repomd.xml]
错误:为仓库 'local' 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were triedrep
错误原因:配置时要指向repomd,当路径出现错误时就会报错。
​

7、源码包(编译)安装

需要依赖gcc和make环境(不是所有的)配置:cd 源码包的解压路径;./configure --prefix=安装路径(/usr/local/nginx)编译:make安装:make install

.configure --help(功能存放的位置文件)

相关路径

--prefix=/usr/local        # 指定安装目录(默认 /usr/local)
--exec-prefix=PATH         # 指定可执行文件的安装目录
--bindir=PATH              # 指定二进制文件安装目录(如 /usr/bin)
--sbindir=PATH             # 指定系统管理命令安装目录(如 /usr/sbin)
--libdir=PATH              # 指定库文件安装目录(如 /usr/lib)
--includedir=PATH          # 指定头文件安装目录

  --help                             print this message
​--prefix=PATH                      set installation prefix--sbin-path=PATH                   set nginx binary pathname--modules-path=PATH                set modules path--conf-path=PATH                   set nginx.conf pathname--error-log-path=PATH              set error log pathname--pid-path=PATH                    set nginx.pid pathname--lock-path=PATH                   set nginx.lock pathname
​--user=USER                        set non-privileged user forworker processes--group=GROUP                      set non-privileged group forworker processes
​--build=NAME                       set build name--builddir=DIR                     set build directory
​--with-select_module               enable select module--without-select_module            disable select module--with-poll_module                 enable poll module--without-poll_module              disable poll module
​--with-threads                     enable thread pool support
​--with-file-aio                    enable file AIO support
​--without-quic_bpf_module          disable ngx_quic_bpf_module
​--with-http_ssl_module             enable ngx_http_ssl_module--with-http_v2_module              enable ngx_http_v2_module--with-http_v3_module              enable ngx_http_v3_module--with-http_realip_module          enable ngx_http_realip_module--with-http_addition_module        enable ngx_http_addition_module--with-http_xslt_module            enable ngx_http_xslt_module--with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module--with-http_image_filter_module    enable ngx_http_image_filter_module--with-http_image_filter_module=dynamicenable dynamic ngx_http_image_filter_module--with-http_geoip_module           enable ngx_http_geoip_module--with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module--with-http_sub_module             enable ngx_http_sub_module--with-http_dav_module             enable ngx_http_dav_module--with-http_flv_module             enable ngx_http_flv_module--with-http_mp4_module             enable ngx_http_mp4_module--with-http_gunzip_module          enable ngx_http_gunzip_module--with-http_gzip_static_module     enable ngx_http_gzip_static_module--with-http_auth_request_module    enable ngx_http_auth_request_module--with-http_random_index_module    enable ngx_http_random_index_module--with-http_secure_link_module     enable ngx_http_secure_link_module--with-http_degradation_module     enable ngx_http_degradation_module--with-http_slice_module           enable ngx_http_slice_module--with-http_stub_status_module     enable ngx_http_stub_status_module
​--without-http_charset_module      disable ngx_http_charset_module--without-http_gzip_module         disable ngx_http_gzip_module--without-http_ssi_module          disable ngx_http_ssi_module--without-http_userid_module       disable ngx_http_userid_module--without-http_access_module       disable ngx_http_access_module--without-http_auth_basic_module   disable ngx_http_auth_basic_module--without-http_mirror_module       disable ngx_http_mirror_module--without-http_autoindex_module    disable ngx_http_autoindex_module--without-http_geo_module          disable ngx_http_geo_module--without-http_map_module          disable ngx_http_map_module--without-http_split_clients_module disable ngx_http_split_clients_module--without-http_referer_module      disable ngx_http_referer_module--without-http_rewrite_module      disable ngx_http_rewrite_module--without-http_proxy_module        disable ngx_http_proxy_module--without-http_fastcgi_module      disable ngx_http_fastcgi_module--without-http_uwsgi_module        disable ngx_http_uwsgi_module--without-http_scgi_module         disable ngx_http_scgi_module--without-http_grpc_module         disable ngx_http_grpc_module--without-http_memcached_module    disable ngx_http_memcached_module--without-http_limit_conn_module   disable ngx_http_limit_conn_module--without-http_limit_req_module    disable ngx_http_limit_req_module--without-http_empty_gif_module    disable ngx_http_empty_gif_module--without-http_browser_module      disable ngx_http_browser_module--without-http_upstream_hash_moduledisable ngx_http_upstream_hash_module--without-http_upstream_ip_hash_moduledisable ngx_http_upstream_ip_hash_module--without-http_upstream_least_conn_moduledisable ngx_http_upstream_least_conn_module--without-http_upstream_random_moduledisable ngx_http_upstream_random_module--without-http_upstream_keepalive_moduledisable ngx_http_upstream_keepalive_module--without-http_upstream_zone_moduledisable ngx_http_upstream_zone_module
​--with-http_perl_module            enable ngx_http_perl_module--with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module--with-perl_modules_path=PATH      set Perl modules path--with-perl=PATH                   set perl binary pathname
​--http-log-path=PATH               set http access log pathname--http-client-body-temp-path=PATH  set path to storehttp client request body temporary files--http-proxy-temp-path=PATH        set path to storehttp proxy temporary files--http-fastcgi-temp-path=PATH      set path to storehttp fastcgi temporary files--http-uwsgi-temp-path=PATH        set path to storehttp uwsgi temporary files--http-scgi-temp-path=PATH         set path to storehttp scgi temporary files
​--without-http                     disable HTTP server--without-http-cache               disable HTTP cache
​--with-mail                        enable POP3/IMAP4/SMTP proxy module--with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module--with-mail_ssl_module             enable ngx_mail_ssl_module--without-mail_pop3_module         disable ngx_mail_pop3_module--without-mail_imap_module         disable ngx_mail_imap_module--without-mail_smtp_module         disable ngx_mail_smtp_module
​--with-stream                      enable TCP/UDP proxy module--with-stream=dynamic              enable dynamic TCP/UDP proxy module--with-stream_ssl_module           enable ngx_stream_ssl_module--with-stream_realip_module        enable ngx_stream_realip_module--with-stream_geoip_module         enable ngx_stream_geoip_module--with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module--with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module--without-stream_limit_conn_module disable ngx_stream_limit_conn_module--without-stream_access_module     disable ngx_stream_access_module--without-stream_geo_module        disable ngx_stream_geo_module--without-stream_map_module        disable ngx_stream_map_module--without-stream_split_clients_moduledisable ngx_stream_split_clients_module--without-stream_return_module     disable ngx_stream_return_module--without-stream_pass_module       disable ngx_stream_pass_module--without-stream_set_module        disable ngx_stream_set_module--without-stream_upstream_hash_moduledisable ngx_stream_upstream_hash_module--without-stream_upstream_least_conn_moduledisable ngx_stream_upstream_least_conn_module--without-stream_upstream_random_moduledisable ngx_stream_upstream_random_module--without-stream_upstream_zone_moduledisable ngx_stream_upstream_zone_module
​--with-google_perftools_module     enable ngx_google_perftools_module--with-cpp_test_module             enable ngx_cpp_test_module
​--add-module=PATH                  enable external module--add-dynamic-module=PATH          enable dynamic external module
​--with-compat                      dynamic modules compatibility
​--with-cc=PATH                     set C compiler pathname--with-cpp=PATH                    set C preprocessor pathname--with-cc-opt=OPTIONS              set additional C compiler options--with-ld-opt=OPTIONS              set additional linker options--with-cpu-opt=CPU                 build for the specified CPU, valid values:pentium, pentiumpro, pentium3, pentium4,athlon, opteron, sparc32, sparc64, ppc64
​--without-pcre                     disable PCRE library usage--with-pcre                        force PCRE library usage--with-pcre=DIR                    set path to PCRE library sources--with-pcre-opt=OPTIONS            set additional build options for PCRE--with-pcre-jit                    build PCRE with JIT compilation support--without-pcre2                    do not use PCRE2 library
​--with-zlib=DIR                    set path to zlib library sources--with-zlib-opt=OPTIONS            set additional build options for zlib--with-zlib-asm=CPU                use zlib assembler sources optimizedfor the specified CPU, valid values:pentium, pentiumpro
​--with-libatomic                   force libatomic_ops library usage--with-libatomic=DIR               set path to libatomic_ops library sources
​--with-openssl=DIR                 set path to OpenSSL library sources--with-openssl-opt=OPTIONS         set additional build options for OpenSSL
​--with-debug                       enable debug logging
​

nginx编译安装过程

tar xf  nginx-1.12.0.tar.gzcd nginx-1.12.0./configure --prefix=/usr/local/nginx1.12发现依赖缺失解决依赖yum install -y pcre-devel或者untils  yum install -y zlib-devel配置完成后在源码包下生成Makefile文件make编译make  install安装命令合并./configure --prefix=/usr/local/nginx18  && make  && make install验证cd /usr/local/nginx1.12###命令优化####
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

注意

  • 案例

1. 检查基础编译工具

Nginx 编译需要 GCC、make 等工具。使用以下命令检查是否已安装:

gcc --version
make --version[root@bogon nginx-1.28.0]# gcc --version
gcc (GCC) 11.4.1 20231218 (Red Hat 11.4.1-3)
Copyright © 2021 Free Software Foundation, Inc.[root@bogon nginx-1.28.0]# make --version
GNU Make 4.3
为 x86_64-redhat-linux-gnu 编译
Copyright (C) 1988-2020 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第 3 版或更新版本<http://gnu.org/licenses/gpl.html>。

若没有对应包的编译工具则执行第二部,安装编译工具

包类型和对应的编译依赖:

源码包(Source Code)

  • 编译:从源代码编译成可执行文件。

  • 依赖严格:必须手动安装所有编译依赖(如 GCC、make、开发库等)。

  • 灵活性高:可自定义编译选项,但维护成本高。

RPM 包(Red Hat Package Manager)

  • 预编译二进制包:无需编译,直接安装。

  • 依赖自动解决:通过 dnf/yum 自动安装依赖。

  • 局限性:可能与系统环境不兼容,需选择匹配的 RPM 包。

源码 RPM 包(SRPM)

  • 包含源码和 spec 文件:需本地编译生成 RPM 包。

  • 依赖明确:spec 文件中定义了编译依赖(BuildRequires)。

  • 用途:自定义编译参数或适配特定环境。

2. 安装编译依赖

sudo dnf install -y gcc gcc-c++ make pcre-devel zlib-devel openssl-devel

ls
公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  nginx-1.28.0.tar.gz(在根下,需移动)
[root@bogon ~]# df -TH(根据使用情况选择移动路径)
文件系统                  类型      容量  已用  可用 已用% 挂载点
devtmpfs                  devtmpfs  4.2M     0  4.2M    0% /dev
tmpfs                     tmpfs     915M     0  915M    0% /dev/shm
tmpfs                     tmpfs     366M  7.5M  359M    3% /run
/dev/mapper/rl_bogon-root xfs        42G  6.0G   36G   15% /
/dev/mapper/rl_bogon-home xfs        21G  179M   20G    1% /home
/dev/nvme0n1p1            xfs       1.1G  318M  689M   32% /boot
tmpfs                     tmpfs     183M   95k  183M    1% /run/user/0
/dev/sr0                  iso9660    11G   11G     0  100% /run/media/root/Rocky-9-4-x86_64-dvd
[root@bogon ~]# mv nginx-1.28.0.tar.gz /home/(根据使用情况移动到家目录下)
[root@bogon ~]# cd /home
[root@bogon home]# ls
a  nginx-1.28.0.tar.gz# 解压源码包
[root@bogon home]# tar xf nginx-1.28.0.tar.gz -C /user/local/
[root@bogon home]# cd /user/local/nginx-1.28.0/
[root@bogon nginx-1.28.0]# ls
auto                conf             html       SECURITY.md
CHANGES             configure        LICENSE    src
CHANGES.ru          contrib          man
CODE_OF_CONDUCT.md  CONTRIBUTING.md  README.md

3. 配置编译选项

[root@bogon nginx-1.28.0]# ./configure  --prefix==/user/local/nginx./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.
报错:Nginx 配置脚本检测到 PCRE 开发库未正确安装
[root@bogon nginx-1.28.0]# yum install -y pcre(安装)
上次元数据过期检查:18:41:13 前,执行于 2025年05月27日 星期二 21时23分39秒。
软件包 pcre-8.44-3.el9.3.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
[root@bogon nginx-1.28.0]# ./configure  --prefix==/user/local/nginx
./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.报错:Nginx 配置脚本检测到 PCRE 开发库未正确安装(换库安装)
[root@bogon nginx-1.28.0]# yum install -y utils
上次元数据过期检查:18:44:29 前,执行于 2025年05月27日 星期二 21时23分39秒。
未找到匹配的参数: utils
错误:没有任何匹配: utils报错:Nginx 配置脚本检测到 PCRE 开发库未正确安装(换库安装)[root@bogon nginx-1.28.0]# yum install -y pcre-utils
上次元数据过期检查:18:50:43 前,执行于 2025年05月27日 星期二 21时23分39秒。
未找到匹配的参数: pcre-utils
错误:没有任何匹配: pcre-utils[root@bogon nginx-1.28.0]# yum install -y pcre-devel
上次元数据过期检查:0:00:42 前,执行于 2025年05月28日 星期三 16时45分13秒。
依赖关系解决。
======================================================================================================================软件包                       架构                     版本                             仓库                     大小
======================================================================================================================
安装:pcre-devel                   x86_64                   8.44-3.el9.3                     local                   470 k
安装依赖关系:pcre-cpp                     x86_64                   8.44-3.el9.3                     local                    26 kpcre-utf16                   x86_64                   8.44-3.el9.3                     local                   184 kpcre-utf32                   x86_64                   8.44-3.el9.3                     local                   175 k事务概要
======================================================================================================================
安装  4 软件包总计:855 k
安装大小:2.6 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务准备中  :                     [root@bogon nginx-1.28.0]# ./configure  --prefix==/user/local/nginxnginx path prefix: "=/user/local/nginx"nginx binary file: "=/user/local/nginx/sbin/nginx"nginx modules path: "=/user/local/nginx/modules"nginx configuration prefix: "=/user/local/nginx/conf"nginx configuration file: "=/user/local/nginx/conf/nginx.conf"nginx pid file: "=/user/local/nginx/logs/nginx.pid"nginx error log file: "=/user/local/nginx/logs/error.log"nginx http access log file: "=/user/local/nginx/logs/access.log"nginx http client request body temporary files: "client_body_temp"nginx http proxy temporary files: "proxy_temp"nginx http fastcgi temporary files: "fastcgi_temp"nginx http uwsgi temporary files: "uwsgi_temp"nginx http scgi temporary files: "scgi_temp"[root@bogon nginx-1.28.0]# ls(成功)
auto     CHANGES.ru          conf       contrib          html     Makefile  objs       SECURITY.md
CHANGES  CODE_OF_CONDUCT.md  configure  CONTRIBUTING.md  LICENSE  man       README.md  src

5. 编译并安装

# 编译
[root@bogon nginx-1.28.0]# make 
-lcrypt -lpcre -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|=/user/local/nginx|" \-e "s|%%PID_PATH%%|=/user/local/nginx/logs/nginx.pid|" \-e "s|%%CONF_PATH%%|=/user/local/nginx/conf/nginx.conf|" \-e "s|%%ERROR_LOG_PATH%%|=/user/local/nginx/logs/error.log|" \< man/nginx.8 > objs/nginx.8
make[1]: 离开目录“/user/local/nginx-1.28.0”
[root@bogon nginx-1.28.0]# ls
auto     CHANGES.ru          conf       contrib          html     Makefile  objs       SECURITY.md
CHANGES  CODE_OF_CONDUCT.md  configure  CONTRIBUTING.md  LICENSE  man       README.md  src# 安装
[root@bogon nginx-1.28.0]# make install
make -f objs/Makefile install
make[1]: 进入目录“/user/local/nginx-1.28.0”
test -d '=/user/local/nginx' || mkdir -p '=/user/local/nginx'
test -d '=/user/local/nginx/sbin' \
test -d '=/user/local/nginx/logs' \|| mkdir -p '=/user/local/nginx/logs'
make[1]: 离开目录“/user/local/nginx-1.28.0”

编译安装完成的软件默认不被systemd管理

问题总结:

在安装和编译 Nginx 的过程中,可能会遇到各种环境依赖、配置参数或编译选项相关的问题。以下是一些常见问题及解决思路:

一、依赖缺失问题

Nginx 编译需要依赖一些基础开发工具和库,缺失时会导致编译失败。

1. 缺少编译器或构建工具
  • 错误提示make: command not foundgcc: command not found

  • 原因:未安装 GCC 编译器或 Make 工具。

  • 解决方法:

    # Debian/Ubuntu 系统
    sudo apt-get update && sudo apt-get install build-essential# CentOS/RHEL 系统
    sudo yum install gcc make automake autoconf libtool
2. 缺少 PCRE 库
  • 错误提示./configure: error: the HTTP rewrite module requires the PCRE library

  • 原因:Nginx 的 rewrite 模块依赖 PCRE(用于正则表达式处理)。

  • 解决方法:

    # Debian/Ubuntu
    sudo apt-get install libpcre3 libpcre3-dev
    ​
    # CentOS/RHEL
    sudo yum install pcre pcre-devel
3. 缺少 zlib 库
  • 错误提示./configure: error: the HTTP gzip module requires the zlib library

  • 原因:gzip 压缩模块依赖 zlib。

  • 解决方法:

    # Debian/Ubuntu
    sudo apt-get install zlib1g zlib1g-dev# CentOS/RHEL
    sudo yum install zlib zlib-devel
4. 缺少 OpenSSL 库
  • 错误提示./configure: error: the HTTP SSL module requires the OpenSSL library

  • 原因:SSL/TLS 功能依赖 OpenSSL。

  • 解决方法:

    # Debian/Ubuntu
    sudo apt-get install openssl libssl-dev# CentOS/RHEL
    sudo yum install openssl openssl-devel
二、配置参数问题

./configure 命令参数错误可能导致编译失败或功能缺失。

1. 模块选择错误
  • 问题:未启用必要模块(如 --with-http_ssl_module)或禁用了核心模块。

  • 解决方法:

    • 查看官方文档,确认所需模块的配置参数。

    • 示例:编译时启用 SSL 和 Rewrite 模块:

      ./configure --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module
2. 路径冲突
  • 错误提示nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

  • 原因:默认安装路径与现有服务(如系统自带 Nginx)冲突,或端口被占用。

  • 解决方法:

    • 使用 --prefix

      指定自定义安装路径:

      ./configure --prefix=/usr/local/nginx_new
    • 编译前停止其他占用 80/443 端口的服务(如 Apache)。

三、编译过程错误
1. 权限不足
  • 错误提示make: *** No rule to make target 'install'. Stop. 或文件写入失败。

  • 原因:未使用 sudo 执行编译或安装命令。

  • 解决方法:

    sudo make && sudo make install
2. 代码编译失败
  • 错误提示:包含 error:warning:的编译日志(如语法错误、类型不匹配)。

  • 原因:

    • 下载的 Nginx 源码损坏或版本与系统不兼容。

    • 依赖库版本过旧(如旧版 GCC 不支持某些 C 特性)。

  • 解决方法:

    • 重新下载源码,校验 MD5/SHA256 哈希值。

    • 升级系统工具链(如 GCC)或使用 LTS 稳定版源码。

四、安装后启动问题
1. 配置文件语法错误
  • 错误提示nginx: [emerg] unknown directive "xxx"nginx: [error] invalid value "xxx"

  • 原因nginx.conf 配置文件存在语法错误(如标点符号、指令拼写错误)。

  • 解决方法:

    • 检查配置文件:

      /usr/local/nginx/sbin/nginx -t
    • 根据提示修正语法错误(例如分号缺失、括号不匹配等)。

2. 端口权限问题
  • 错误提示nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

  • 原因:普通用户无法监听 1024 以下端口(如 80/443)。

  • 解决方法:

    • 使用sudo 启动 Nginx:

      sudo /usr/local/nginx/sbin/nginx
    • 将端口修改为 1024 以上(如 8080),或为 Nginx 赋予端口权限(需谨慎)。

3. 进程冲突
  • 错误提示nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (9: Bad file descriptor)

  • 原因:残留的 Nginx 进程或 PID 文件未清理。

  • 解决方法:

    # 杀死残留进程
    sudo kill -9 $(pidof nginx)
    # 删除旧 PID 文件
    sudo rm -f /usr/local/nginx/logs/nginx.pid
    # 重新启动
    sudo /usr/local/nginx/sbin/nginx
五、其他常见问题
1. 系统兼容性问题
  • 问题:在非 Linux 系统(如 macOS、Windows)编译 Nginx 时可能遇到工具链差异。

  • 解决方法:

    • macOS:使用 Homebrew 安装依赖(brew install pcre zlib openssl),或直接通过 brew install nginx 一键安装。

    • Windows:建议使用预编译的二进制包,或通过 WSL(Windows Subsystem for Linux)编译。

2. 模块编译失败
  • 问题:第三方模块(如 ngx_http_geoip2_module)编译时依赖额外库(如 GeoIP2)。

  • 解决方法:

    • 先安装模块依赖库,再通过

      --add-module

      参数编译:

      # 示例:编译时添加 Redis 模块
      wget https://github.com/leev/ngx_http_redis/archive/master.zip
      unzip master.zip
      ./configure --add-module=./ngx_http_redis-master
      make && make install

总结:编译安装 Nginx 的最佳实践

  1. 使用稳定版本:优先下载 Nginx 官方 LTS 版本。

  2. 提前安装依赖:在编译前通过包管理器安装所有必需的开发库(如 build-essentialpcre-devel 等)。

  3. 清晰配置参数:明确需要启用的模块和安装路径,避免默认配置冲突。

  4. 逐步调试:编译过程中保留终端输出,遇到错误时根据提示逐步排查依赖或参数问题。

  5. 备份与测试:安装前备份原有配置,启动后通过 curl 或浏览器验证服务是否正常。

8、二进制安装

只需要运行bin文件
  • 案例
jdk-6u14-linux-x64.bin./jdk-6u14-linux-x64.bin根据提示进行操作即可

四、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

    • 核心任务:

      1. 解析/etc/systemd/system/default.target(默认运行级别)

      2. 并行启动服务单元(Unit)

      3. 激活sysinit.target(系统初始化)、basic.target(基础服务)

  • 关键操作命令

    systemctl get-default         # 查看默认target
    systemctl set-default multi-user.target  # 设置默认为命令行模式
    journalctl -b                # 查看本次启动日志
[root@bogon ~]# systemctl get-default
graphical.target

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

五、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说明
0poweroff.target关机状态,使用该级别时将会关闭主机
1rescue.target单用户模式,不需要密码验证即可登录系统,多用于系统维护
2multi-user.target用户定义/域特定运行级别。默认等同于3
3multi-user.target字符界面的完整多用户模式,大多数服务器主机运行在此级别
4multi-user.target用户定义/域特定运行级别。默认等同于3
5graphical.target图形界面的多用户模式,提供了图形桌面操作环境
6reboot.target重新启动,使用该级别时将会重启主机

查看运行级别

runlevel
N|运行级别数字   当前运行级别数字
[root@bogon ~]# runlevel
N 5

切换运行级别

init  运行级别数字
[root@bogon ~]# runlevel
5 3

查看默认运行级别

systemctl get-default

设置默认运行级别

systemctl set-default  运行级别target

2、systemctl命令的使用

语法结构:

systemctl  服务控制类型   服务名称[.service]

服务控制类型需改

start:启动

stop:停止

restart:重启,会停止服务

reload:重载,不停止服务,加载配置文件;优雅的重启

status:查看状态

1、Active: active (running) #正常运行
2、Active: inactive (dead) #正常停止
3、Active:failed(Result:ext-code) #错误退出

enable:开机自启动

is-enabled:查看是否开机自启动

1、systemctl is-enabled http
2、disabled
#关闭开机自启动
3、systemctl is-enabled http
4、enabled
#开启开机自启动

disabled:关闭开机自启动

--now 配合enable与disable使用;在进行开机自启或不自启的同时进行打开或关闭指定服务

  • 选项
-t
​
指定unit类型
​
Unit Commands
​
list-units
​
列出所有unit,一般与-t结合

案例

systemctl list-units -t service
​
systemctl daemon-reload  #当改变unit的脚本文件后,需要进行重新识别

相关文章:

  • Android LiveData 详解
  • 查询oracle进程数和会话数进行优化
  • 友达光电12.1寸液晶屏G121XN01 V001工控屏
  • 深入浅出DeepSeek:从零开始的AI编程指南
  • There is a chart instance already initialized on the dom. 柱状图初始化时报前面这个错误如何解决?
  • 龙虎榜——20250528
  • LeeCode 94. 二叉树的中序遍历
  • 74道Node.js高频题整理(附答案背诵版)
  • 简乐 1.4.0 | 非常简洁 无损下载 畅听全网
  • 头歌之动手学人工智能-Pytorch 之autograd
  • 王树森推荐系统公开课 排序05:排序模型的特征
  • 【NLP】将 LangChain 与模型上下文协议 (MCP) 结合使用
  • 华为OD机试真题——模拟工作队列(2025A卷:200分)Java/python/JavaScript/C/C++/GO最佳实现
  • bat 批处理通过拖拽,来获取拖入文件的信息
  • Linux之高效文本编辑利器 —— vim
  • 【动态规划】子数组系列(二)
  • CSP 2024 提高级第一轮(CSP-S 2024)阅读程序第一题解析
  • Typora中文直装版
  • 2025.5.28总结
  • 2. 数据结构基本概念 (2)
  • 网站怎么做数据备份/天天网站
  • 高端模板网站建设/精准客源推广引流
  • 跨国网站怎么做/微信管理系统登录
  • 淘宝 网站建设教程视频/怎么自己弄一个平台
  • 公司哪家好点/中山seo关键词
  • iapp网站做软件教程/企业培训师资格证报考2022