1.1 源码安装
当你需要定制化程序的时候
cd /opt/
wget -c https://nginx.org/download/nginx-1.28.0.tar.gz
tar -xvf nginx-1.28.0.tar.gz
yum install gcc pcre-devel zlib-devel -y
./configure #检测环境
make && make install #make编译,make install安装编译好的程序到系统
/usr/local/nginx/sbin/nginx
[root@rhel93 html]# echo "hello world" > /usr/local/nginx/html/index.html
[root@rhel93 html]# curl 192.168.168.197
hello world
1.2 yum安装
1.2.1 本地仓库file://
本地路径里面需要有被安装的软件包
rhel7+
[root@rhel79 yum.repos.d]# cat /etc/yum.repos.d/base.repo
[base]
name=base
baseurl=file:///mnt/
gpgcheck=0
[root@rhel79 ~]# mount /dev/sr0 /mnt
rhel9+
[root@rhel93 ~]# cat /etc/yum.repos.d/base.repo
[baseos]
name=baseos
baseurl=file:///media/BaseOS
gpgcheck=0
[App]
name=app
baseurl=file:///media/AppStream
gpgcheck=0
[root@rhel93 ~]# mount /dev/sr0 /media/
1.2.2 网络仓库http://
网站里有需要被安装的软件包
rhel7+
[root@rhel79 yum.repos.d]# cat /etc/yum.repos.d/aliyun.repo
[aliyun]
name=aliyun
baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009/os/x86_64/
gpgcheck=0
[root@rhel79 yum.repos.d]# ping -c2 www.baidu.com
PING www.a.shifen.com (183.2.172.177) 56(84) bytes of data.
64 bytes from 183.2.172.177 (183.2.172.177): icmp_seq=1 ttl=128 time=78.3 ms
64 bytes from 183.2.172.177 (183.2.172.177): icmp_seq=2 ttl=128 time=43.3 ms
rhel9+
[root@rhel93 ~]# cat /etc/yum.repos.d/aliyun.repo
[aliyun-base]
name=aliyun-baseos
baseurl=https://mirrors.aliyun.com/centos-stream/9-stream/BaseOS/x86_64/os/
gpgcheck=0
[aliyun-app]
name=aliyun-app
baseurl=https://mirrors.aliyun.com/centos-stream/9-stream/AppStream/x86_64/os/
gpgcheck=0
[root@rhel93 ~]# ping -c2 www.baidu.com
PING www.a.shifen.com (183.2.172.177) 56(84) 比特的数据。
64 比特,来自 183.2.172.177 (183.2.172.177): icmp_seq=1 ttl=128 时间=60.0 毫秒
64 比特,来自 183.2.172.177 (183.2.172.177): icmp_seq=2 ttl=128 时间=35.1 毫秒
1.3 自建yum仓库
下载某软件包制作仓库:
#download只下载不安装httpd软件包,--resolve是解决依赖
[root@rhel93 ~]# yum download httpd --resolve --destdir /yum_repo/httpd/Packages
[root@rhel93 ~]# tree /yum_repo/httpd/
/yum_repo/httpd/
└── Packages
├── apr-1.7.0-11.el9.x86_64.rpm
├── apr-util-1.6.1-23.el9.x86_64.rpm
├── apr-util-bdb-1.6.1-23.el9.x86_64.rpm
├── apr-util-openssl-1.6.1-23.el9.x86_64.rpm
├── httpd-2.4.57-5.el9.x86_64.rpm
├── httpd-core-2.4.57-5.el9.x86_64.rpm
├── httpd-filesystem-2.4.57-5.el9.noarch.rpm
├── httpd-tools-2.4.57-5.el9.x86_64.rpm
├── mod_http2-1.15.19-5.el9.x86_64.rpm
├── mod_lua-2.4.57-5.el9.x86_64.rpm
└── redhat-logos-httpd-90.4-2.el9.noarch.rpm
# createrepo为一堆 RPM 软件包创建一个元数据仓库(repodata/目录),使其成为一个可被 yum或 dnf包管理器识别和使用的正式软件仓库。
[root@rhel93 yum.repos.d]# yum install createrepo_c -y
[root@rhel93 ~]# createrepo /yum_repo/httpd/
[root@rhel93 ~]# tree -L 1 /yum_repo/httpd/
/yum_repo/httpd/
├── Packages
└── repodata
[root@rhel93 ~]# cat /etc/yum.repos.d/httpd.repo
[httpd]
name=httpd
baseurl=file:///yum_repo/httpd
gpgcheck=0
#查看httpd仓库中的软件包
[root@rhel93 ~]# yum repo-pkgs httpd list
下载epel源中软件制作仓库:
[root@rhel93 ~]# yum install yum-utils createrepo -y
[root@rhel93 ~]# reposync --repoid=epel -p /yum_repo/
[root@rhel93 ~]# tree -L 2 /yum_repo/
/yum_repo/
└── epel
└── Packages
[root@rhel93 ~]# createrepo /yum_repo/epel/
[root@rhel93 ~]# tree -L 2 /yum_repo/
/yum_repo/
└── epel
├── Packages
└── repodata
[root@rhel93 ~]# cat /etc/yum.repos.d/bendi-epel.repo
[bendi-epel]
name=bendi-epel
baseurl=file:///yum_repo/epel
gpgcheck=0
#列出bendi-epel这个id仓库中的可用软件包
[root@rhel93 ~]# yum repo-pkgs bendi-epel list available
2、运行程序
安装nginx程序
yum install nginx -y
2.1 直接执行命令
先找到nginx程序文件
#查询nginx程序生成了哪些文件,主要找bin/sbin目录下的程序文件
[root@rhel93 ~]# rpm -ql nginx | grep bin
/usr/bin/nginx-upgrade
[root@rhel93 ~]# echo $PATH
/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
[root@rhel93 ~]# man nginx-upgrade
#查询nginx程序依赖的包生成了哪些文件,主要找bin/sbin目录下的程序文件
[root@rhel93 ~]# rpm -ql nginx-core | grep bin
/usr/sbin/nginx
[root@rhel93 ~]# man nginx
#运行nginx
[root@rhel93 ~]# nginx
#查询nginx的配置文件,主要找conf结尾的文件
#-qc表示查询nginx-core包生成的所有的配置文件
[root@rhel93 ~]# rpm -qc nginx-core | grep conf
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
[root@rhel93 ~]# rpm -ql nginx-core | grep conf
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
#-qa查询所有已经安装了的软件包
[root@rhel93 ~]# rpm -qa | grep nginx
nginx-filesystem-1.20.1-14.el9_2.1.noarch
nginx-core-1.20.1-14.el9_2.1.x86_64
nginx-1.20.1-14.el9_2.1.x86_64
[root@rhel93 ~]# nginx -h
nginx version: nginx/1.20.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-s signal : send signal to a master process: stop, quit, reopen, reload
#查看nginx是否正常运行
[root@rhel93 ~]# ps -ef | grep nginx
root 35915 1 0 09:36 ? 00:00:00 nginx: master process nginx
nginx 35916 35915 0 09:36 ? 00:00:00 nginx: worker process
nginx 35917 35915 0 09:36 ? 00:00:00 nginx: worker process
nginx 35918 35915 0 09:36 ? 00:00:00 nginx: worker process
nginx 35919 35915 0 09:36 ? 00:00:00 nginx: worker process
root 36035 8241 0 09:37 pts/0 00:00:00 grep --color=auto nginx
#查看nginx是否正常运行
[root@rhel93 ~]# ps aux | grep nginx
root 35915 0.0 0.0 10052 948 ? Ss 09:36 0:00 nginx: master process nginx
nginx 35916 0.0 0.2 13940 4984 ? S 09:36 0:00 nginx: worker process
nginx 35917 0.0 0.2 13940 4984 ? S 09:36 0:00 nginx: worker process
nginx 35918 0.0 0.2 13940 4984 ? S 09:36 0:00 nginx: worker process
nginx 35919 0.0 0.2 13940 4984 ? S 09:36 0:00 nginx: worker process
root 36181 0.0 0.1 6636 2336 pts/0 R+ 09:37 0:00 grep --color=auto nginx
#查看nginx是否正常监听且监听端口是多少
[root@rhel93 ~]# ss -lntup | grep 80
tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=35919,fd=6),("nginx",pid=35918,fd=6),("nginx",pid=35917,fd=6),("nginx",pid=35916,fd=6),("nginx",pid=35915,fd=6))
tcp LISTEN 0 511 [::]:80 [::]:* users:(("nginx",pid=35919,fd=7),("nginx",pid=35918,fd=7),("nginx",pid=35917,fd=7),("nginx",pid=35916,fd=7),("nginx",pid=35915,fd=7))
#查看nginx是否正常监听且监听端口是多少
[root@rhel93 ~]# netstat -lntup | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 35915/nginx: master
tcp6 0 0 :::80 :::* LISTEN 35915/nginx: master
2.2 systemctl管理服务
#本次启动nginx
[root@rhel93 ~]# systemctl start nginx
#查看nginx的状态
[root@rhel93 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
Active: active (running) since Sun 2025-08-31 10:10:10 CST; 6s ago #主要看当前行中的active(running)表示运行中
Process: 2088 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 2089 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 2090 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 2091 (nginx)
Tasks: 5 (limit: 10816)
Memory: 6.3M
CPU: 18ms
CGroup: /system.slice/nginx.service
├─2091 "nginx: master process /usr/sbin/nginx"
├─2092 "nginx: worker process"
├─2093 "nginx: worker process"
├─2094 "nginx: worker process"
└─2095 "nginx: worker process"
8月 31 10:10:10 rhel93 systemd[1]: Starting The nginx HTTP and reverse proxy server...
8月 31 10:10:10 rhel93 nginx[2089]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
8月 31 10:10:10 rhel93 nginx[2089]: nginx: configuration file /etc/nginx/nginx.conf test is successful
8月 31 10:10:10 rhel93 systemd[1]: Started The nginx HTTP and reverse proxy server.
#停止本次nginx的运行
[root@rhel93 ~]# systemctl stop nginx
#设置nginx的开机自动启动,--now表示现在立马就启动
[root@rhel93 ~]# systemctl enable --now nginx
[root@rhel93 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
Active: active (running) since Sun 2025-08-31 10:11:07 CST; 1s ago
Process: 2463 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 2464 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 2465 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 2466 (nginx)
Tasks: 5 (limit: 10816)
Memory: 4.8M
CPU: 16ms
CGroup: /system.slice/nginx.service
├─2466 "nginx: master process /usr/sbin/nginx"
├─2467 "nginx: worker process"
├─2468 "nginx: worker process"
├─2469 "nginx: worker process"
└─2470 "nginx: worker process"
8月 31 10:11:07 rhel93 systemd[1]: Starting The nginx HTTP and reverse proxy server...
8月 31 10:11:07 rhel93 nginx[2464]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
8月 31 10:11:07 rhel93 nginx[2464]: nginx: configuration file /etc/nginx/nginx.conf test is successful
8月 31 10:11:07 rhel93 systemd[1]: Started The nginx HTTP and reverse proxy server.
#重启验证nginx是否开机自动运行
[root@rhel93 ~]# reboot
#关闭nginx的开机自动启动,--now表示现在立马关闭nginx
[root@rhel93 ~]# systemctl disable --now nginx
#查询nginx的service文件是哪个包生成的
[root@rhel93 ~]# rpm -qf /usr/lib/systemd/system/nginx.service
nginx-1.20.1-14.el9_2.1.x86_64
3、查看进程
3.1 静态查看
#ps -ef查看进程某一刻的状态,使用awk可以匹配nginx进程信息
[root@rhel93 ~]# ps -ef | awk ' NR==1 {print $0 } /nginx/ {print $0}'
UID PID PPID C STIME TTY TIME CMD
root 13089 1 0 10:43 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 13090 13089 0 10:43 ? 00:00:00 nginx: worker process
nginx 13091 13089 0 10:43 ? 00:00:00 nginx: worker process
nginx 13092 13089 0 10:43 ? 00:00:00 nginx: worker process
nginx 13093 13089 0 10:43 ? 00:00:00 nginx: worker process
#ps aux查看进程某一刻的状态,使用awk可以匹配nginx进程信息
[root@rhel93 ~]# ps aux | awk ' NR==1 {print $0 } /nginx/ {print $0}'
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 13089 0.0 0.0 10052 944 ? Ss 10:43 0:00 nginx: master process /usr/sbin/nginx
nginx 13090 0.0 0.2 13940 5012 ? S 10:43 0:00 nginx: worker process
nginx 13091 0.0 0.2 13940 5012 ? S 10:43 0:00 nginx: worker process
nginx 13092 0.0 0.2 13940 5012 ? S 10:43 0:00 nginx: worker process
nginx 13093 0.0 0.2 13940 5012 ? S 10:43 0:00 nginx: worker process
3.2 动态查看
#动态查看进程信息,默认每3秒刷新一次
top
3.3 进程树方式查看进程
[root@rhel93 ~]# pstree
[root@rhel93 ~]# pstree -p
systemd(1)─┬─NetworkManager(866)─┬─{NetworkManager}(869)
│ └─{NetworkManager}(870)
├─VGAuthService(838)
├─anacron(19695)
├─atd(903)
├─auditd(800)─┬─sedispatch(802)
│ ├─{auditd}(801)
│ └─{auditd}(803)
├─chronyd(853)
├─crond(905)
├─dbus-broker-lau(826)───dbus-broker(828)
├─firewalld(832)───{firewalld}(1097)
├─irqbalance(834)───{irqbalance}(851)
├─login(916)───bash(1657)
├─lsmd(835)
├─mcelog(836)
├─nginx(13089)─┬─nginx(13090)
│ ├─nginx(13091)
│ ├─nginx(13092)
│ └─nginx(13093)
4、查看进程监听端口
[root@rhel93 ~]# netstat -lntup
Active Internet connections (only servers)
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 13089/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 893/sshd: /usr/sbin
tcp6 0 0 :::80 :::* LISTEN 13089/nginx: master
tcp6 0 0 :::22 :::* LISTEN 893/sshd: /usr/sbin
udp 0 0 127.0.0.1:323 0.0.0.0:* 853/chronyd
udp6 0 0 ::1:323 :::* 853/chronyd
[root@rhel93 ~]# ss -lntup
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
udp UNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:(("chronyd",pid=853,fd=5))
udp UNCONN 0 0 [::1]:323 [::]:* users:(("chronyd",pid=853,fd=6))
tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=13093,fd=6),("nginx",pid=13092,fd=6),("nginx",pid=13091,fd=6),("nginx",pid=13090,fd=6),("nginx",pid=13089,fd=6))
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=893,fd=3))
tcp LISTEN 0 511 [::]:80 [::]:* users:(("nginx",pid=13093,fd=7),("nginx",pid=13092,fd=7),("nginx",pid=13091,fd=7),("nginx",pid=13090,fd=7),("nginx",pid=13089,fd=7))
tcp LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=893,fd=4))
#过滤进程的监听端口
[root@rhel93 ~]# ss -lntup | grep 80
tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=13093,fd=6),("nginx",pid=13092,fd=6),("nginx",pid=13091,fd=6),("nginx",pid=13090,fd=6),("nginx",pid=13089,fd=6))
tcp LISTEN 0 511 [::]:80 [::]:* users:(("nginx",pid=13093,fd=7),("nginx",pid=13092,fd=7),("nginx",pid=13091,fd=7),("nginx",pid=13090,fd=7),("nginx",pid=13089,fd=7))
#过滤进程的命令查看进程的监听信息
[root@rhel93 ~]# ss -lntup | grep nginx
tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=13093,fd=6),("nginx",pid=13092,fd=6),("nginx",pid=13091,fd=6),("nginx",pid=13090,fd=6),("nginx",pid=13089,fd=6))
tcp LISTEN 0 511 [::]:80 [::]:* users:(("nginx",pid=13093,fd=7),("nginx",pid=13092,fd=7),("nginx",pid=13091,fd=7),("nginx",pid=13090,fd=7),("nginx",pid=13089,fd=7))
5、杀死进程
5.1 关闭正常进程
[root@rhel93 ~]# ps -ef | grep nginx
root 13089 1 0 10:43 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 13090 13089 0 10:43 ? 00:00:00 nginx: worker process
nginx 13091 13089 0 10:43 ? 00:00:00 nginx: worker process
nginx 13092 13089 0 10:43 ? 00:00:00 nginx: worker process
nginx 13093 13089 0 10:43 ? 00:00:00 nginx: worker process
root 23649 1688 0 11:11 pts/0 00:00:00 grep --color=auto nginx
[root@rhel93 ~]# kill 13089
[root@rhel93 ~]# killall nginx
5.2 非正常进程的关闭
[root@rhel93 ~]# ps -ef | grep nginx
root 24322 1 0 11:13 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 24323 24322 0 11:13 ? 00:00:00 nginx: worker process
nginx 24324 24322 0 11:13 ? 00:00:00 nginx: worker process
nginx 24325 24322 0 11:13 ? 00:00:00 nginx: worker process
nginx 24326 24322 0 11:13 ? 00:00:00 nginx: worker process
root 24382 1688 0 11:13 pts/0 00:00:00 grep --color=auto nginx
[root@rhel93 ~]# kill -9 24322