CentOS 7.6 編譯安裝 apache/httpd 和 php7
基礎環境
-
阿里雲-輕量應用服務器 CentOS 7.6
-
目標 PHP 版本:7.4.13
-
目標 apache/httpd 版本:2.4.46
下載源碼包
php 鏡像:http://php.p2hp.com
apache 官網:http://httpd.apache.org/download.cgi,官網下載頁會提示可用下載鏡像
cd /data/Downloads/
wget https://mirrors.sohu.com/php/php-7.4.13.tar.gz
wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.gz
tar -zxvf php-7.4.13.tar.gz
tar -zxvf httpd-2.4.46.tar.gz
准備安裝
編譯安裝 php
cd /data/Downloads/php-7.4.13
查看配置參數
./configure --help
編譯配置
- 指定安裝目錄:
./configure --prefix=/usr/local/php74
- 指定配置文件路徑
php.ini
(安裝完畢后查看配置文件信息:php --ini
)
--with-config-file-path=/etc/php74
- 指定配置文件目錄
*/etc
--sysconfdir=/etc/php74
- 啟用
php-fpm
,暫時未使用,但是在后續的操作中順路查詢了后續開啟的方法,得到的結果是不可編譯添加,只能初始編譯開啟
--enable-fpm
- (廢棄)執行
make
,報錯:未找到sqlite3
,因基本使用不到sqlite
,故使用./configure --help | grep sqlite
查看相關編譯參數后,添加參數:
--without-sqlite3 --without-pdo-sqlite
- 其他
--with-apxs2=/usr/local/apache24/bin/apxs
- 編譯安裝,根據報錯依次安裝:
yum install libxml2-devel sqlite-devel
make
make install
安裝后配置
# 配置可直接執行的 php 命令,這里是 php74
ln -s /usr/local/php7.4/bin/php /usr/local/bin/php74
# 配置可直接執行的 php-fpm 命令,這里是 php74-fpm
ln -s /usr/local/php7.4/sbin/php-fpm /usr/local/bin/php74-fpm
# 暫未使用
cp /usr/local/php7.4/etc/php-fpm.conf.default /usr/local/php7.4/etc/php-fpm.conf
# 暫未使用
cp /usr/local/php7.4/etc/php-fpm.d/www.conf.default /usr/local/php7.4/etc/php-fpm.d/www.conf
# 復制配置文件,該處可能會有問題
cp /data/Downloads/php-7.4.13/php.ini-development | php.ini-production /usr/local/php7.4/etc/php.ini
安裝 Composer
composer 不在正常的 php 編譯流程之中,只是測試使用時引出了后續的操作
# 下載,可使用 wget / curl 等
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
# 創建安裝目錄,composer 安裝工具無法自動創建
mkdir /usr/local/composer
# 安裝,指定安裝目錄,指定安裝文件名
php composer-setup.php --install-dir=/usr/local/composer --filename=composer
# 創建可執行文件軟鏈接
ln -s /usr/local/composer/composer.phar /usr/local/bin/composer
# 與第一個操作對應,暫時不明
php -r "unlink('composer-setup.php');"
報錯 Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?
,結合百度(參考鏈接)和 ./configure --help | grep openssl
解決方法一:可以在配置階段重新添加以下參數進行配置后,重新配置、編譯、安裝:
--with-openssl
解決方法二:編譯模塊的方法添加(參考鏈接)
查看之前的編譯參數
cat -n /data/Downloads/php-7.4.13/config.nice
php74 -i | grep configure
php74 -r "phpinfo();" | grep configure
已編譯安裝的 php 添加模塊
openssl 為例
切換到解壓的源目錄下的 /data/php-7.4.13/ext/openssl
,執行
/usr/local/php7.4/bin/phpize
報錯 Cannot find config.m4. Make sure that you run '/usr/local/php7.4/bin/phpize' in the top level source directory of the module
ls
查看,沒有 config.m4
,但是有 config0.m4
,將該文件復制或移動並重命名(參考鏈接)為 config.m4
重新執行 phpize
報錯 Cannot find autoconf
安裝
yum install autoconf
再次執行 phpize
當前目錄下生成 configure
等文件,執行
./configure
報錯 configure: error: Cannot find php-config. Please use --with-php-config=PATH
執行
./configure --with-php-config=/usr/local/php7.4/bin/php-config
報錯
no package 'openssl' found
執行 openssl version
發現是有 openssl
的,查看 ./configure --help | grep openssl
,添加參數后執行
./configure --with-php-config=/usr/local/php7.4/bin/php-config --with-openssl
再次報錯 no package 'openssl' found
,安裝 yum install openssl-devel
(參考鏈接) 后執行
./configure --with-php-config=/usr/local/php7.4/bin/php-config
編譯安裝,make,輸出
Libraries have been installed in:
/data/php-7.4.13/ext/openssl/modules
make install,輸出
Installing shared extensions: /usr/local/php7.4/lib/php/extensions/no-debug-non-zts-20190902/
查看 php 啟用的模塊
php74 -m
php74-fpm -m
此時若修改 php.ini 中開啟 openssl 以及 extension_dir 無果,原因可能是配置文件的問題,詳情
php 編譯默認配置文件路徑是 /usr/local/php7.4/lib,而不是 etc,查看 php 配置文件信息(參考鏈接)
php74 --ini
Configuration File (php.ini) Path: /usr/local/php7.4/lib
Loaded Configuration File: (none)
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
解決方法一:直接使用 lib 作為配置文件目錄,搞一份 php.ini 文件過去,以后修改這個
解決方法一:ln -s /usr/local/php7.4/etc/php.ini /usr/local/php7.4/lib/php.ini
解決方法三:重新編譯 php,加上參數(參考鏈接)
--with-config-file-path=/usr/local/php7.4/etc
編譯安裝 apache/httpd
cd /data/Downloads/httpd-2.4.46
查看編譯參數
./configure --help
編譯配置
指定安裝目錄、啟用 SSL、啟用動態模塊加載
./configure
--prefix=/usr/local/apache2.4
--sysconfdir=/etc/httpd
--enable-ssl
--enable-so
--enable-module=shared
根據報錯依次安裝
yum install -y apr-devel apr-util-devel pcre-devel openssl-devel
編譯安裝
make && make install
安裝后配置
ln -s /usr/local/apache2.4/bin/apachectl /usr/local/bin/apachectl
ln -s /usr/local/apache2.4/bin/httpd /usr/local/bin/httpd
基本服務管理
apachectl start/stop/restart/status
# status 使用有問題,暫未解決
systemctl start 報錯 lynx 未找到:yum install lynx
start
報錯 AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using {ip}. Set the 'ServerName' directive globally to suppress this message
,配置文件中配置域名即可
/usr/local/apache2.4/conf/httpd.conf
ServerName domain.com:80
訪問網站頁面輸出:It works!
- 站點配置文件
mkdir /etc/httpd/conf.d
/etc/httpd/httpd.conf
IncludeOptional conf.d/*.conf
配置服務管理
cp /usr/local/apache2.4/bin/apachectl /etc/init.d/httpd
/etc/init.d/httpd 中
#!/bin/sh 下添加
# chkconfig: 35 70 30 # 這三項參數暫時不明原理
# description:...
chkfonfig --add httpd
此時可用 systemctl 管理 httpd
httpd 開機啟動
chkconfig --add httpd
后貌似已經可以開機自啟
已編譯安裝的 apache 添加模塊
該處涉及到 apache 模塊的動態加載和靜態加載,后續有需要再補充
目前的處理就是直接修改配置文件 /usr/local/apache2.4/conf/httpd.conf
查看 apache 加載的模塊
查看編譯時編譯的模塊,靜態加載的
apachectl -l
查看所有模塊,所有的
apachectl -t -D DUMP_MODULES
關聯 apache/httpd 和 php
重新完整編譯(成功),或僅添加參數編譯(未嘗試)
./configure --prefix=/usr/local/php7.4 --without-sqlite3 --without-pdo-sqlite
-enable-fpm --with-apxs2=/usr/local/apache2.4/bin/apxs/
執行 make
報錯
collect2: error: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1
執行 make clean
后重新 make && make install
(參考鏈接)
編譯后,在 /usr/local/apache2.4/modules/
目錄會多出 libphp7.so
,同時 /usr/local/apache2.4/conf/httpd.conf
最后會多出 LoadModule php7_module modules/libphp7.so
,再加上
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
並修改
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
編輯 /usr/local/apache2.4/hotdocs/index.php
<?php phpinfo(); ?>
進行關聯測試
yum 安裝 httpd
因為涉及 selinux 的相關配置不是很熟悉,又想用 selinux 的安全上下文等來限制 apache,而且在編譯安裝過程中對部分目錄和文件的作用比較熟悉了,最后采用了 yum 安裝的方式
yum install httpd -y
systemctl start httpd
systemctl enable httpd
# 禁用默認的測試頁面
# 將 /etc/httpd/conf.d/welcome.html 中的內容全部注釋,不要移除該文件,該文件中的注釋說了 升級的時候會重建該文件
# 修改網站目錄后后重新設置 selinux 安全上下文
chcon -u system_u -r object_r -t httpd_sys_content_t -Rv /srv/website/
# 添加配置文件后重新設置 selinux 安全上下文
chcon -u system_u -r object_r -t httpd_config_t -Rv /etc/httpd/conf.d/*.conf
chown -R 修改所屬用戶和組
chmod -R 修改詳細權限
cp -r 遞歸復制
其他相關問題
libsystemd
php 編譯安裝 --enable-fpm --with-fpm-systemd 報錯 no package 'libsystemd' found
yum install systemd-devel
主域名訪問正確,子域名訪問錯誤
IncludeOptional 方式時,配置文件路徑,采用全路徑試試
子域名訪問正確,主域名訪問錯誤
阿里雲 DNS 解析除設置 * 泛解析外,還需要增加主域名解析 @
ARP 未找到
yum install arp
找不到包:你確定需要的是 arp 而不是 apr ?
安裝 semanage
查找提供該功能的軟件包(參考鏈接)
yum provides semanage
yum install policycoreutils-python
semanage 相關命令
- 查看所有的安全上下文
semanage fcontext -l
getsebool
setsebool -P
mysqli 擴展
如 openssl,但是會出現加載失敗的問題,Unable to load dynamic library 'mysqli'
undefined symbol: mysqlnd_global_stats
,編譯添加的時候加上 --with-mysqli=/usr/bin/mysql_config
,可以通過 find 命令查找 mysql_config 路徑,目前不知道原來有沒有,因為在嘗試過程中執行了一次 yum install mysql-devel
(參考鏈接)
其他非相關臨時記錄
微信小程序測試跳過域名和 HTTPS 校驗
在微信開發者工具中,右上角詳情設置中,本地設置,勾選:不校驗合法域名、web-view(業務域名)、TLS 版本以及 HTTPS 證書,(參考鏈接 最下方)
跳過域名校驗
在微信開發者工具中,可以臨時開啟
開發環境不校驗請求域名、TLS版本及HTTPS證書
選項,跳過服務器域名的校驗。此時,在微信開發者工具中及手機開啟調試模式時,不會進行服務器域名的校驗。在服務器域名配置成功后,建議開發者關閉此選項進行開發,並在各平台下進行測試,以確認服務器域名配置正確。
如果手機上出現 “打開調試模式可以發出請求,關閉調試模式無法發出請求” 的現象,請確認是否跳過了域名校驗,並確認服務器域名和證書配置是否正確。
Wordpress 連接阿里雲 RDS 失敗
我的問題應該不是數據庫和連接的問題,通過查看 apache 站點日志,報錯是
[日期時間] [php7:warn] [pid...] [client...] PHP Warning: preg_match(): Allocation of JIT m
emory failed, PCRE JIT will be disabled. This is likely caused by security restrictions. Either grant PHP permission to allocate executa
ble memory, or set pcre.jit=0 in /../wp-includes/load.php on line 43
設置了 pcre.jit=0,無果,后猜到可能是 selinux 的權限問題,所以直接改了 yum 安裝的 httpd 為編譯安裝,這里后來反應過來應該先把 selinux 關掉試試,反正是成了
不行,又改回去了,不信試不成
- 正確原因,查看 selinux 日志
tail /var/log/audit/audit.log | grep httpd
type=AVC msg=audit(...): avc: denied { name_connect } for pid=... comm="httpd" dest=3306 scontext=system_u:system_rhttpd_t:s0 tcontext=system_u:object_r:mysqld_port_t:s0 tclass=tcp_socket permissive=0
根據其中一條大概猜是 selinux + httpd + mysql 的問題,因為未使用本地 mysql,檢索詞:selinux 允許 httpd 連接遠程數據庫,解決方案(參考鏈接):
setsebool -P httpd_can_network_connect=on