Linux下apache以root身份运行

最近在某个项目需求中需要在 Linux 下使 apache 以 root 身份运行(默认使用的是 apache 用户),直接修改 apache 配置文件 httpd.conf 中的运行身份为 User root 和 Group root 后启动 apache 结果会报下面的错误:

Error: Apache has not been designed to serve pages while running as root. There are known race conditions that will allow any local user to read any file on the system. If you still desire to serve pages as root then add -DBIG_SECURITY_HOLE to the EXTRA_CFLAGS line in your src/Configuration file and rebuild the server. It is strongly suggested that you instead modify the User directive in your httpd.conf file to list a non-root user.

看来 Linux 发行版中的 apache 因为安全原因默认就不允许以 root 身份运行了,必须修改编译选项 BIG_SECURITY_HOLE 重新编译安装 apache 才可以。

由于我使用的是 64 位的 RedHat Enterprise Linux 6 系统,考虑直接使用源代码 RPM 包的形式编译,我们实际上只需要替换 apache 的 httpd 这个二进制文件就可以启用 root 权限了,这样是最简单方便的方法了。

首先确认当前 apache 的版本:

rpm -q httpd
httpd-2.2.15-9.el6.x86_64

下载同样版本的 source rpm 包:

cd /usr/src/redhat/SRPMS
wget http://ftp.redhat.com/pub/redhat/linux/enterprise/6Server/en/os/SRPMS/httpd-2.2.15-9.el6.src.rpm

然后使用 rpm2cpio 命令解压缩 RPM 包:

rpm2cpio httpd-2.2.15-9.el6.src.rpm | cpio -iduv

接着从解压缩出来的源代码中可以看到源代码包:httpd-2.2.15-9.tar.bz2 还有一堆 patch 补丁文件,另外就是比较重要的编译配置文件 httpd.spec 了。

这时就可以修改 httpd.spec 文件中的编译选项了,根据最开始的提示找到 CFLAGS,并加上 BIG_SECURITY_HOLE 定义:

CFLAGS=”$RPM_OPT_FLAGS -Wformat-security -fno-strict-aliasing -DBIG_SECURITY_HOLE”

保存退出,接着就可以直接使用 rpmbuild 命令直接编译出 RPM 包了:

rpmbuild --ba httpd.spec

稍微一会就可以找到编译出的 RPM 包了:

/root/rpmbuild/RPMS/x86_64/httpd-2.2.15-9.el6.x86_64.rpm

从编译出的 RPM 包里解压缩出 httpd、httpd.event、httpd.worker 这三个文件替换到 /usr/sbin 等目录下,再重启 httpd 服务就可以使用 root 身份运行 apache 了。

发表评论





*