Debian/Ubuntu 从源码编译安装 Apache 2

在六大Web服务器软件,Apache、IIS、Lighttpd、Nginx、LiteSpeed、Zeus中,Apache 的世界使用量排名始终位居榜首,并且Apache 安装简单,参考资料众多,所以绝对是Web 服务器软件的首选。

一、准备工作

系统版本:Ubuntu-10.10-Server-I386

1.1、安装基础编译环境

bash
aptitude  install build-essential

1.2、编译安装 Zlib 库

text
wget http://zlib.net/zlib-1.2.5.tar.gz
tar -zxf zlib-1.2.5.tar.gz
cd zlib-1.2.5/
./configure --prefix=/usr/local
make && make install

1.3、安装 OpenSSL (可选)

详细安装见 《Linux 从源码编译安装 OpenSSL》

二、编译安装 Apache2.2

这是 apache2.2 安装最大的难点,详细说明见 apache2.2 中文文档配置源代码树部分,其中关于模块选项。

我的选择 (推荐)

bash
wget http://www.apache.org/dist/httpd/httpd-2.2.17.tar.gz
tar -zxf httpd-2.2.17.tar.gz
cd httpd-2.2.17/
./configure  --prefix=/usr/local/apache --enable-mods-shared=most --enable-ssl=shared --with-ssl=/usr/local
make && make install

使用模块有两种方法:一是静态连接进核心,二是作为DSO模块动态加载

bash
--enable-mods-shared=most 动态加载大部分模块
--enable-ssl=shared 动态加载 mod-ssl 模块
--enable-ssl=static 静态加载 mod-ssl 模块

另一种选择

bash
./configure  --enable-so --enable-rewrite --enable-ssl --with-ssl=/usr/local

上面意思是将 mod_so 和 mo_rewrite、ssl 模块静态编译进 apache(还可以添加更多模块),mod_so 作用是允许 apache 以后运行时作为 DSO 模块动态加载,等同于 –enable-modules=so

三、启动Apache服务器

两种方法,可以使用 apachectl 或者 httpd 皆可。简单说 apachectl 是控制器,可以开、关、重启 apache ,是启动脚本。httpd 是后台守护进程,是apache的主程序。因此apachectl中必须指明httpd路径。(更多介绍)

将apachectl加入系统变量中,即直接编辑/etc/environment文件,或者新建软链接。

bash
ln -s /usr/local/apache/bin/apachectl /usr/local/bin
ln -s /usr/local/apache/bin/httpd /usr/local/bin
bash
sudo apachectl -k start
sudo apachectl -k stop
sudo apachectl -k restart

四、开机自动启动 Apache

这里首先要清楚什么是运行级别,详细介绍见《Ubuntu系统update-rc.d命令详解》

方法一(推荐)

bash
ln -s /usr/local/apache/bin/apachectl /etc/init.d/
update-rc.d apachectl defaults

方法二 (图形界面)

bash
sudo install sysv-rc-conf
sudo sysv-rc-conf //找到apachectl 将其2,3,4,5都选中

五、其他

5.1、Apache 升级

除非有大的版本号变动,否则可按原编译选项重新编译安装即可。

bash
./configure  --prefix=/usr/local/apache --enable-mods-shared=most --enable-ssl=shared --with-ssl=/usr/local
 make && make install
apachectl -k graceful-stop
apachectl -k start

5.2、Apache 配置

详细 Apache2.2 配置见《Linux 从源码编译安装及配置 PHP 5》

ESC