Hugh's Blog

CentOS 6.9 下编译安装 httpd2.4 及 PHP5.6

服务器环境为 CentOS 6.9,在 docker 中测试:

docker pull centos:6.9
docker run -d -it --name centos-69 -p 18880:80 -p 18443:443 centos:6.9 bash
docker exec -it centos-69 bash

进入到测试环境中:

yum update
# service 命令可能会缺失
yum install initscripts

httpd 2.4 编译

# 查看当前安装版本并删除
rpm -qa | grep httpd
yum remove httpd****

# 安装依赖
yum -y install gcc pcre-devel openssl-devel expat-devel lynx

# 下载组件包
cd
wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.41.tar.gz
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz

# 解压
tar -zxvf httpd-2.4.41.tar.gz
tar -zxvf apr-1.7.0.tar.gz
tar -zxvf apr-util-1.6.1.tar.gz

mv apr-1.7.0 httpd-2.4.41/srclib/apr
mv apr-util-1.6.1 httpd-2.4.41/srclib/apr-util

cd httpd-2.4.41

# 查看帮助
./configure --help

# 开始编译
# 安装路径为 /usr/local
./configure --prefix=/usr/local/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-included-apr \
--enable-modules=most \
--enable-mods-shared=most \
--enable-mpms-shared=all \
--with-mpm=prefork \
--enable-deflate

make && make install

# 配置命令
ln -s /usr/local/httpd24/bin/httpd /usr/local/bin/httpd24
httpd24 -v

编译结果:

Installing configuration files
mkdir /usr/local/httpd24/conf
mkdir /usr/local/httpd24/conf/extra
mkdir /usr/local/httpd24/conf/original
mkdir /usr/local/httpd24/conf/original/extra
Installing HTML documents
mkdir /usr/local/httpd24/htdocs
Installing error documents
mkdir /usr/local/httpd24/error
Installing icons
mkdir /usr/local/httpd24/icons
mkdir /usr/local/httpd24/logs
Installing CGIs
mkdir /usr/local/httpd24/cgi-bin
Installing header files
Installing build system files
Installing man pages and online manual
mkdir /usr/local/httpd24/man
mkdir /usr/local/httpd24/man/man1
mkdir /usr/local/httpd24/man/man8
mkdir /usr/local/httpd24/manual
make[1]: Leaving directory `/root/httpd-2.4.41'

PHP 5.6 编译

由于没有采用 fpm 的运行模式,需要注意修改 apxs2 的解释文件路径:

# 第一行改为: #!/usr/bin/perl -w
which perl
vim /usr/local/httpd24/bin/apxs

编译过程:

# 添加源
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

# 安装依赖
yum install -y libxml2-devel bzip2-devel curl-devel libmcrypt libmcrypt-devel libpng-devel libjpeg-devel freetype-devel readline-devel libxslt-devel

# ldap
yum install -y openldap openldap-devel cyrus-sasl cyrus-sasl-ldap cyrus-sasl-devel
cp /usr/lib64/libldap* /usr/lib/

# 下载安装包并解压
cd
wget https://www.php.net/distributions/php-5.6.40.tar.gz
tar -zxvf php-5.6.40.tar.gz
cd php-5.6.40

# 查看帮助
./configure --help

# 开始编译
# 安装路径为 /usr/local
./configure --prefix=/usr/local/php56 \
--with-config-file-path=/usr/local/php56/etc \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-opcache \
--enable-fpm \
--enable-mbstring \
--enable-bcmath \
--enable-soap \
--enable-xml \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--enable-zip \
--enable-calendar \
--enable-ftp \
--enable-exif \
--enable-wddx \
--enable-gd-native-ttf \
--enable-maintainer-zts \
--enable-inline-optimization \
--with-fpm-user=www-data \
--with-fpm-group=www-data \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gettext \
--with-iconv \
--with-mcrypt \
--with-mhash \
--with-openssl \
--with-bz2 \
--with-xsl \
--with-curl \
--with-zlib \
--with-bz2 \
--with-gd \
--with-pear \
--with-ldap \
--with-ldap-sasl \
--with-libxml-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-readline \
--with-apxs2=/usr/local/httpd24/bin/apxs

make && make install

# 配置命令
ln -s /usr/local/php56/bin/php /usr/local/bin/php56
php56 -v

编译结果:

Installing PHP SAPI module:       apache2handler
/usr/local/httpd24/build/instdso.sh SH_LIBTOOL='/usr/local/httpd24/build/libtool' libphp5.la /usr/local/httpd24/modules
/usr/local/httpd24/build/libtool --mode=install install libphp5.la /usr/local/httpd24/modules/
libtool: install: install .libs/libphp5.so /usr/local/httpd24/modules/libphp5.so
libtool: install: install .libs/libphp5.lai /usr/local/httpd24/modules/libphp5.la
libtool: warning: remember to run 'libtool --finish /root/php-5.6.40/libs'
chmod 755 /usr/local/httpd24/modules/libphp5.so
[activating module `php5' in /usr/local/httpd24/conf/httpd.conf]
Installing shared extensions:     /usr/local/php56/lib/php/extensions/no-debug-zts-20131226/
Installing PHP CLI binary:        /usr/local/php56/bin/
Installing PHP CLI man page:      /usr/local/php56/php/man/man1/
Installing PHP FPM binary:        /usr/local/php56/sbin/
Installing PHP FPM config:        /usr/local/php56/etc/
Installing PHP FPM man page:      /usr/local/php56/php/man/man8/
Installing PHP FPM status page:   /usr/local/php56/php/php/fpm/
Installing PHP CGI binary:        /usr/local/php56/bin/
Installing PHP CGI man page:      /usr/local/php56/php/man/man1/
Installing build environment:     /usr/local/php56/lib/php/build/
Installing header files:           /usr/local/php56/include/php/
Installing helper programs:       /usr/local/php56/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php56/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php56/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.4
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.3
[PEAR] PEAR           - installed: 1.10.7
Wrote PEAR system config file at: /usr/local/php56/etc/pear.conf
You may want to add: /usr/local/php56/lib/php to your php.ini include_path
/root/php-5.6.40/build/shtool install -c ext/phar/phar.phar /usr/local/php56/bin
ln -s -f phar.phar /usr/local/php56/bin/phar
Installing PDO headers:           /usr/local/php56/include/php/ext/pdo/

