六、linux软件安装

本文详细介绍了在Linux系统中,尤其是CentOS上,如何使用yum和rpm命令进行软件安装。内容涵盖查询、安装、卸载和升级等操作,探讨了yum与rpm的区别,以及各自的优势和缺点。同时,讲解了rpm包的来源、yum软件安装的相关问题,包括仓库、缓存目录和源。此外,还讨论了编译安装和二进制安装的场景,以及多任务模块和一键安装脚本的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux 安装软件

一. yum命令的简单使用

1.查询
yum search 指定字符串  在软件包详细信息中搜索指定字符串
yum list 查看哪些软件安装了,哪些软件没有安装
  rpm -qa --query all 作用相似
yum repolist 查看那些源可以使用
yum provides 命令  查看指定命令是通过那个软件包安装过来的
  rpm -qf /bin/ls --query all 作用相似
yum install tree 安装tree命令
2.安装
yum install 命令 安装指定命令
yum reinstall 命令 重新安装指定命令
3.卸载
yum remove tree 删除tree命令
4.升级
yum update 默认升级所有的软件 ,安装好系统升级所有的软件可以起到补丁的作用
5.其他
yum clean all 清除yum命令的repocache内容

二. rpm命令的简单使用

1.查询
rpm -qa --query all # 查询系统里已经安装的所有软件
rpm -qi  --query information # 查询软件的信息
rpm -ql  --query list # 查询已经安装的软件的安装路径
rpm -qc    # 查询已经安装的软件的安装路径
rpm -qf  --query file # 查询已经安装的命令或者是文件是通过那个软件包安装过来的
rpm -qpi --query package information # 查询没有安装的软件包的信息
rpm -qpl --query package list # 查询没有安装的软件包会安装到哪里
2.安装
rpm -ivh --install verbose hash # 显示安装软件的详细过程信息
--hash --Print 50 hash marks as the package archive is unpacked. #符号
--reinstall 重新安装
3.卸载
-e --erase 卸载软件
4.升级
rpm -U  tree-1.7.0-15.el8.x86_64.rpm 
-U --update 升级软件
如果软件没有安装,则升级操作会自动帮助安装升级

三. yum和rpm的区别

1.yum与rpm的关系

yum底层源代码是由python语言编写的命令脚本,是站在rpm的肩膀上的命令,即底层依然靠rpm来实现安装软件。

yum源码如下:

vi /usr/bin/yum
try:
#!/usr/bin/python
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
   %s
Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
%s
If you cannot solve this problem yourself, please go to
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

