在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.www58058.com/43274