源码安装httpd以及源码安装的介绍

# 源码安装包管理

## 1. 源码包基本概述

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

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

## 2.源码包的安装

编译需要编译环境,开发环境,开发库,开发工具。
常用的编译环境有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定义好的路径拷贝至安装目录中

### 2.1源码包编译实例

##### 下面通过编译安装httpd来深入理解源码包安装(网站apache.org)

```

1.安装依赖包
[root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make vim wget
Complete!

2.创建系统用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache
[root@localhost ~]# id apache
uid=991(apache) gid=991(apache) groups=991(apache)

3.下载软件包(官网下apache.org)
[root@localhost ~]# wget https://downloads.apache.org/apr/apr-1.7.4.tar.gz
[root@localhost ~]# wget https://downloads.apache.org/apr/apr-util-1.6.3.tar.gz
[root@localhost ~]# wget https://downloads.apache.org/httpd/httpd-2.4.58.tar.gz

4.解压软件包
[root@localhost ~]# ls
anaconda-ks.cfg  apr-1.7.4.tar.gz  apr-util-1.6.3.tar.gz  httpd-2.4.58.tar.gz
[root@localhost ~]# tar xf apr-1.7.4.tar.gz 
[root@localhost ~]# tar xf apr-util-1.6.3.tar.gz 
[root@localhost ~]# tar xf httpd-2.4.58.tar.gz 
[root@localhost ~]# ls
anaconda-ks.cfg  apr-1.7.4  apr-1.7.4.tar.gz  apr-util-1.6.3  apr-util-1.6.3.tar.gz  httpd-2.4.58  httpd-2.4.58.tar.gz

5.编译软件包
//apr-1.7.4
[root@localhost ~]# cd apr-1.7.4
[root@localhost apr-1.7.4]# ls
apr-config.in  apr.pc.in   buildconf         configure     encoding    libapr.dsp  Makefile.in   network_io     random        support     tools
apr.dep        apr.spec    build-outputs.mk  configure.in  file_io     libapr.mak  Makefile.win  NOTICE         README        tables      user
apr.dsp        atomic      CHANGES           docs          helpers     libapr.rc   memory        NWGNUmakefile  README.cmake  test
apr.dsw        build       CMakeLists.txt    dso           include     LICENSE     misc          passwd         shmem         threadproc
apr.mak        build.conf  config.layout     emacs-mode    libapr.dep  locks       mmap          poll           strings       time
[root@localhost apr-1.7.4]# vim configure
    cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
   # $RM "$cfgfile"          //注释这一行

[root@localhost apr-1.7.4]# ./configure --prefix=/usr/local/apr
configure: creating ./config.status
config.status: creating Makefile
config.status: creating include/apr.h
config.status: creating build/apr_rules.mk
config.status: creating build/pkg/pkginfo
config.status: creating apr-1-config
config.status: creating apr.pc
config.status: creating test/Makefile
config.status: creating test/internal/Makefile
config.status: creating include/arch/unix/apr_private.h
config.status: executing libtool commands
config.status: executing default commands
[root@localhost apr-1.7.4]# echo $?
0
[root@localhost apr-1.7.4]# ls    //是否生成makefile文件
apr-1-config   apr.mak    build             CMakeLists.txt  configure     encoding    libapr.dsp  locks         misc           passwd        shmem    threadproc
apr-config.in  apr.pc     build.conf        config.layout   configure.in  file_io     libapr.mak  Makefile      mmap           poll          strings  time
apr.dep        apr.pc.in  buildconf         config.log      docs          helpers     libapr.rc   Makefile.in   network_io     random        support  tools
apr.dsp        apr.spec   build-outputs.mk  config.nice     dso           include     libtool     Makefile.win  NOTICE         README        tables   user
apr.dsw        atomic     CHANGES           config.status   emacs-mode    libapr.dep  LICENSE     memory        NWGNUmakefile  README.cmake  test
[root@localhost apr-1.7.4]# make
[root@localhost apr-1.7.4]# echo $?
0
[root@localhost apr-1.7.4]# make install
[root@localhost apr-1.7.4]# echo $?
0

//apr-util-1.6.3
[root@localhost ~]# cd apr-util-1.6.3
[root@localhost apr-util-1.6.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.6.3]# echo $?
0
[root@localhost apr-util-1.6.3]# make
[root@localhost apr-util-1.6.3]# echo $?
0
[root@localhost apr-util-1.6.3]# make install
[root@localhost apr-util-1.6.3]# echo $?
0

//httpd-2.4.58
[root@localhost ~]# cd httpd-2.4.58
[root@localhost httpd-2.4.58]# ./configure --prefix=/usr/local/apache \
> --sysconfdir=/etc/httpd24 \                       //指定配置文件位置,这一行也可以去掉
> --enable-so \                                      //去掉的话,默认就放在/usr/local/apache/conf/httpd.conf
> --enable-ssl \
> --enable-cgi \
> --enable-rewrite \
> --with-zlib \
> --with-pcre \
> --with-apr=/usr/local/apr \
> --with-apr-util=/usr/local/apr-util/ \
> --enable-modules=most \
> --enable-mpms-shared=all \
> --with-mpm=prefork
configure: summary of build options:

    Server Version: 2.4.58
    Install prefix: /usr/local/apache
    C compiler:     gcc
    CFLAGS:          -g -O2  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E
[root@localhost httpd-2.4.58]# echo $?
0
[root@localhost httpd-2.4.58]# make
make[4]: Leaving directory '/root/httpd-2.4.58/modules/mappers'
make[3]: Leaving directory '/root/httpd-2.4.58/modules/mappers'
make[2]: Leaving directory '/root/httpd-2.4.58/modules'
make[2]: Entering directory '/root/httpd-2.4.58/support'
make[2]: Leaving directory '/root/httpd-2.4.58/support'

make[1]: Leaving directory '/root/httpd-2.4.58'
[root@localhost httpd-2.4.58]# echo $?
0
[root@localhost httpd-2.4.58]# make install
[root@localhost httpd-2.4.58]# echo $?
0

6.配置环境变量
[root@localhost ~]# cd /usr/local/apache
[root@localhost apache]# ls
bin  build  cgi-bin  error  htdocs  icons  include  logs  man  manual  modules
[root@localhost apache]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@localhost apache]# source /etc/profile.d/httpd.sh
[root@localhost apache]# which httpd
/usr/local/apache/bin/httpd

[root@localhost apache]# ln -s /usr/local/apache/include/ /usr/include/apache
[root@localhost apache]# vim /etc/man_db.conf 
#
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/apache/man
#---------------------------------------------------------

7.启动apache
//取消这一行注释
[root@localhost ~]# cd /etc/httpd24/                                //进入到配置文件存放的位置
[root@localhost httpd24]# ls
extra  httpd.conf  magic  mime.types  original
[root@localhost httpd24]# vim httpd.conf                        //编辑配置文件
ServerName www.example.com:80
//启动
[root@localhost httpd24]# apachectl start
[root@localhost ~]# ss -antl
State              Recv-Q             Send-Q                           Local Address:Port                           Peer Address:Port             Process             
LISTEN             0                  128                                    0.0.0.0:22                                  0.0.0.0:*                                    
LISTEN             0                  511                                          *:80                                        *:*                                    
LISTEN             0                  128                                       [::]:22                                     [::]:*                                    

8.设置开机自启
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service httpd.service
[root@localhost system]# apachectl stop
[root@localhost system]# ss -antl
State              Recv-Q             Send-Q                           Local Address:Port                           Peer Address:Port             Process             
LISTEN             0                  128                                    0.0.0.0:22                                  0.0.0.0:*                                    
LISTEN             0                  128                                       [::]:22                                     [::]:*                                    
[root@localhost system]# vim httpd.service 
[root@localhost system]# cat httpd.service 
[Unit]
Description=httpd server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload
[root@localhost system]# systemctl status httpd
○ httpd.service - httpd server daemon
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled)
     Active: inactive (dead)
[root@localhost system]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@localhost system]# systemctl status httpd
● httpd.service - httpd server daemon
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
     Active: active (running) since Mon 2023-12-11 14:48:00 CST; 5s ago
     Process: 222039 ExecStart=/usr/local/apache/bin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 222042 (httpd)
      Tasks: 6 (limit: 100179)
     Memory: 5.4M
        CPU: 31ms
     CGroup: /system.slice/httpd.service
             ├─222042 /usr/local/apache/bin/httpd -k start
             ├─222043 /usr/local/apache/bin/httpd -k start
             ├─222044 /usr/local/apache/bin/httpd -k start
             ├─222045 /usr/local/apache/bin/httpd -k start
             ├─222046 /usr/local/apache/bin/httpd -k start
             └─222047 /usr/local/apache/bin/httpd -k start

Dec 11 14:47:59 localhost.localdomain systemd[1]: Starting httpd server daemon...
Dec 11 14:48:00 localhost.localdomain systemd[1]: Started httpd server daemon.
[root@localhost system]# ss -antl
State              Recv-Q             Send-Q                           Local Address:Port                           Peer Address:Port             Process             
LISTEN             0                  128                                    0.0.0.0:22                                  0.0.0.0:*                                    
LISTEN             0                  511                                          *:80                                        *:*                                    
LISTEN             0                  128                                       [::]:22                                     [::]:*                                    

  • 26
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值