""" % (sys.exc_value, sys.version)
    sys.exit(1)

sys.path.insert(0, '/usr/share/yum-cli')
try:
    import yummain
    yummain.user_main(sys.argv[1:], exit_code=True)
except KeyboardInterrupt, e:
    print >> sys.stderr, "\n\nExiting on user cancel."
    sys.exit(1)
2.yum管理软件的优点和rpm的缺点

yum命令能更方便的管理rpm软件包,自动的解决rpm包间的依赖关系,可以配置多个资源库。相反rpm命令则不可以自动解决软件包之间的依赖关系,缺乏自动化和智能化。

问题1:yum如何解决依赖关系?
当敲下yum install tree时,yum命令会自动的加载/mnt/repodate数据到/var/cache/yum/x86_64/7/base中缓存,该文件中.sqlite文件和.xml文件里存放了yum下载的软件依赖关系信息。

cd /mnt/repodata/
ls
0e82d111931e402316702696ae4178e65673dc0f9c23a2bb012d192cb2625ea1-primary.sqlite.bz2
4a1c8221285db1b9d5361d4567baa883e60ee29c32d8d0a1b7777d7d7761664d-filelists.xml.gz
60aa4673bf5fa63d1a7a640ef407b8e48fe5b3337720d844a29a53fec130d6da-filelists.sqlite.bz2
9d6292fa12fd9ca472fafa8c0b348c3e1f139681aa066d41dc53b3563bcd1f53-other.sqlite.bz2
a4e2b46586aa556c3b6f814dad5b16db5a669984d66b68e873586cd7c7253301-c7-x86_64-comps.xml.gz
a532e7a8702a10ffb880fe381f35662cfbde9014e85ea32cba19da7677f6aca3-primary.xml.gz
b157d8b012febabf1c72d579b5155b8519a362a64bbd37c40acb22b6cc544ccf-other.xml.gz
cca56f3cffa18f1e52302dbfcf2f0250a94c8a37acd8347ed6317cb52c8369dc-c7-x86_64-comps.xml
repomd.xml
repomd.xml.asc
TRANS.TBL

cd /var/cache/yum/x86_64/7/base
ls
6d0c3a488c282fe537794b5946b01e28c7f44db79097bb06826e1c0c88bad5ef-primary.sqlite.bz2
a4e2b46586aa556c3b6f814dad5b16db5a669984d66b68e873586cd7c7253301-c7-x86_64-comps.xml.gz
cachecookie
d6d94c7d406fe7ad4902a97104b39a0d8299451832a97f31d71653ba982c955b-filelists.sqlite.bz2
gen
mirrorlist.txt
packages
repomd.xml
 yum clean all  # 清除元数据缓存
已加载插件:fastestmirror, product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

正在清理软件源: base extras updates
Cleaning up list of fastest mirrors
ls
gen  packages # repodata缓存清除


问题2:yum如何知道去哪里下载软件?
在某台主机的centos7.9版本的环境上,当敲下yum install tree时,系统自动调用fastestmirror软件,来查找距离你主机最近的下载源镜像,(我们国内为了下载软件的速度考虑各大高校,企业运营了许多镜像站点,它们是官方站点源的复刻,便于我们快速下载软件包),每一个镜像源都是一个文件夹,里面有相应的软件包。

仓库文件目录:/etc/yum.repos.d/

cat CentOS-Base.repo  # 查看base源文件
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#


仓库文件目录:/etc/yum.repos.d
[base]
name=CentOS-$releasever - Base # 仓库的名字
mirrorlist=http://mirrorlist.centos.org/?   # 官网源链接 release=$releasever&arch=$basearch&repo=os&infra=$infra # 查询获取离你最近的镜像位置
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/ # 仓库的具体位置 源的位置
gpgcheck=1 # 加密技术 通过repo_gpgcheck对源进行检查,是否是真的base仓库
#enabled=0 # 表示该仓库不可用
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 # 公钥存储地址

#released updates 
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

四. rpm和yum安装命令的典型目录结构

4.1 典型应用程序的目录结构
# 普通执行程序文件-->/usr/bin
[root@mahaoliang bin]# pwd
/usr/bin
[root@mahaoliang bin]# ls .|grep nginx
[root@mahaoliang bin]# ls
[                                   glib-compile-schemas   nl-route-delete           sg_turs
a2p                                 gmake                  nl-route-get              sg_unmap
aclocal                             gneqn                  nl-route-list             sg_verify
aclocal-1.13                        gnroff                 nl-rule-list              sg_vpd
addr2line                           gpasswd                nl-tctree-list            sg_write_buffer
alias                               gpg                    nl-util-addr              sg_write_long

# 服务器执行程序文件和管理程序文件-->/usr/sbin
[root@mahaoliang bin]# cd /usr/sbin
[root@mahaoliang sbin]# ls
accessdb              exportfs                   load_policy                 postcat                    sulogin
addgnupghome          faillock                   logrotate                   postconf                   sushell
addpart               fdformat                   logsave                     postdrop                   swaplabel
adduser               fdisk                      losetup                     postfix                    swapoff
agetty                filefrag                   lpasswd                     postkick       

# 应用程序配置文件-->/etc
[root@mahaoliang sbin]# cd /etc
[root@mahaoliang etc]# ls
adjtime                  dracut.conf.d  inittab                   my.cnf.d           rc3.d             subgid-
aliases                  e2fsck.conf    inputrc                   netconfig          rc4.d             subuid
aliases.db               environment    iproute2                  NetworkManager     rc5.d             subuid-
alternatives             ethertypes     issue                     networks           rc6.d             s

# 日志文件-->/var/log
[root@mahaoliang etc]# cd /var/log
[root@mahaoliang log]# ls
anaconda           btmp-20220601       lastlog            nginx             spooler-20220612      vmware-network.log
audit              cron                maillog            rhsm              tallylog              vmware-vgauthsvc.log.0
boot.log           cron-20220529       maillog-20220529   secure            tuned                 vmware-vmsvc-root.log
boot.log-20220528  cron-20220606       maillog-20220606   secure-20220529   vmware-network.1.log  vmware-vmtoolsd-root.log
boot.log-20220531  cron-20220612       maillog-20220612   secure-20220606   vmware-network.2.log  wtmp
boot.log-20220602  dmesg               messages           secure-20220612   vmware-network.3.log  xferlog
boot.log-20220604  dmesg.old           messages-20220529  spooler           vmware-network.4.log  xferlog-20220607
boot.log-20220610  firewalld           messages-20220606  spooler-20220529  vmware-network.5.log  xferlog-20220612
btmp               grubby_prune_debug  messages-20220612  spooler-20220606  vmware-network.6.log  yum.log
[root@mahaoliang log]# 

# 应用程序文档文件-->/usr/share/doc
[root@mahaoliang log]# cd /usr/share/doc
[root@mahaoliang doc]# ls
acl-2.2.51                           kbd-1.15.5                    perl-constant-1.27
aic94xx-firmware-30                  kexec-tools-2.0.15            perl-Data-Dumper-2.145
alsa-firmware-1.0.28                 keyutils-1.5.8                perl-Encode-2.51
alsa-lib                             keyutils-libs-1.5.8           perl-Error-0.17020

# 应用程序手册文件-->/usr/share/man
[root@mahaoliang doc]# cd /usr/share/man
[root@mahaoliang man]# ls
cs  de  fr  id  ja  man0p  man1p  man2   man3   man3x  man4x  man5x  man6x  man7x  man8x  man9x  nl  pt     ru  sv  zh_CN
da  es  hu  it  ko  man1   man1x  man2x  man3p  man4   man5   man6   man7   man8   man9   mann   pl  pt_BR  sk  tr  zh_TW
[root@mahaoliang man]# 

4.2 yum安装nginx的默认目录详解
# nginx是一种web应用服务, yum -y install nginx
我们通过yum安装往往会找不到默认的配置文件,文件目录等
[root@mahaoliang man]# nginx -v
nginx version: nginx/1.22.0

# 我们先通过yum install nginx 安装好这个服务
[root@mahaoliang man]# ps -aux|grep nginx
root      17102  0.0  0.1  49176  2040 ?        Ss   05:39   0:00 nginx: master process nginx
nginx     19086  0.0  0.1  49580  2032 ?        S    10:25   0:00 nginx: worker process
nginx     19087  0.0  0.1  49580  2032 ?        S    10:25   0:00 nginx: worker process
root      20784  0.0  0.0 112824   980 pts/0    S+   10:54   0:00 grep --color=auto nginx

# nginx的配置文件在/etc/nginx/nginx.conf
[root@mahaoliang man]# cat /etc/nginx/nginx.conf 
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

# 自定义的配置文件放在/etc/nginx/conf.d
[root@mahaoliang man]# cd /etc/nginx/conf.d/
[root@mahaoliang conf.d]# ls
default.conf
[root@mahaoliang conf.d]# pwd
/etc/nginx/conf.d

# 项目文件存放在/usr/share/nginx/html/
[root@mahaoliang conf.d]# cd /usr/share/nginx/html/
[root@mahaoliang html]# ls
50x.html                                  回忆54C72DB6112F10616C4886D363E3AB4C.png
index.html                                回忆826351EFBA24FB220D75C541A4535A88.png
回忆137AACAFE81AB701E45A23A29994C609.png  回忆B09D801DEF543F83C2966080477A692F.png
回忆2C12B075EA65149739F89D5BC8898F3F.png
[root@mahaoliang html]# pwd
/usr/share/nginx/html

# 日志文件存放在/var/log/nginx/
[root@mahaoliang html]# cd /var/log/nginx
[root@mahaoliang nginx]# ls
access.log              access.log-20220612  error.log-20220602.gz  error.log-20220610.gz  error.log-20220612
access.log-20220611.gz  error.log            error.log-20220609.gz  error.log-20220611.gz
[root@mahaoliang nginx]# pwd
/var/log/nginx

# 还有一些其他的安装文件都在/etc/nginx 
[root@mahaoliang nginx]# cd /etc/nginx
[root@mahaoliang nginx]# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params
[root@mahaoliang nginx]# pwd
/etc/nginx
[root@mahaoliang nginx]# 

五. rpm包下载来源分析

5.1 rpm是什么?
red hat package manager 
红帽公司出品的软件包管理工具
rpm包管理机制的系统:
	centos,redhat,opensuse,oracle linux ,fedora等  --》红帽系
	zziplib-0.13.68-8.el8.x86_64.rpm
	enterprise linux 8
	x86_64 -->64
	i686 -->32
	
    yelp-libs-3.28.1-3.el8.i686.rpm
    yelp-libs-3.28.1-3.el8.x86_64.rpm

deb包管理机制的系统:
	debian,Ubuntu
	
    zfsutils-linux_0.8.3-1ubuntu12_amd64.deb
5.2 rpm包是什么?
其实就redhat系的linux系统里的软件包
是别人制作好的可以直接安装使用的软件包
类似于windows里的.exe
来源:source code 源码--打包制作或第三方制作
Source RPM  : openssh-8.0p1-5.el8.src.rpm  -->里面有源码的rpm包 --》不能直接安装使用,安装的过程其实是解压src.rpm包得到软件的源代码
包含了源码的rpm包
5.3 哪里可以下载或者获得rpm包?
来源:
1、镜像文件 --centos或者redhat公司制作
    使用:挂载镜像:
	请将镜像文件放入光驱
	使用mount命令挂载
          mount /dev/cdrom  /mnt
	使用镜像文件
2、官方网站
3、第三方平台 
http://www.rpmfind.net
4、自己制作

六. yum软件安装的相关问题

6.1仓库文件目录:
藏宝图-->指引yum去哪里下载软件包
[root@mahaoliang nginx]# cd /etc/yum.repos.d
[root@mahaoliang yum.repos.d]# ls
CentOS-Base.repo       CentOS-fasttrack.repo  CentOS-Vault.repo          epel-testing.repo
CentOS-CR.repo         CentOS-Media.repo      CentOS-x86_64-kernel.repo  nginx.repo
CentOS-Debuginfo.repo  CentOS-Sources.repo    epel.repo
[root@mahaoliang yum.repos.d]# pwd
/etc/yum.repos.d
[root@mahaoliang yum.repos.d]# 

如何获得仓库文件?
# 自己创建
如:grafana
# 下载rpm包安装
如:mysql
# 下载repo文件
如:docker
6.2 缓存目录
    缓存目录:/var/cache/yum/x86_64/7,yum下载软件包前加载元数据到缓存目录,已解决软件的依赖关系。
	元数据:解决依赖关系的数据,有哪些软件,每个软件依赖哪些软件等;
	元数据的位置:/var/cache/dnf  --》存放的是各个源解决依赖关系和软件信息的元数据
    yum clean all  -->清除缓存的元数据
6.3 源
源:
	1.官方源
		centos官方
		某个软件的官方
			如:nginx
	2.第3方
		epel-release 源
			 yum install epel-release
				 /etc/yum.repos.d/epel.repo
			 安装glances
	3.可以自己制作源

如何禁用一个源?
1.enabled=0
2.删除repo文件
3.修改repo文件的后缀名
4.sudo yum-config-manager --enable docker-ce-nightly
 yum-config-manager --disable docker-ce-nightly

七. 编译安装

7.1 编译安装优势和缺点
1.为什么需要编译安装?
  根据需求来定制
	    指定安装路径
		某些模块的功能开启或者禁用
			负载均衡
			web
			mail
			https支持
			等
		为了业务的最优配置
2.编译安装的优势?
	自定义
	安全
		最新版本的软件
3.缺点
	速度稍微慢
		步骤多
7.2 nginx编译安装实例
0.下载,然后解压
[root@localhost lianxi]# curl -O https://nginx.org/download/nginx-1.21.6.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1048k  100 1048k    0     0   225k      0  0:00:04  0:00:04 --:--:--  225k
[root@localhost lianxi]#

[root@localhost lianxi]# tar xf  nginx-1.21.6.tar.gz
[root@localhost lianxi]# ls
hello  hello.c  nginx-1.21.6  nginx-1.21.6.tar.gz
[root@localhost lianxi]# cd nginx-1.21.6
[root@localhost nginx-1.21.6]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost nginx-1.21.6]# cd src/
[root@localhost src]# ls
core  event  http  mail  misc  os  stream
[root@localhost src]# cd core/
[root@localhost core]# ls
nginx.c           ngx_crc32.c  ngx_list.c             ngx_palloc.c          ngx_rbtree.h    ngx_spinlock.c
nginx.h           ngx_crc32.h  ngx_list.h             ngx_palloc.h          ngx_regex.c     ngx_string.c
ngx_array.c       ngx_crc.h    ngx_log.c              ngx_parse.c           ngx_regex.h     ngx_string.h
ngx_array.h       ngx_crypt.c  ngx_log.h              ngx_parse.h           ngx_resolver.c  ngx_syslog.c
ngx_buf.c         ngx_crypt.h  ngx_md5.c              ngx_parse_time.c      ngx_resolver.h  ngx_syslog.h
ngx_buf.h         ngx_cycle.c  ngx_md5.h              ngx_parse_time.h      ngx_rwlock.c    ngx_thread_pool.c
ngx_conf_file.c   ngx_cycle.h  ngx_module.c           ngx_proxy_protocol.c  ngx_rwlock.h    ngx_thread_pool.h
ngx_conf_file.h   ngx_file.c   ngx_module.h           ngx_proxy_protocol.h  ngx_sha1.c      ngx_times.c
ngx_config.h      ngx_file.h   ngx_murmurhash.c       ngx_queue.c           ngx_sha1.h      ngx_times.h
ngx_connection.c  ngx_hash.c   ngx_murmurhash.h       ngx_queue.h           ngx_shmtx.c
ngx_connection.h  ngx_hash.h   ngx_open_file_cache.c  ngx_radix_tree.c      ngx_shmtx.h
ngx_core.h        ngx_inet.c   ngx_open_file_cache.h  ngx_radix_tree.h      ngx_slab.c
ngx_cpuinfo.c     ngx_inet.h   ngx_output_chain.c     ngx_rbtree.c          ngx_slab.h

1.编译前的配置
[root@localhost core]#cd  /lianxi/ nginx-1.21.6
[root@localhost nginx-1.21.6]# ./configure --prefix=/usr/local/wangshuai
--prefix=/usr/local/wangshuai 指定nginx的安装路径
./configure  --prefix=/usr/local/nginx
收集配置安装的参数
	--with-http_ssl_module
			启用
	--without-pcre2
			禁用
	编译前的配置,生成Makefile的文件
		指导make工作的配置文件
2. make 编译
	将c语言编写的代码make   --》背后就是调用gcc 按照我们编译前的配置makefile 里的内容去编译
	将源码编译成二进制文件编译成二进制文件
	gcc  --linux中的c语言编译器
	make -j 2
		启用2个进程去编译安装
			速度快
3. make install
	将编译好的二进制文件拷贝指定的安装目录下
4.修改path变量
将nginx服务程序所在的目录加入到PATH中
[root@localhost sbin]# PATH=/usr/local/wangshuai/sbin/:$PATH
[root@localhost sbin]# which nginx
/usr/local/wangshuai/sbin/nginx
5.启动
 直接在程序文件下启动:
[root@localhost nginx-1.21.6]# cd /usr/local/wangshuai/
[root@localhost wangshuai]# ls
conf  html  logs  sbin
[root@localhost wangshuai]# cd sbin/
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ./nginx

加入PATH后敲下nginx启动
[root@localhost sbin]# PATH=/usr/local/wangshuai/sbin/:$PATH
[root@localhost sbin]# which nginx
/usr/local/wangshuai/sbin/nginx
[root@localhost sbin]# nginx

6.如何知道nginx是否启动?
	看进程
		ps aux|grep nginx
		pidof nginx
	看端口
		ss  -anplut|grep nginx
			80
	直接访问 --网页访问
	看日志
	启动的命令 --nginx
	停止的命令 --nginx  -s  stop
	
7.[root@localhost sbin]# service firewalld stop  关闭防火墙
在使用浏览器去访问

八. 多任务模块及一键安装脚本的实现

8.1 多任务模块
1.很多模块的开启
2.多个编译安装的nginx,是否共存?
	如果都想使用80端口,只能启动一个nginx
	如果占用不同的端口,都可以启动,修改主配置文件,占用不同的端口 listen 

8.2 一键安装nginx脚本
one_key_install_nginx.sh
[root@mahaoliang 0612]# cat one_key_install_nginx.sh 
#version:1.21.6
#!/bin/bash

#install nginx
#version:1.21.6
#author: cali
#mail: 695811769@qq.com
#company: sanchuang
#time:  2022-06-10

#create user chenyulin
useradd -s /sbin/nologin chenyulin # 创建用户

#download nginx
mkdir -p /lianxi/chenyulin  # 下载nginx压缩包
cd /lianxi/chenyulin
curl -O https://nginx.org/download/nginx-1.21.6.tar.gz

#uncompress nginx source
tar xf nginx-1.21.6.tar.gz # 解压压缩包
cd nginx-1.21.6

#resolution dependency
yum  install pcre2 pcre2-devel zlib zlib-devel openssl  openssl-devel -y # 软件安装依赖解决

#configure
./configure --prefix=/usr/local/chenyulin  --user=chenyulin --with-threads  --with-http_ssl_module --with-http_v2_module # 安装前的配置工作

#make
make -j 2 # 编译
#install
make install # 安装
#path variable 
PATH=/usr/local/chenyulin/sbin/:$PATH # 添加路径到环境变量PATH
echo 'PATH=/usr/local/chenyulin/sbin/:$PATH'  >>/root/.bashrc # 设置开机自启nginx

#start nginx

if  pidof nginx &>/dev/null ;then
	echo "nginx is running"
	#kill old nginx process
	killall -9 nginx # 杀死之前的nginx进程
	#start new nginx process
	nginx # 启动新的进程
else
	nginx
fi
[root@mahaoliang 0612]# 

# 安装成功后自动打开编译安装的nginx和手动开启yum安装的nginx:
[root@mahaoliang 0612]# ps aux|grep nginx
root      32803  0.0  0.0  48656  1168 ?        Ss   13:58   0:00 nginx: master pro
chenyul+  32805  0.0  0.1  49116  1932 ?        S    13:58   0:00 nginx: worker pro
root      33071  0.0  0.0 112824   980 pts/0    S+   14:03   0:00 grep --color=auto
[root@mahaoliang 0612]# nginx
[root@mahaoliang 0612]# ps aux|grep nginx
root      32803  0.0  0.0  48656  1168 ?        Ss   13:58   0:00 nginx: master pro
chenyul+  32805  0.0  0.1  49116  1932 ?        S    13:58   0:00 nginx: worker pro
root      33099  0.0  0.0  49056  1152 ?        Ss   14:04   0:00 nginx: master pro
nginx     33100  0.0  0.1  49444  1884 ?        S    14:04   0:00 nginx: worker pro
nginx     33101  0.0  0.1  49444  1892 ?        S    14:04   0:00 nginx: worker pro
root      33105  0.0  0.0 112824   980 pts/0    S+   14:04   0:00 grep --color=auto
[root@mahaoliang 0612]# lsof -i:80
COMMAND   PID      USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   32803      root    6u  IPv4 161515      0t0  TCP *:http (LISTEN)
nginx   32805 chenyulin    6u  IPv4 161515      0t0  TCP *:http (LISTEN)
[root@mahaoliang 0612]# lsof -i:721
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   33099  root    6u  IPv4 161659      0t0  TCP *:721 (LISTEN)
nginx   33100 nginx    6u  IPv4 161659      0t0  TCP *:721 (LISTEN)
nginx   33101 nginx    6u  IPv4 161659      0t0  TCP *:721 (LISTEN)
[root@mahaoliang 0612]# cd /usr/local/chenyulin/
[root@mahaoliang chenyulin]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp    
conf              html          proxy_temp  scgi_temp
conf 存放nginx的配置文件 configure
html 存放网站的网页文件
logs 存放日志的
sbin 存放nginx的二进制文件
[root@mahaoliang chenyulin]# cd html/
[root@mahaoliang html]# ls
50x.html  index.html
[root@mahaoliang html]# vim index.html 

# 查看ip地址 通过网页进行连接 
[root@mahaoliang html]# ip a
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
    link/ether 00:0c:29:b0:bc:56 brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.187/24 brd 192.168.3.255 scope global noprefixroute dynamic ens33
       valid_lft 1745sec preferred_lft 1745sec
    inet6 fe80::63cb:270:df20:f427/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@mahaoliang html]# 

ss查看本机上开启的端口
[root@localhost sbin]# ss -anplut|grep 80
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=8464,fd=6),("nginx",pid=8463,fd=6))
[root@localhost sbin]#

[root@mahaoliang html]# ss -anplut|grep 80
udp    UNCONN     0      0         *:68                    *:*                   users:(("dhclient",pid=17980,fd=6))
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=32805,fd=6),("nginx",pid=32803,fd=6))
[root@mahaoliang html]# ss -anplut|grep 721
tcp    LISTEN     0      128       *:721                   *:*                   users:(("nginx",pid=33101,fd=6),("nginx",pid=33100,fd=6),("nginx",pid=33099,fd=6))
[root@mahaoliang html]# 

1.停止Nginx服务的四种方法
从容停止服务
这种方法较stop相比就比较温和一些了,需要进程完成当前工作后再停止。
nginx -s quit
立即停止服务
这种方法比较强硬,无论进程是否在工作,都直接停止进程。
nginx -s stop
systemctl 停止
systemctl属于Linux命令
systemctl stop nginx.service
killall 方法杀死进程
直接杀死进程,在上面无效的情况下使用,态度强硬,简单粗暴!
killall nginx

2.启动Nginx
nginx直接启动
nginx
systemctl命令启动
systemctl start nginx.service

3.查看启动后记录
ps aux | grep nginx

4.重启Nginx服务
systemctl restart nginx.service

5.重新载入配置文件
当有系统配置文件有修改,用此命令,建议不要停止再重启,以防报错!
nginx -s reload

6.查看端口号
netstat -tlnp

九. 二进制安装

二进制安装
	到官方网站下载源码包解压后,直接使用
	里面的程序都是一件编译好的可执行的二进制文件
	非常快捷方便
	prometheus和MySQL

十. 各种安装方式的形象描述

比喻
rpm和yum
	相当于到专卖店买衣服,款式,颜色,面料等都不可以修改了,喜欢,直接带走
		速度快,安装路径都是固定了的,不能修改,部分功能也许没有开启
源码编译安装
	到裁缝店去定制衣服,颜色,面料,款式都可以自己决定,可以选择定制
		时间长,好处功能完全可以自己定制
二进制安装
	解压后就可以使用
		速度快
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值