如何编译安装Apache+PHP7?

Apache

1 前言

笔者平素不喜欢简单的编译安装,因为很多系统管理员为了省事,编译安装的东西没有rpm包安装那种容易管理的感觉,所以,笔者的编译安装是尽量接近rpm安装的。

2 最佳实践

2.1 环境配置

2.1.1 环境配置

IP Address = 10.168.0.90
OS Type = CentOS 7.x-x86_64, RHEL-7.x x86_64, RHEL-8.x-x86_64

2.1.2 安装编译环境

yum -y install gcc gcc-c++ make expat-devel

2.1.3 安装常用工具

yum install -y wget net-tools vim bzip2 openssh-clients

2.1.4 下载apache软件包

cd ~
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.6.5.tar.gz
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
wget http://archive.apache.org/dist/httpd/httpd-2.4.35.tar.gz

2.1.5 下载php安装包

cd ~
wget http://am1.php.net/distributions/php-7.2.11.tar.bz2

如以上安装包存在过时的情况或你需要其他版本请从以下页面下载,
http://apr.apache.org/download.cgi
http://www.apache.org/dyn/closer.cgi/httpd/
http://www.php.net/releases/

2.1.6 防火墙配置

firewall-cmd --permanent --add-service http
firewall-cmd --reload
firewall-cmd --list-all

2.1.7 关闭SELinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0

2.2 编译部署APR

2.2.1 清理旧的apr包

yum remove -y arp apr-util apr-util-ldap

2.2.2 解压软件包

cd ~
tar -xf apr-1.6.5.tar.gz
tar -xf apr-util-1.6.1.tar.gz

2.2.3 预编译APR

cd ~/apr-1.6.5
./configure

2.2.4 编译并安装APR

make
make install

2.2.5 预编译APR-Util

cd ~/apr-util-1.6.1
./configure --with-apr=/usr/local/apr

2.2.6 编译并安装APR-Util

make
make install

2.3 编译部署Apache HTTPD

2.3.1 解压软件包

cd ~
tar -xf httpd-2.4.35.tar.gz

2.3.2 预编译软件包

cd ~/httpd-2.4.35
./configure --bindir=/usr/sbin/ \
            --sbindir=/usr/sbin/ \
            --sysconfdir=/etc/httpd/ \
            --libdir=/usr/lib64/  \
            --mandir=/usr/share/man/ \
            --includedir=/usr/include/ \
            --enable-so

如果遇到如下错误提示,

configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

则需要安装以下包解决依赖关系,

yum install -y pcre-devel

2.3.3 预编和安装软件包

make
make install

2.3.4 创建运行用户

groupadd  -g 48 apache
useradd -u 48 -g 48 -d /usr/share/httpd -s /sbin/nologin apache

2.3.5 修改运行用户

sed -i "s/User daemon/User apache/g" /etc/httpd/httpd.conf
sed -i "s/Group daemon/Group apache/g" /etc/httpd/httpd.conf

2.3.6 测试服务启动与关闭

apachectl -f /etc/httpd/httpd.conf -k start
apachectl -f /etc/httpd/httpd.conf -k stop

2.3.7 检查服务启动

netstat -antp | grep httpd
ps -ef | grep httpd

2.3.8 检查服务

http://10.168.0.90/

2.4 安装优化与定制

2.4.1 配置服务

vim /etc/systemd/system/httpd.service

增加如下配置:

[Unit]
Description=httpd service
After=syslog.target network.target

[Service]
Type=forking
PIDFile=/usr/local/apache2/logs/httpd.pid
ExecStart=/bin/sh -c '/usr/sbin/apachectl -f /etc/httpd/httpd.conf -k start'
ExecStop=/bin/sh -c '/usr/sbin/apachectl -f /etc/httpd/httpd.conf -k stop'
ExecReload=/bin/sh -c '/usr/sbin/apachectl -f /etc/httpd/httpd.conf -k graceful'
ExecStartPost=/bin/sh -c '/usr/bin/chown apache:apache -R /usr/local/apache2/logs/httpd.pid'

[Install]
WantedBy=multi-user.target

重载使配置生效:

systemctl daemon-reload

2.4.2 启动服务并开机启动

systemctl start httpd.service
systemctl enable httpd.service

2.4.3 新增配置文件目录

