如何编译安装Apache+PHP5?

PHP

1 前言

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

2 最佳实践

2.1 环境配置

2.1.1 环境配置

IP Address = 10.168.0.90
OS Type = CentOS 7.5 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

2.1.4 下载apache软件包

cd ~
wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.5.tar.gz
wget http://mirrors.shu.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://cn2.php.net/distributions/php-5.6.40.tar.bz2

注:具体的下载页面请参阅
http://www.php.net/releases/

2.1.6 防火墙配置

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

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

根据提示解决依赖关系:

yum install -y apr-devel apr-util-devel 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
ln -s /usr/local/apache2/modules/ /etc/httpd/modules

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
}

2.4.6 测试日志切割

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-5.6.40.tar.bz2

2.5.3 安装前构建

cd ~/php-5.6.40
./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-5.6.40
./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-5.6.40
./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-5.6.40
./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/

根据提示解决安装的依赖关系

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/php-5.6.40/ext/standard/info.c:83: undefined reference to `ts_resource_ex'
ext/standard/.libs/info.o: In function `php_info_print':
/root/php-5.6.40/ext/standard/info.c:97: undefined reference to `ts_resource_ex'
ext/standard/.libs/info.o: In function `php_info_print_html_esc':
/root/php-5.6.40/ext/standard/info.c:69: undefined reference to `ts_resource_ex'
ext/standard/.libs/info.o: In function `php_print_gpcse_array':
/root/php-5.6.40/ext/standard/info.c:204: undefined reference to `executor_globals_id'
ext/standard/.libs/info.o: In function `php_print_info':
/root/php-5.6.40/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/libphp5.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 添加测试代码

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.8 增加SSL支持

2.8.1 增加防火墙

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

2.8.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

根据提示解决依赖关系:

yum install -y openssl-devel

2.8.3 启用SSL所需的模块

vim /etc/httpd/httpd.conf

启用如下选项:

LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

2.8.4 增加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
<VirtualHost _default_:443>
    ErrorLog logs/ssl_error_log
    TransferLog logs/ssl_access_log
    LogLevel warn
    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCipherSuite DEFAULT:!EXP:!SSLv2:!DES:!IDEA:!SEED:+3DES
    SSLCertificateFile /etc/httpd/2_www.cmdschool.org.cer
    SSLCertificateKeyFile /etc/httpd/3_www.cmdschool.org.key
    SSLCertificateChainFile /etc/httpd/1_root_bundle.cer
    <Files ~ "\.(cgi|shtml|phtml|php3?)$">
        SSLOptions +StdEnvVars
    </Files>
    <Directory "/var/www/cgi-bin">
        SSLOptions +StdEnvVars
    </Directory>
    SetEnvIf User-Agent ".*MSIE.*" \
        nokeepalive ssl-unclean-shutdown \
        downgrade-1.0 force-response-1.0
    CustomLog logs/ssl_request_log \
        "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>

注:证书请自行上传到指定的路径

2.8.5 重启服务是配置生效

systemctl restart 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

没有评论

发表评论

PHP
如何编译安装Apache+PHP8.1.1?

1 前言 笔者平素不喜欢简单的编译安装,因为很多系统管理员为了省事,编译安装的东西没有rpm包安装那 …

Apache
如何编译安装Apache+PHP7.4.3?

1 前言 笔者平素不喜欢简单的编译安装,因为很多系统管理员为了省事,编译安装的东西没有rpm包安装那 …

Apache
如何打包Docker Apache+PHP 7.4.11(可连MySQL)?

1 基础知识 以下版本基于MySQL数据库的连接,本章节需要你先掌握Linux系统的PHP 7.4. …