修改配置

httpd 自启动

cp /usr/local/httpd24/bin/apachectl /etc/init.d/httpd24

/etc/init.d/httpd24 修改配置:

#!/bin/sh
# chkconfig: 35 85 15 # 添加
# description: httpd 2.4.41 # 添加
# Licensed to the Apache Software Foundation (ASF) under one or more
...
STATUSURL="http://127.0.0.1:80/server-status" # 修改

/usr/local/httpd24/conf/extra/httpd-info.conf 修改配置:

...
<Location /server-status>
    SetHandler server-status
    Require host 127.0.0.1 # 修改
    Require ip 127
</Location>
...

/usr/local/httpd24/conf/httpd.conf 修改配置:

...
ServerName 127.0.0.1 # 修改
...
# Real-time info on requests and configuration
Include conf/extra/httpd-info.conf # 修改
...

添加到开机启动:

service httpd24 restart
chkconfig --add httpd24
chkconfig httpd24 on
chkconfig --list

PHP 配置

cp /root/php-5.6.40/php.ini-production /usr/local/php56/etc/php.ini

php.ini 文件修改:

...
extension_dir = "/usr/local/php56/lib/php/extensions/no-debug-zts-20131226" # 修改
...
[opcache] # 扩展
zend_extension=opcache.so
...

httpd + PHP

httpd.conf 配置:

...
#LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php5_module        modules/libphp5.so # 添加,重要
...
DocumentRoot "/var/www/html" # 修改
<Directory "/var/www/html"> # 修改
...
<IfModule mime_module>
    ...
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    AddType application/x-httpd-php .php .pthml # 添加
    AddType application/x-httpd-php-source .phps # 添加
</IfModule>
...
# Various default settings
Include conf/extra/httpd-default.conf # 修改

httpd 开启 https

# 生成自签名证书
cd /usr/local/httpd24/conf
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt

httpd.conf 更新配置:

...
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so # 修改
...
LoadModule ssl_module modules/mod_ssl.so # 修改
...
# Secure (SSL/TLS) connections
Include conf/extra/httpd-ssl.conf # 修改

extra/httpd-ssl.conf 更新配置:

...
DocumentRoot "/var/www/html" # 修改
...

service httpd24 restart 重启服务器。

PHP 编译扩展

以 swoole 为例:

# 下载扩展
cd
wget http://pecl.php.net/get/swoole-1.10.2.tgz
tar -zxvf swoole-1.10.2.tgz
cd swoole-1.10.2

/usr/local/php56/bin/phpize

# 查看帮助
./configure --help

# 开始编译
./configure --with-php-config=/usr/local/php56/bin/php-config --enable-openssl
make && make install

# 修改 php.ini 配置及重启 httpd 生效
# [swoole]
# extension=swoole.so
# 查看扩展信息
php56 -m | grep swoole
php56 --ri swoole

编译结果:

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php56/lib/php/extensions/no-debug-zts-20131226/
Installing header files:           /usr/local/php56/include/php/

参考

httpd 2.4.27 + php 5.6.31 + mysql 5.5.57 + centOS6.8_x64

深度剖析apache 2.4web服务器