|
1、概述
linux中的软件安装一共有三种,相对于Windows的简易操作,对于linux的使用者来说,软件安装也是一门学问。linux中的软件包一共有这么几种:
1)、RPM:即RedHat Package Manager(RPM软件包管理器)的简写,执行二进制文件的安装,一个完整的RPM包应该包括可执行文件,man手册,库文件,头文件。RPM软件包的存储格式如下:
其中应用平台包括32位系统的i386,i586,i686,64位系统的x86-64。
2)、tar:这是一种压缩后的归档包,常见的有gzip和bzip2压缩的包,格式如下:
2、linux中的软件安装方式
1)、RPM:使用RPM软件管理工具安装应用软件,优点是简单易操作,可以快速的安装需求的软件,并且能够进行软件包的相关查询。但是缺点也很明显,就是无法自主解决软件包的依赖关系,对于一些较复杂的软件安装,使用这种方式无疑会增加管理员的负担,而且显得繁琐。
2)、yum:全称为 (Yellow dog Updater, Modified),是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装。
3)、源代码:通常在安装linux操作系统的光盘上都有集成一些基础的软件包,不过光盘上的软件包的版本通常都比较低,企业环境或者一些大型服务站需要最新的可以控制管理的软件程序,只能去下载最新的软件包,这个时候就只能使用源码安装了。源码安装的优点是使用的软件包一般都是最新的版本,并且在安装过程中可以自定义设置,方便管理。
3、案例实施
系统环境:CentOS6.5-32bit
1)、rpm安装的基本操作
安装前检测软件包是否已被安装过:
1
| [iyunv@fcy Packages]# rpm -qa httpd #-q表示查询 -a表示所有
|
如果安装过则会这样显示:
1
2
| [iyunv@fcy Packages]# rpm -qa man
man-1.6f-32.el6.i686
|
如果安装前想知道即将安装的软件包都包含什么内容,可以如下查询:
1
2
3
4
5
6
7
8
9
10
11
12
13
| [iyunv@fcy Packages]# rpm -qlp httpd-tools-2.2.15-29.el6.centos.i686.rpm #-p表示提供的服务 -l表示list列表
/usr/bin/ab
/usr/bin/htdbm
/usr/bin/htdigest
/usr/bin/htpasswd
/usr/bin/logresolve
/usr/share/doc/httpd-tools-2.2.15
/usr/share/doc/httpd-tools-2.2.15/LICENSE
/usr/share/man/man1/ab.1.gz
/usr/share/man/man1/htdbm.1.gz
/usr/share/man/man1/htdigest.1.gz
/usr/share/man/man1/htpasswd.1.gz
/usr/share/man/man1/logresolve.1.gz
|
安装rpm包(-i表示install,-v表示详细信息,-h表示显示进度条):
1
2
3
| [iyunv@fcy Packages]# rpm -ivh setup-2.8.14-20.el6_4.1.noarch.rpm
Preparing... ########################################### [100%]
package setup-2.8.14-20.el6_4.1.noarch is already installe
|
如果想查询安装过的软件包都提供哪些服务,可以这样查询(注意与没安装过的包的查询方式不同,安装过的包想要查询只需要填上软件名称,而没有安装过的软件查询则需要输入完整的软件包名称):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| [iyunv@fcy Packages]# rpm -ql man
/etc/cron.daily/makewhatis.cron
/etc/man.config
/usr/bin/apropos
/usr/bin/man
/usr/bin/man2html
/usr/bin/manpath
/usr/bin/whatis
/usr/libexec/man-cmp.sh
/usr/sbin/makewhatis
/usr/share/doc/man-1.6f
/usr/share/doc/man-1.6f/COPYING
/usr/share/doc/man-1.6f/README
/usr/share/locale/bg/LC_MESSAGES/man
/usr/share/locale/cs/LC_MESSAGES/man
......(省略若干)
|
卸载软件包:
1
| [iyunv@fcy Packages]# rpm -e man #-e表示卸载软件
|
升级软件:
1
2
3
4
5
6
7
| rpm -Uvh 软件包名称 #-U表示update
#这种方式可以执行两种功能,即升级和安装,如果你的系统没有安装这个包,则先执行安装
[iyunv@fcy Packages]# rpm -Uvh man-pages-3.22-20.el6.noarch.rpm
Preparing... ########################################### [100%]
1:man-pages ########################################### [100%]
rpm -Fvh 软件包名称 #-F表示fresh
#这种方式只执行升级功能
|
如果两个软件包在安装时由于文件名称产生冲突,可以用如下命令:
1
| rpm -ivh --replacefiles 软件包名称
|
如果两个软件包产生冲突,可以使用如下命令:
1
| rpm -ivh --replacepkgs 软件包名称
|
如果想查询软件包是否改变,可以启用校验功能:
1
| [iyunv@fcy Packages]# rpm -V httpd #-V表示启用校验 当没有输出信息时说明软件包无变化
|
当修改过httpd的配置文件内容后,再校验:
1
2
3
| [iyunv@fcy Packages]# echo "#test" >>/etc/httpd/conf/httpd.conf
[iyunv@fcy Packages]# rpm -V httpd
S.5....T. c /etc/httpd/conf/httpd.conf
|
可以看到,校验结果马上不同了,其中常见的值分别代表如下:
c-->表示config文件发生改变;
d-->表示doc文件发生改变;
S-->表示文件大小发生变化;
5-->表示MD5检验值发生变化;
U-->表示所属用户发生变化;
T-->表示时间发生变化;
G-->表示所属组发生变化。
2)、yum安装的基本操作
(1)本地yum源安装
挂载光盘并查看
1
2
3
4
5
6
7
8
| [iyunv@fcy ~]# mount /dev/cdrom /media/cdrom #挂载光盘到/media/cdrom
mount: block device /dev/sr0 is write-protected, mounting read-only
[iyunv@fcy ~]# df -h #以易读的方式显示挂载信息
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 15G 1.1G 13G 8% /
tmpfs 122M 0 122M 0% /dev/shm
/dev/sda1 194M 25M 160M 14% /boot
/dev/sr0 3.6G 3.6G 0 100% /media/cdrom #光盘挂载信息
|
查看yum配置文件/etc/yum.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
| [iyunv@fcy ~]# cat /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever #表示yum安装时下载的安装包的缓存目录
keepcache=0 #表示缓存文件存留的时间,等于0即为安装完成后就删除
debuglevel=2 #调试的等级
logfile=/var/log/yum.log #日志文件的存放位置
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=16&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
|
查看yum源配置文件/etc/yum.repos.d/
1
2
3
4
5
6
| [iyunv@fcy ~]# ll /etc/yum.repos.d/
total 16
-rw-r--r--. 1 root root 1926 Nov 27 2013 CentOS-Base.repo #一般用作网络源
-rw-r--r--. 1 root root 638 Nov 27 2013 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 630 Nov 27 2013 CentOS-Media.repo #一般用作本地源
-rw-r--r--. 1 root root 3664 Nov 27 2013 CentOS-Vault.repo
|
配置yum本地源配置文件/etc/yum.repos.d/CentOS-Media.repo(先移除网络源文件)
1
2
3
4
5
6
7
8
| [c6-media] #repo ID 即源名称
name=CentOS-$releasever - Media #对repo ID的描述
baseurl=/
file:///media/cdrom/ #yum源路径,填入光盘的挂载点
file:///media/cdrecorder/
gpgcheck=1 #gpg校验,等于1是开启状态
enabled=1 #yum源启用状态,等于1是启用状态
gpgkey= #gpg校验的文件位置
|
其中对于配置文件中的baseurl,有如下几种路径:
file表示本地文件,http表示web文件,ftp表示ftp服务器
配置好本地yum源后,需要使用命令清除yum缓存:
1
2
3
4
5
| [iyunv@fcy ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base c6-media extras updates
Cleaning up Everything
Cleaning up list of fastest mirrors
|
查看启用的yum源及可用文件:
1
2
3
4
5
6
7
| [iyunv@fcy yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* c6-media:
repo id repo name status
c6-media CentOS-6 - Media 4802
repolist: 4802
|
查看安装包信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| [iyunv@fcy ~]# yum info httpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* c6-media:
Installed Packages #installed表示已安装,未安装显示为available
Name : httpd
Arch : i686
Version : 2.2.15
Release : 29.el6.centos
Size : 2.8 M
Repo : installed
From repo : c6-media
Summary : Apache HTTP Server
URL : http://httpd.apache.org/
License : ASL 2.0
Description : The Apache HTTP Server is a powerful, efficient, and extensible
: web server.
|
查看所有的和已安装的包:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| yum list all #由于信息过多,不展示
[iyunv@fcy ~]# yum list installed
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* c6-media:
Installed Packages
ConsoleKit.i686 0.4.1-3.el6 @anaconda-CentOS-201311271240.i386/6.5
ConsoleKit-libs.i686 0.4.1-3.el6 @anaconda-CentOS-201311271240.i386/6.5
ConsoleKit-x11.i686 0.4.1-3.el6 @anaconda-CentOS-201311271240.i386/6.5
GConf2.i686 2.28.0-6.el6 @anaconda-CentOS-201311271240.i386/6.5
MAKEDEV.i686 3.24-6.el6 @anaconda-CentOS-201311271240.i386/6.5
ORBit2.i686 2.14.17-3.2.el6_3 @anaconda-CentOS-201311271240.i386/6.5
acl.i686 2.2.49-6.el6 @anaconda-CentOS-201311271240.i386/6.5
aic94xx-firmware.noarch 30-2.el6 @anaconda-CentOS-201311271240.i386/6.5
alsa-lib.i686 1.0.22-3.el6 @anaconda-CentOS-201311271240.i386/6.5
......(省略)
|
查看可用的组安装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| [iyunv@fcy ~]# yum grouplist
Loaded plugins: fastestmirror
Setting up Group Process
Loading mirror speeds from cached hostfile
* c6-media:
Installed Groups:
E-mail server
Fonts
General Purpose Desktop
Graphical Administration Tools
Input Methods
Legacy X Window System compatibility
Perl Support
Security Tools
Web Server
Installed Language Groups:
Chinese Support [zh]
Available Groups:
Additional Development
Backup Client
Backup Server
......(省略)
|
安装组软件:
1
2
| [iyunv@fcy ~]# yum groupinstall "Desktop Platform Development" -y #-y表示不显示交互信息
......(过程略)
|
安装软件包:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| [iyunv@fcy yum.repos.d]# yum install mysql #安装mysql
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* c6-media: #从repo ID为c6-media的源上查找软件包
Setting up Install Process
Resolving Dependencies #自动处理依赖关系
--> Running transaction check
---> Package mysql.i686 0:5.1.71-1.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================
Package Arch Version Repository Size
=========================================================================================
Installing:
mysql i686 5.1.71-1.el6 c6-media 903 k
Transaction Summary
=========================================================================================
Install 1 Package(s)
Total download size: 903 k
Installed size: 2.3 M
......(过程略)
|
软件包卸载:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| [iyunv@fcy yum.repos.d]# yum remove man #卸载软件包man
Loaded plugins: fastestmirror
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package man.i686 0:1.6f-32.el6 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================
Package Arch Version Repository Size
=========================================================================================
Removing:
man i686 1.6f-32.el6 @c6-media 377 k
Transaction Summary
=========================================================================================
Remove 1 Package(s)
|
组软件包卸载:
本地软件包安装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| [iyunv@fcy Packages]# yum localinstall man-1.6f-32.el6.i686.rpm --nogpgcheck -y
#localhostinstall表示本地文件安装 --nogpgcheck表示不进行软件包校验
Loaded plugins: fastestmirror
Setting up Local Package Process
Examining man-1.6f-32.el6.i686.rpm: man-1.6f-32.el6.i686
Marking man-1.6f-32.el6.i686.rpm to be installed
Loading mirror speeds from cached hostfile
* c6-media:
Resolving Dependencies
--> Running transaction check
---> Package man.i686 0:1.6f-32.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================
Package Arch Version Repository Size
......(过程略)
Installed:
man.i686 0:1.6f-32.el6
Complete!
|
3)、源代码安装(apache服务)
拷贝所需的源代码到/root/apache/
1
2
3
4
5
| [iyunv@fcy ~]# ll /root/apache/
total 6652
-rw-r--r--. 1 root root 982243 Jul 18 03:44 apr-1.4.6.tar.gz #增强Apache可移植性的工具
-rw-r--r--. 1 root root 829779 Jul 18 03:44 apr-util-1.5.1.tar.gz #apr的工具包
-rw-r--r--. 1 root root 4994460 Jul 18 03:44 httpd-2.4.9.tar.bz2 #Apache的源代码
|
将源代码解压缩到/usr/local/src
1
2
3
4
5
6
| [iyunv@fcy apache]# tar -zxvf apr-1.4.6.tar.gz -C /usr/local/src/ #-C表示指定解压目录
apr-1.4.6/
apr-1.4.6/shmem/
apr-1.4.6/shmem/win32/
apr-1.4.6/shmem/win32/shm.c
......(过程略)
|
1
2
3
4
5
6
7
| [iyunv@fcy apache]# tar -zxvf apr-util-1.5.1.tar.gz -C /usr/local/src/
apr-util-1.5.1/
apr-util-1.5.1/crypto/
apr-util-1.5.1/crypto/apr_passwd.c
apr-util-1.5.1/crypto/crypt_blowfish.h
apr-util-1.5.1/crypto/apr_crypto_nss.dsp
......(过程略)
|
1
2
3
4
5
6
7
8
9
| [iyunv@fcy apache]# tar -jxvf httpd-2.4.9.tar.bz2 -C /usr/local/src/
httpd-2.4.9/
httpd-2.4.9/.deps
httpd-2.4.9/.gdbinit
httpd-2.4.9/ABOUT_APACHE
httpd-2.4.9/acinclude.m4
httpd-2.4.9/ap.d
httpd-2.4.9/Apache-apr2.dsw
......(过程略)
|
安装源代码所需要的开发环境(包括“Development tools”,“Additional Development”,
“Desktop Platform Development”):
1
2
| [iyunv@fcy apache]# yum groupinstall "Development tools" -y
......(过程略)
|
由于httpd要依赖于apr和apr-util,所以先装apr:
1
2
3
| [iyunv@fcy apr-1.4.6]# ./configure --prefix=/usr/local/apr
#生成配置文件,并指定安装目录为/usr/local/apr
......(过程略)
|
1
2
| [iyunv@fcy apr-1.4.6]# make && make install #编译并安装apr
......(过程略)
|
源码安装apr-util:
1
2
| [iyunv@fcy apr-util-1.5.1]# ./configure --prefix=/usr/local/apr-util/ --with-apr=/usr/local/apr/bin/apr-1-config
#生成配置文件,并指定安装目录为/usr/local/apr-util,并且依赖于apr安装目录下的apr-1-config文件
|
1
| [iyunv@fcy apr-util-1.5.1]# make && make install #编译并安装
|
源码安装httpd:
1
2
3
| [iyunv@fcy httpd-2.4.9]# ./configure --prefix=/usr/local/apache/ --with-apr=/usr/local/apr/bin/apr-1-config --with-apr-util=/usr/local/apr-util
#到这里会报错,说缺少pcre工具,用yum安装pcre-devel即可
|
1
| [iyunv@fcy httpd-2.4.9]#make && make install #编译并安装
|
启动apache服务
1
2
3
4
5
6
| [iyunv@fcy httpd-2.4.9]# find / -name httpd #搜索apache的启动文件
/usr/local/src/httpd-2.4.9/httpd
/usr/local/apache/bin/httpd
[iyunv@fcy httpd-2.4.9]# /usr/local/apache/bin/httpd -k start
AH00557: httpd: apr_sockaddr_info_get() failed for fcy.com.cn
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
|
添加apache环境变量(/etc/profile)
刷新环境变量,并测试:
1
2
3
| [iyunv@fcy httpd-2.4.9]# source /etc/profile #执行脚本
[iyunv@fcy httpd-2.4.9]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin #已经添加到环境变量中
|
再启动Apache服务,此时由于变量路径已被添加到系统变量中,可以直接执行
1
2
3
4
| [iyunv@fcy httpd-2.4.9]# httpd -k start #不用再输入绝对路径也可以执行
AH00557: httpd: apr_sockaddr_info_get() failed for fcy.com.cn
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
httpd (pid 30667) already running
|
编写apache控制脚本(/etc/init.d/httpd):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| #!/bin/bash #指定脚本语言类型为shell
prog=/usr/local/apache/bin/apachectl #定义变量prog指向apache启动程序
lockfile=/var/lock/subsys/httpd #指定锁文件目录
start(){ #定义函数start
if [ -e $lockfile ]; then #判断锁文件是否存在
echo "the apache server is started" #如果存在则提示服务已启动
else
echo -n "the apache server is starting......" #否则提示启动过程
#echo的-n参数表示显示信息不换行
sleep 1 #进程等待一秒
$prog -k start &>/dev/null #将启动时的警告信息放进垃圾桶(/dev/null)
[ $? -eq 0 ] && echo -e [ "\033[32m OK \033[0m" ] && touch $lockfile || echo -e [ "\033[31m fail \033[0m" ]
#如果启动成功则显示OK并创建锁文件,否则输出fail
#echo的-e参数表示指定输出文本的颜色,引号内为颜色显示的格式,颜色数值在/etc/DIR_color
fi #函数结尾
}
stop(){
if [ ! -e $lockfile ]; then
echo "the apache server is stoped"
else
echo "the apache server is stoping......"
sleep 1
$prog -k stop &>/dev/null
[ $? -eq 0 ] && echo -e [ "\033[32m OK \033[0m" ] && rm -rf $lockfile || echo -e [ "\033[31m fail \033[0m" ]
fi
}
restart(){
stop
start
}
case "$1" in #表示启动命令的参数个数
start) #如果参数为start则调用start函数
start
;; #调用结束
stop) #如果参数为stop则调用stop函数
stop
;; #调用结束
restart) #如果参数为restart则先调用stop再调用start
stop
start
;; #调用结束
*) #如果输入的参数不在定义的函数中,则显示可调用的函数名称
echo "USAGE:{start|stop|restart}"
esac #脚本结束提示符
|
测试apache服务控制脚本
查看apache进程和端口号:
1
2
3
| [iyunv@fcy init.d]# netstat -tupln |grep httpd
#-t表示tcp -u表示udp -p表示给予协议 -l表示列表显示 -n表示已数字形式显示
tcp 0 0 :::80 :::* LISTEN 30952/httpd
|
|
|