mkdir /etc/httpd/conf.d
echo 'IncludeOptional /etc/httpd/conf.d/*.conf' >> /etc/httpd/httpd.conf

2.4.4 优化目录结构

mkdir /var/www
ln -s /usr/local/apache2/htdocs /var/www/html
ln -s /usr/local/apache2/logs /var/log/httpd

2.4.5 配置日志切割

修改配置文件:

vim /etc/logrotate.d/httpd

加入如下配置:

/usr/local/apache2/logs/*log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        systemctl reload httpd > /dev/null 2>/dev/null || true
    endscript
}

你可以使用如下命令测试日志切割,

logrotate /etc/logrotate.d/httpd

2.5 编译安装PHP

2.5.1 清理php安装包

yum remove `rpm -qa | grep php`

2.5.2 解压安装包

cd ~/
tar -xf php-7.2.11.tar.bz2

2.5.3 安装前构建

cd ~/php-7.2.11
./configure --bindir=/usr/sbin/ \
            --sbindir=/usr/sbin/ \
            --sysconfdir=/etc/ \
            --libdir=/usr/lib64/  \
            --mandir=/usr/share/man/ \
            --includedir=/usr/include/ \
            --with-apxs2=/usr/sbin/apxs

注:如果需要使用MySQL数据库,请使用如下参数编译,

cd ~/php-7.2.11
./configure --bindir=/usr/sbin/ \
            --sbindir=/usr/sbin/ \
            --sysconfdir=/etc/ \
            --libdir=/usr/lib64/  \
            --mandir=/usr/share/man/ \
            --includedir=/usr/include/ \
            --with-apxs2=/usr/sbin/apxs \
            --with-pdo-mysql

注:如果需要使用PostgreSQL数据库,请使用如下参数编译(注,不同版本的PostgreSQL实际路径有所不同),
如PostgreSQL9.6版本,

cd ~/php-7.2.11
./configure --bindir=/usr/sbin/ \
            --sbindir=/usr/sbin/ \
            --sysconfdir=/etc/ \
            --libdir=/usr/lib64/  \
            --mandir=/usr/share/man/ \
            --includedir=/usr/include/ \
            --with-apxs2=/usr/sbin/apxs \
            --with-pdo-pgsql=/usr/pgsql-9.6/

如PostgreSQL10版本,

cd ~/php-7.2.11
./configure --bindir=/usr/sbin/ \
            --sbindir=/usr/sbin/ \
            --sysconfdir=/etc/ \
            --libdir=/usr/lib64/  \
            --mandir=/usr/share/man/ \
            --includedir=/usr/include/ \
            --with-apxs2=/usr/sbin/apxs \
            --with-pdo-pgsql=/usr/pgsql-10/

如果遇到以下错误提示,

configure: error: libxml2 not found. Please check your libxml2 installation.

则需要安装以下包解决依赖关系,

yum install -y libxml2-devel

根据提示解决安装的依赖关系(可选,适用于PostgreSQL数据库),请根据当前的具体版本安装,

yum install -y postgresql96-devel
yum install -y postgresql10-devel

注:以上根据实际选一个版本即可

2.5.4 编译并安装

make
make install | tee install.log

如果有如下提示,

/root/7.2.11/ext/standard/info.c:83: undefined reference to `ts_resource_ex'
ext/standard/.libs/info.o: In function `php_info_print':
/root/7.2.11/ext/standard/info.c:97: undefined reference to `ts_resource_ex'
ext/standard/.libs/info.o: In function `php_info_print_html_esc':
/root/7.2.11/ext/standard/info.c:69: undefined reference to `ts_resource_ex'
ext/standard/.libs/info.o: In function `php_print_gpcse_array':
/root/7.2.11/ext/standard/info.c:204: undefined reference to `executor_globals_id'
ext/standard/.libs/info.o: In function `php_print_info':
/root/7.2.11/ext/standard/info.c:1109: undefined reference to `executor_globals_id'
collect2: error: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1

可用以下命令处理,

make clean

2.5.5 配置php.ini

cp php.ini-development /etc/php.ini
ln -s /etc/php.ini /usr/lib64/

2.5.6 修改时区

sed -i 's/;date.timezone =/date.timezone = Asia\/Shanghai/g' /etc/php.ini

2.6 Apache加载并优化PHP

2.6.1 确认模块编译成功

find /usr/ -name libphp\*.so

可见如下显示:

/usr/local/apache2/modules/libphp7.so

2.6.2 确认模块已经加载

grep modules/libphp /etc/httpd/httpd.conf

可见如下显示,否则请手动添加,

LoadModule php7_module modules/libphp7.so

注:适合于PHP 7

LoadModule php5_module modules/libphp5.so

注:适合于PHP 5

2.6.3 配置后缀和session的位置

vim /etc/httpd/conf.d/php.conf

加入如下配置:

#
# Cause the PHP interpreter to handle files with a .php extension.
#
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

#
# Allow php to handle Multiviews
#
AddType text/html .php

#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php

#
# Uncomment the following lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#<FilesMatch \.phps$>
# SetHandler application/x-httpd-php-source
#</FilesMatch>

#
# Apache specific PHP configuration options
# those can be override in each configured vhost
#
php_value session.save_handler "files"
php_value session.save_path "/var/lib/php/session"

配置session目录:

mkdir -p /var/lib/php/session
chown apache:apache /var/lib/php/session 
chmod 775 /var/lib/php/session

2.6.4 重启服务是配置生效

systemctl restart httpd.service

2.7 测试PHP

2.7.1 添加测试代码

mkdir -p /var/www/html
echo '<?php phpinfo(); ?>' > /var/www/html/index.php

2.7.2 访问测试连接

http://10.168.0.90/index.php

2.8 动态增加PHP模块

请按照以下教程执行,
https://www.cmdschool.org/archives/3166

2.9 增加SSL支持

2.9.1 增加防火墙

firewall-cmd --permanent --add-service https
firewall-cmd --reload
firewall-cmd --list-all

2.9.2 增加SSL所需的模块

cd ~/httpd-2.4.35
./configure --bindir=/usr/sbin/ \
            --sbindir=/usr/sbin/ \
            --sysconfdir=/etc/httpd/ \
            --libdir=/usr/lib64/  \
            --mandir=/usr/share/man/ \
            --includedir=/usr/include/ \
            --enable-so \
            --enable-ssl \
            --enable-socache-shmcb

如果遇到以下错误提示,

checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures

则需要安装以下包解决依赖关系,

yum install -y openssl-devel

2.9.3 预编和安装软件包

make
make install

2.9.4 启用SSL所需的模块

vim /etc/httpd/httpd.conf

启用如下选项:

LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

2.9.5 增加SSL所需的配置

vim /etc/httpd/conf.d/httpd-ssl.conf

增加如下配置:

LoadModule ssl_module modules/mod_ssl.so
Listen 443
SSLPassPhraseDialog builtin
SSLSessionCache shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout 300
SSLRandomSeed startup file:/dev/urandom 256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin

住:由于配置虚拟机需要申请证书,故以上省略虚拟机配置。

2.9.6 重启服务是配置生效

systemctl restart httpd

2.9.7 确认配置生效

netstat -antp | grep :::443

可见如下显示,

tcp6       0      0 :::443                  :::*                    LISTEN      8545/httpd

参阅文档:
==============

Apache的下载:
—————-
http://archive.apache.org/dist/httpd/

Apache的安装
—————-
http://httpd.apache.org/docs/2.4/install.html

PHP的安装
http://php.net/manual/en/install.unix.apache2.php

Apache SSL
—————–
http://httpd.apache.org/docs/2.4/ssl/ssl_howto.html

http://httpd.apache.org/docs/2.4/mod/mod_ssl.html

Apache的服务控制
—————-
https://httpd.apache.org/docs/trunk/stopping.html

没有评论

发表回复

Tomcat
如何编译安装Tomcat的Native库?

1 基础知识 Tomcat的Native库允许Tomcat使用OpenSSL作为JSSE的替代品来支 …

编译安装
如何基于Oracle Linux 9.x 编译BerkeleyDB?

1 基础知识 1.1 简介 Berkeley DB是一个开源的文件数据库,介于关系数据库与内存数据库 …

编译安装
如何在Oracle Linux 9.x编译部署Redis?

1 基础知识 1.1 Redis的简介 Redis是一种开放源代码(BSD许可)的运行于内存中的数据 …