源码安装管理

源码安装管理

源码包基本概述

在linux环境下面安装源码包是比较常见的,在早期运维管理工作当中,大部分软件都是通过源码安装的。那么安装一个源码包,是需要我们自己把源代码编译成二进制的可执行文件。

源码包的编译用到了linux系统里的编译器,通常源码包都是用C语言开发的,这也是因为C语言为linux上最标准的程序语言。Linux上的C语言编译器叫做gcc,利用它就可以把C语言变成可执行的二进制文件。所以如果你的机器上没有安装gcc就没有办法去编译源码。可以使用yum -y install gcc来完成安装。

源码包的好处

  • 自定义修改源代码
  • 定制需要的相关功能
  • 新版软件优先更新源码

源码包的获取

官方网站, 可以获得最新的软件包

Apache官方网站

Nginx官方网站

Mysql官方网站

例如在nginx官网上下载nginx安装包

  • 首先我们提前在windows上面进入nginx网站,找到下载,找打一个包右键,选择复制链接地址
  • 进入Linux系统,输入以下命令
[root@zhoulijie ~]# wget
  • 将复制的地址粘贴上去
[root@zhoulijie ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
  • 在当前目录就可以ls出我们下载下来的文件
[root@zhoulijie ~]# ls
anaconda-ks.cfg       nginx-1.14.2.tar.gz  模板  图片  下载  桌面
initial-setup-ks.cfg  公共                 视频  文档  音乐

源码包的分类

  • 源码格式(需要编译安装)
  • 二进制格式(解压后可以直接使用)

源码包的安装

编译需要编译环境,开发环境,开发库,开发工具。 常用的编译环境有c、c++、perl、java、python5种 c环境的编译器:gcc(GNU C Complier) c++环境的编译器:g++ make:c、c++的统一项目管理工具,编译时有可能调用gcc也有可能调用g++。使用makefile文件定义make按何种次序去编译源程序文件中的源程序

源码安装三部曲(常见):
第一步: ./configure(定制组件)

1.指定安装路径,例如 --prefix=/opt/nginx-1.12
2.启用或禁用某项功能, 例如 --enable-ssl
3.和其它软件关联,例如–with-pcre
4.检查安装环境,例如是否有编译器 gcc,是否满足软件的依赖需求
5.检测通过后生成Makefile文件

第二步: make

1.执行make命令进行编译, 可以使用-j指定CPU核心数进行编译
2.按Makefile文件进行编译, 编译成可执行二进制文件
3.生成各类模块和主程序

第三步: make install

1.按Makefile定义好的路径拷贝至安装目录中

上面介绍的源码三部曲不能百分百通用于所有源码包, 也就是说源码包的安装并非存在标准安装步骤,但是大部分源码安装都是类似的步骤

建议:
拿到源码包解压后,然后进入到目录找相关的帮助文档,通常会以INSTALL或者README为文件名

configure脚本的功能
  • 让用户选定编译特性
  • 检查编译环境是否符合程序编译的基本需要
源码包编译事例

下面我们就来实战一番

在linux中编译nginx 启动nginx服务

[root@zhoulijie ~]# tar -xf nginx-1.14.2.tar.gz
[root@zhoulijie ~]# cd nginx-1.14.2/
  • 源码安装三部曲

第一步: ./configure

[root@zhoulijie nginx-1.14.2]# ./configure
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

哦霍,完蛋 出征未捷身先死。那么往下看吧

// 解决方法
[root@zhoulijie nginx-1.14.2]# yum install -y gcc gcc-c++ make

再次执行

[root@zhoulijie nginx-1.14.2]# ./configure
........................省略一大片....................

./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.

下面来看解决办法

[root@zhoulijie nginx-1.14.2]# yum install -y pcre-devel

再次执行

[root@zhoulijie nginx-1.14.2]# ./configure
........................省略一大片....................

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

下面来看解决办法

[root@zhoulijie nginx-1.14.2]# yum -y install zlib-devel

再次执行

[root@zhoulijie nginx-1.14.2]# ./configure
........................省略一大片....................

  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/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"
  

出现这样就表示完成,其实还有一种错误,这里由于我可能安装openssl-devel

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL 
library into the system, or build the OpenSSL library statically
from the source with nginx by using --with-openssl=<path> option.

// 解决办法
yum install openssl-devel

第二步:make

[root@zhoulijie nginx-1.14.2]# make

第三步: make instal

[root@zhoulijie nginx-1.14.2]# make install

// 此时未知是否安装好,可以使用echo $?
[root@zhoulijie nginx-1.14.2]# echo $?
0
## 0代表没有错误 1代表错误
  • 启动nginx
[root@zhoulijie nginx-1.14.2]# ls
[root@zhoulijie nginx-1.14.2]# cd objs/
[root@zhoulijie objs]# ./nginx
[root@zhoulijie objs]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port     
LISTEN     0      128          *:111                      *:*
LISTEN     0      128          *:80                       *:*

## nginx 80端口

实验成果

编译安装注意事项
  • 如果安装时不是使用的默认路径,则必须要修改PATH环境变量,以能够识别此程序的二进制文件路径;
    • 修改/etc/profile文件或在/etc/profile.d/目录建立一个以.sh为后缀的文件,在里面定义export PATH=$PATH:/path/to/somewhere
  • 默认情况下,系统搜索库文件的路径只有/lib,/usr/lib
    • 增添额外库文件搜索路径方法:
      • 在/etc/ld.so.conf.d/中创建以.conf为后缀名的文件,而后把要增添的路径直接写至此文件中。此时库文件增添的搜索路径重启后有效,若要使用增添的路径立即生效则要使用ldconfig命令
    • ldconfig:通知系统重新搜索库文件
/etc/ld.so.conf和/etc/ls.so.conf.d/*.conf    //配置文件
/etc/ld.so.cache            //缓存文件
-v      //显示重新搜索库的过程
-p      //打印出系统启动时自动加载并缓存到内存中的可用库文件名及文件路径映射关系
  • 头文件:输出给系统
    • 默认:系统在/usr/include中找头文件,若要增添头文件搜索路径,使用链接进行
  • man文件路径:安装在–prefix指定的目录下的man目录
    • 默认:系统在/usr/share/man中找man文件。此时因为编译安装的时候不是安装到默认路径下,如果要查找man文件则可以使用以下两种方法:
      • man -M /path/to/man_dir command
      • 在/etc/man.config文件中添加一条MANPATH
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值