源码安装

1.源码包分类

源码格式(需要编译安装)

二进制格式(解压后可以直接使用)

2.源码包安装,常见三步
编译需要,编译环境,开发环境,开发库,开发工具
常用编译环境,c , c++ ,perl ,java ,python  5 种
c环境的编译器:gcc   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为文件名

3.编译安装注意事项

环境变量

如果安装时不是使用的默认路径,则必须要修改PATH环境变量,以能够识别此程序的二进制文件路径;
修改/etc/profile文件或在/etc/profile.d/目录建立一个以.sh为后缀的文件,在里面定义export PATH=$PATH:/path/to/somewhere

库文件

默认情况下,系统搜索库文件的路径只有/lib,/usr/lib
增添额外库文件搜索路径方法:
在/etc/ld.so.conf.d/中创建以.conf为后缀名的文件,而后把要增添的路径直接写至此文件中。此时库文件增添的搜索路径重启后有效,若要使用增添的路径立即生效则要使用ldconfig命令
ldconfig:通知系统重新搜索库文件

头文件

头文件:输出给系统
默认:系统在/usr/include中找头文件,若要增添头文件搜索路径,使用链接进行

man文件路径

man文件路径:安装在--prefix指定的目录下的man目录
默认:系统在/usr/share/man中找man文件。此时因为编译安装的时候不是安装到默认路径下,如果要查找man文件则可以使用以下两种方法:
man -M /path/to/man_dir command
在/etc/man_db.conf文件中添加一条MANPATH
一.编译安装nginx
1.基础环境准备工作
[root@RedHat8 soft]# yum -y install wget vim gcc make
Updating Subscription Management repositories.
Unable to read consumer identity
......
Installed:
  wget-1.19.5-10.el8.x86_64    vim-enhanced-2:8.0.1763-16.el8.x86_64                 vim-filesystem-2:8.0.1763-16.el8.noarch
  gcc-8.5.0-4.el8_5.x86_64     make-1:4.2.1-10.el8.x86_64
  .......
Complete!
2.下载源码包
[root@RedHat8 soft]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
......
nginx-1.24.0.tar.gz      100%[=================================>]   1.06M   308KB/s    in 3.5s    

2023-08-28 15:06:22 (308 KB/s) - ‘nginx-1.24.0.tar.gz’ saved [1112471/1112471]
3.解压源码包,并进入相应目录
[root@RedHat8 soft]# ll
total 1088
-rw-r--r--. 1 root root 1112471 Apr 12 00:04 nginx-1.24.0.tar.gz
[root@RedHat8 soft]# tar xf nginx-1.24.0.tar.gz 
[root@RedHat8 soft]# cd nginx-1.24.0
[root@RedHat8 nginx-1.24.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
4.配置相关的选项,并生成Makefile
使用 ./configure --help 命令查看可以使用的选项
--prefix=PREFIX 这个选项的意思是定义软件包安装到哪里
建议,源码包都是安装在/opt/目录下
[root@RedHat8 nginx-1.24.0]# ./configure --prefix=/opt/nginx-1.24.0
........
./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.

报错,缺少pcre-devel
[root@RedHat8 nginx-1.24.0]# yum install -y pcre-devel
Updating Subscription Management repositories.
Unable to read consumer identity
............
Installed:
  pcre-cpp-8.42-6.el8.x86_64      pcre-devel-8.42-6.el8.x86_64    pcre-utf16-8.42-6.el8.x86_64   
  pcre-utf32-8.42-6.el8.x86_64   
Complete!

[root@RedHat8 nginx-1.24.0]# ./configure --prefix=/opt/nginx-1.24.0
.............
./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.

报错,缺少zlib-devel
[root@RedHat8 nginx-1.24.0]# yum -y install zlib-devel
..................
Installed:
  zlib-devel-1.2.11-17.el8.x86_64                                                 
Complete!

configure脚本 指定编译参数 软件安装位置为 /opt
[root@RedHat8 nginx-1.24.0]# ./configure --prefix=/opt/nginx-1.24.0
验证是否成功 0为成功
[root@RedHat8 nginx-1.24.0]# echo $?
0
生成Makefile
[root@RedHat8 nginx-1.24.0]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
5.编译并安装
[root@RedHat8 nginx-1.24.0]# make
[root@RedHat8 nginx-1.24.0]# make install
[root@RedHat8 nginx-1.24.0]# echo $?
0
[root@RedHat8 nginx-1.24.0]# ls /opt
nginx-1.24.0

6.建立软链接
[root@RedHat8 opt]# ll
total 0
drwxr-xr-x. 11 root root 151 Aug 28 15:44 nginx-1.24.0
[root@RedHat8 opt]# ln -s /opt/nginx-1.24.0 /opt/nginx
[root@RedHat8 opt]# ll
total 0
lrwxrwxrwx.  1 root root  17 Aug 28 15:56 nginx -> /opt/nginx-1.24.0
drwxr-xr-x. 11 root root 151 Aug 28 15:44 nginx-1.24.0
7.修改PATH环境变量
[root@RedHat8 sbin]# pwd
/opt/nginx/sbin
[root@RedHat8 sbin]# echo 'export PATH=/opt/nginx/sbin:$PATH' >> /etc/profile
[root@RedHat8 ~]# bash
8.关闭防火墙 selinux
[root@RedHat8 sbin]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RedHat8 sbin]# setenforce 0
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