1、源码安装简介

       源码安装是通过软件开放者公布的程序源代码来自己编译安装软件包的一种方式。

             优点,可以自行选择自己所需要的模块,来定制自己所需要的软件特性。也适用于当新开发的程序没人编译打包的情况。当需要安装一个刚开发出来的软件包,而网络上又没有相应的打包过的rpm包的时候,就需要我们自行编译安装了


             缺点;源码安装有难度,不是每个人都适用。


2.程序运行所需要的工作环境以及程序的组成部分


      程序的运行需要该程序所依赖的库文件,因此安装程序的时候必须保证其所依赖的库存在。程序安装之后通常的是有二进制程序,库文件、配置文件、帮助文档等组成的。这些文件被放在计算机中指定的地方,可以自己存放位置。不过为了方便管理,一般建议存放位置遵循FHS规定存放。即二进制文件放置 二进制程序:/bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, /usr/local/sbin


   库文件放置:/lib, /usr/lib, /lib64, /usr/lib64, /usr/local/lib, /usr/local/lib64

   配置文件一般放置:/etc


3.源代码编译、连接、以及生成可执行程序的过程;

   通常所见到的源码都是用“c‘ 、‘c++’..等语言开发的。而一个项目工程往往是有多个分散的源代码文件组合成的,这样就给编译带来了难度。因此就有了autoconf,automake,这样的工具来给我们的编译安装过程带来变节。

   编译安装时,源码首先通过autoconf、和automake工具生成configure脚本、和makefile.in文件。然后makefile.in文件结合configure生成makefile文件。这样一来,make就可以通过其配置文件makefile来进行编译和连接了。而configure脚本此时就被用来检查系统环境是否符合编译需求。

4、编译安装步骤(我们以安装nginx举例说明)

     安装前请确定是否安装了必须的开发组包,如果系统为CentOS 5,常用的开发包组为“Development Tools”和“Development Libraries”。CentOS 6,常用的开发包组为“Development Tools”和“Server Platform Development”


    1.首先去获取源代码。

        可以通过www.SourceForge.net    www.github.com

     wwww.code.google.com  或者直接google搜索软件源码的名字就行。

[root@station57 ~]# wget http://nginx.org/download/nginx-1.4.5.tar.gz
--2014-02-25 06:51:40--  http://nginx.org/download/nginx-1.4.5.tar.gz
Resolving nginx.org... 206.251.255.63
Connecting to nginx.org|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 768728 (751K) [application/octet-stream]
Saving to: ?.ginx-1.4.5.tar.gz?
100%[========================================>] 768,728     39.7K/s   in 19s    
2014-02-25 06:52:00 (39.6 KB/s) - ?.ginx-1.4.5.tar.gz?.saved [768728/768728]


    2.拿到源代码后进行解压

[root@station57 ~]# tar -xf nginx-1.4.5.tar.gz
[root@station57 ~]# ls
nginx-1.4.5  nginx-1.4.5.tar.gz  repodata  script  scriptExpand  yum.sh


    3.进入到解压后的目录里

[root@station57 ~]# cd nginx-1.4.5
[root@station57 nginx-1.4.5]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README

    4.目录里有个README文件,一般先查看这个文件看看有什么说明

    5。查看readme文件后就开始执行configure脚本了,在执行脚本之前看一下帮助信息,一般信息里会有目录安装路径,组件的路径方法和相应模块。可以根据帮助信息定制自己的程序组件。

[root@station57 nginx-1.4.5]# ./configure --help
  --help                             print this message
  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --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 for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes
  --builddir=DIR                     set build directory
  --with-rtsig_module                enable rtsig module
  --with-select_module               enable select module
  --without-select_module            disable select module

   6.通过帮助文件,我们可以选择相应选项来安装,这里用--prefix=/usr/local/nginx 来定制程序路径。用--conf-path=/etc/nginx 来定制配置文件保存路径。

[root@station57 nginx-1.4.5]# ./configure --prefix=/usr/local/nginx

   7.configure脚本执行结果

   

[root@station57 nginx-1.4.5]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx
checking for OS
 + Linux 2.6.32-358.el6.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
checking for gcc -pipe switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for unistd.h ... found
checking for inttypes.h ... found
.....

wKioL1MN8l3hEmO1AAHb53xoeHc420.jpg

wKioL1MN8l3AZBe9AAGwgQPWgHM488.jpg

.....












    8.执行make编译

[root@station57 nginx-1.4.5]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.4.5'
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/os/unix -I objs \
 ....

   9.执行make install


 make install 的作用其实是将上一步中编译的文件cp到相应的位置,因此我们会看到类似下边的一堆cp命令。

   

[root@station57 nginx-1.4.5]# make install
make -f objs/Makefile install
make[1]: Entering directory `/root/nginx-1.4.5'
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 '/etc'    || mkdir -p '/etc'
cp conf/koi-win '/etc'
cp conf/koi-utf '/etc'
cp conf/win-utf '/etc'
test -f '/etc/mime.types'     || cp conf/mime.types '/etc'
...

    10.如果上面几步没有报错,就说明nginx安装成功,通过安装前后对比可以看出,编译之后,多了一个makefile和objs两个文件。


     图片wKioL1MN8t2wouGtAAB-OeXAEOg430.jpg


    11.直接去二进制安装目录下运行nginx二进制程序就可以把nginx启动了

          不过nginx默认是监听80端口,所以nginx启动之前应该确认把80端口关闭。有可能系统正在运行http服务,此时应把http服务停掉。service httpd stop

[root@station57 sbin]# /usr/local/nginx/sbin/nginx

   图片

wKioL1MN8wrARZuJAAIf0Sc5QNA806.jpg

wKiom1MN8zGD7TkXAABXdfIwpyA005.jpg


    12,因为是源码安装的,所以二进制程序路径没有在环境变量中定义,每次启动必须用绝对路径启动,这样就不方便了。因此我们需要讲nginx加入到服务脚本,让它通过service nginx start 方式来启动。这时我们就需要编辑一个服务脚本,把nginx路径加入到环境变量中,[root@station57 profile.d]# vim /etc/profile.d/nginx.sh


            PATH=/usr/local/nginx/sbin:$PATH

            export PATH