在Linux中通过编译源代码进行安装软件,需要GCC(GNU Compiler Collection,GNU编译器套件)的支持。
通过yum安装GCC
[root@localhost ~]# yum install gcc
C程序源码编译安装三个步骤:
第一步:./configure
(1)通过选项传递参数,指定启用特性、安装路径等;执行时会参考用户的指定以及Makefile.in文件生成makefile;
–help:获取其支持使用的选项
选项分类:
安装路径设定:
–prefix=/PATH/TO/SOMEWHERE:指定默认安装位置;默认为/usr/local/
–sysconfdir=/PATH/TO/SOMEWHERE:指定配置文件安装位置
System types:交叉编译时,指定目标系统类型
Optional Features: 可选特性
–disable-FEATURE
–enable-FEATURE[=ARG]
Optional Packages: 可选包
–with-PACKAGE[=ARG]
–without-PACKAGE
(2)检查依赖到的外部环境
第二步:make
根据makefile文件,构建应用程序
第三步:make install
在Apache官网下载源码包进行编译安装,下载地址:http://httpd.apache.org/download.cgi
[root@localhost ~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.23.tar.bz2 [root@localhost ~]# tar xf httpd-2.4.23.tar.bz2 [root@localhost ~]# cd httpd-2.4.23 [root@localhost httpd-2.4.23]# ./configure --prefix=/usr/local/apache2 --sysconfdir=/usr/local/conf/apache2.conf configure: error: APR not found. Please read the documentation.
Apache在安装时需要依赖一些环境,这里报错提示需要安装APR(Apache Portable Runtime),下载地址:http://apr.apache.org/download.cgi
[root@localhost ~]# wget http://mirror.bit.edu.cn/apache//apr/apr-1.5.2.tar.bz2 [root@localhost ~]# tar xf apr-1.5.2.tar.bz2 [root@localhost ~]# cd apr-1.5.2 [root@localhost apr-1.5.2]# ./configure --prefix=/usr/local/apr [root@localhost apr-1.5.2]# make [root@localhost apr-1.5.2]# make install [root@localhost apr-1.5.2]# cd ../httpd-2.4.23/ [root@localhost httpd-2.4.23]# ./configure --prefix=/usr/local/apache2 --sysconfdir=/usr/local/conf/apache2.conf configure: error: APR not found. Please read the documentation.
依旧报上面错误,这是因为编译安装自定义了APR的安装目录,所以需要通过–with-apr手动指定APR安装目录
[root@localhost httpd-2.4.23]# ./configure --prefix=/usr/local/apache2 --sysconfdir=/usr/local/conf/apache2.conf --with-apr=/usr/local/apr configure: error: APR-util not found. Please read the documentation.
这次报错提示缺少APR-util环境,下载地址:http://apr.apache.org/download.cgi
[root@localhost ~]# wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.5.4.tar.bz2 [root@localhost ~]# tar xf apr-util-1.5.4.tar.bz2 [root@localhost ~]# cd apr-util-1.5.4 [root@localhost apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util configure: error: APR could not be located. Please use the --with-apr option.
报错提示编译APR-util需要指定APR路径
[root@localhost apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [root@localhost apr-util-1.5.4]# make [root@localhost apr-util-1.5.4]# make install
有了以上经验,再次编译Apache时指定APR-util路径
[root@localhost httpd-2.4.23]# ./configure --prefix=/usr/local/apache2 --sysconfdir=/usr/local/conf/apache2.conf --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
此处报错提示缺少PCRE环境,下载地址:https://sourceforge.net/projects/pcre/files/pcre
注意:此处应安装PCRE;如果安装PCRE2会报如下错误:
configure: error: Did not find pcre-config script at /usr/local/pcre.
[root@localhost ~]# wget https://sourceforge.net/projects/pcre/files/pcre/8.39/pcre-8.39.tar.bz2 [root@localhost ~]# tar xf pcre-8.39.tar.bz2 [root@localhost ~]# cd pcre-8.39 [root@localhost pcre-8.39]# ./configure --prefix=/usr/local/pcre configure: error: You need a C++ compiler for C++ support.
此处报错提示需要C++编译器,通过yum安装gcc-c++
[root@localhost pcre-8.39]# yum install gcc-c++ [root@localhost pcre-8.39]# ./configure --prefix=/usr/local/pcre [root@localhost pcre-8.39]# make [root@localhost pcre-8.39]# make install
再次编译Apache,指定PCRE路径
[root@localhost httpd-2.4.23]# ./configure --prefix=/usr/local/apache2 --sysconfdir=/usr/local/conf/apache2.conf --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre [root@localhost httpd-2.4.23]# make [root@localhost httpd-2.4.23]# make install
至此,Apache编译安装完成了,接下来进行安装后的配置:
(1)导出二进制程序目录至PATH环境变量中:
编辑文件/etc/profile.d/apache.sh
export PATH=$PATH:/usr/local/apache2/bin
(2)导出头文件:
基于符号链接的方式实现:
ln -sv /usr/local/apache2/include/ /usr/include/apache
(3)导出库文件路径:
编辑文件/etc/ld.so.conf.d/NAME.conf,添加新的库文件所在目录至此文件中
让系统重新生成缓存:ldconfig [-v]
(4)导出帮助手册:
编辑/etc/man_db.conf文件,添加一个MANPATH(CentOS7下自动识别man手册)
启动Apache服务命令:apachectl start
使用ss -tnl命令查看系统是否监听于80端口,使用iptables -F命令临时清除防火墙规则,最后使用浏览器访问服务器ip,若出现
It works!
则表示Apache服务启动成功,安装到此结束。
原创文章,作者:萝卜,如若转载,请注明出处:http://www.178linux.com/43274