Ubuntu 20.04通过编译安装apache2

发布于 2025-02-03 最后更新于 2025-02-05 2,618 次阅读 633 字


Ubuntu 20.04通过编译安装apache2(httpd-2.4.54)完整步骤


0.Apache官网

Apache官网: https://dlcdn.apache.org/

1.下载安装源码

下载httpd-2.4.54,下载地址:
https://archive.apache.org/dist/httpd/httpd-2.4.54.tar.gz

下载apr-1.7.5,下载地址:
https://dlcdn.apache.org//apr/apr-1.7.5.tar.gz

下载apr-util-1.6.3,下载地址:
https://dlcdn.apache.org//apr/apr-util-1.6.3.tar.gz

apr-1.7.5.tar.gz和apr-util-1.6.3.tar.gz
wget https://dlcdn.apache.org/apr/apr-1.7.5.tar.gz
wget https://dlcdn.apache.org/apr/apr-util-1.6.3.tar.gz
tar -zxvf apr-1.7.5.tar.gz
tar -zxvf apr-util-1.6.3.tar.gz
mv apr-1.7.5 apr
mv apr-util-1.6.3 apr-util

cd apr
./configure --prefix=/usr/local/apr
make -j 4 && make install

cd apr-util
./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr
make -j 4 && make install

2.安装依赖

依次执行以下命令:

apt update -y
apt install build-essential libpcre3 libpcre3-dev libssl-dev -y && 
apt install libexpat1-dev -y &&
apt install libapr1-dev libaprutil1-dev -y ;

也可以像上面那样编译安装,下载链接(http://sourceforge.net/projects/pcre/files/pcre/)
./configure --prefix=/usr/local/pcre
make
sudo make install

编译时若提示error: You need a C++ compiler for C++ support.说明系统没有C/C++编译环境,使用以下环境安装:
sudo apt-get install build-essential

编译apache出现make[2]: *** [exports.lo] 错误 1
将apr和apr-util源码复制到apache源码的srclib文件夹中,
并分别以apr和apr-util命名,configure后面加上--with-included-apr,并重新make:
>
./configure --prefix=/usr/local/apache2  
--with-apr=/usr/local/apr \ 
--with-apr-util=/usr/local/apr-util \  
--with-included-apr \
--enable-so \ 
--enable-dav \ 
--enable-mainer-mode \  
--enable-rewritemake ;
>

3.安装apache

wget https://dlcdn.apache.org/httpd/httpd-2.4.63.tar.gz
tar -zxvf httpd-2.4.63.tar.gz
cd httpd-2_x_NN

将apr和apr-util源码复制到apache源码的srclib文件夹中:
cp apr httpd-2.4.63/srclib/
cp apr-util httpd-2.4.63/srclib/

./configure --prefix=/usr/local/apache2  
--with-apr=/usr/local/apr 
--with-apr-util=/usr/local/apr-util  
##--with-included-apr \
--enable-so \ 
--enable-dav \ 
--enable-mainer-mode \  
--enable-rewritemake ;
或
./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-included-apr --enable-so --enable-dav --enable-mainer-mode --enable-rewritemake

make -j 4 && make install
echo $?

增加环境变量:

which httpd                             # 查看httpd安装位置为 /usr/local/apache2/bin
/usr/local/apache2/bin/httpd -v         # 查看httpd版本为2.4.54

export PATH=PATH:/usr/local/apache2/bin
source .bash_profile
或
ln -sPWD/apachectl /bin/apache2
ln -s $PWD/conf /etc/apache2

4.启动apache

apachectl start

5.验证访问

curl ip地址, 查看apache是否启动起来, 浏览器访问127.0.0.1, 页面显示 it works!