編譯前準(zhǔn)備
yum --exclude=kernel* update -y
yum groupinstall -y 'Development Tools'
yum install -y epel-release
yum install -y wget openssl-devel bzip2-devel libffi-devel sqlite-devel vim gpm-libs oniguruma oniguruma-devel libsodium libsodium-devel xz-devel libxml2-devel libcurl-devel
yum install -y bash-completion bash-completion-extras libicu-devel libjpeg libjpeg-devel nss_ldap cmake boost-devel libevent libevent-devel gd gd-devel openjpeg-devel
yum install -y libgcrypt-devel libpng-devel libgpg-error-devel libxslt-devel cmake libmcrypt-devel libmcrypt recode-devel recode
安裝高版本libzip
yum remove -y libzip-devel
yum install -y http://rpms.remirepo.net/enterprise/7/remi/x86_64/libzip5-1.9.2-3.el7.remi.x86_64.rpm
yum install -y http://rpms.remirepo.net/enterprise/7/remi/x86_64/libzip5-devel-1.9.2-3.el7.remi.x86_64.rpm
編譯安裝源碼
PHP官網(wǎng)下載PHP源碼
wget https://www.php.net/distributions/php-7.4.1.tar.gz
tar -zvxf php-7.4.1.tar.gz && cd php-7.4.1/
mkdir -p /usr/local/php-7.4.1 && useradd -s /sbin/nologin www
在Linux下安裝PHP俐填,源代碼方式安裝静稻,總需要配置很多參數(shù)捅膘。這里列出常用配置參數(shù)娃闲,并詳細(xì)用中文解釋說明了忆矛。
給大家一些參考:
--prefix=/usr/local/PHP #php 安裝目錄
--with-apxs2=/usr/local/apache/bin/apxs #表示使用apache2.0以上版本媳维,apxs表示使用的apache1.0版本 --with-fpm-user=www #指定所屬用戶 --with-fpm-grop=www #指定所屬組
--with-config-file-path=/usr/local/PHP/etc #指定php.ini位置
--with-MySQL=/usr/local/mysql #mysql安裝目錄攻晒,對mysql的支持
--with-MySQLi=/usr/local/mysql/bin/mysql_config #mysqli文件目錄,優(yōu)化支持
--enable-safe-mode #打開安全模式
--enable-ftp #打開ftp的支持
--enable-zip #打開對zip的支持
--with-bz2 #打開對bz2文件的支持
--with-jpeg-dir #打開對jpeg圖片的支持
--with-png-dir #打開對png圖片的支持
--with-freetype-dir #打開對freetype字體庫的支持
--without-iconv #關(guān)閉iconv函數(shù)寂嘉,種字符集間的轉(zhuǎn)換
--with-libXML-dir #打開libxml2庫的支持
--with-XMLrpc #打開xml-rpc的c語言
--with-zlib-dir #打開zlib庫的支持
--with-gd #打開gd庫的支持奏瞬,7.4 后不再支持這種寫法枫绅,所以需要改為 --enable-gd
--enable-gd-native-ttf #支持TrueType字符串函數(shù)庫
--with-curl #打開curl瀏覽工具的支持
--with-curlwrappers #運(yùn)用curl工具打開url流
--with-ttf #打開freetype1.*的支持,可以不加了
--with-xsl #打開XSLT 文件支持硼端,擴(kuò)展了libXML2庫并淋,需要libxslt軟件
--with-gettext #打開gnu 的gettext 支持,編碼庫用到
--with-pear #打開pear命令的支持珍昨,PHP擴(kuò)展用的
--enable-calendar #打開日歷擴(kuò)展功能
--enable-mbstring #多字節(jié)县耽,字符串的支持
--enable-bcmath #打開圖片大小調(diào)整,用到zabbix監(jiān)控的時候用到了這個模塊
--enable-sockets #打開 sockets 支持
--enable-exif #圖片的元數(shù)據(jù)支持
--enable-magic-quotes #魔術(shù)引用的支持
--disable-rpath #關(guān)閉額外的運(yùn)行庫文件
--disable-debug #關(guān)閉調(diào)試模式
--with-mime-magic=/usr/share/file/magic.mime #魔術(shù)頭文件位置
CGI方式安裝才用的參數(shù)
--enable-fpm #打上PHP-fpm 補(bǔ)丁后才有這個參數(shù),CGI方式安裝的啟動程序
--enable-fastCGI #支持fastcgi方式啟動PHP
--enable-force-CGI-redirect #同上 ,幫助里沒有解釋
--with-ncurses #支持ncurses 屏幕繪制以及基于文本終端的圖形互動功能的動態(tài)庫
--enable-pcntl #freeTDS需要用到的镣典,可能是鏈接mssql 才用到
mhash和mcrypt算法的擴(kuò)展
--with-mcrypt #算法
--with-mhash #算法
--with-gmp
--enable-inline-optimization
--with-openssl #openssl的支持兔毙,加密傳輸時用到的
--enable-dbase
--with-pcre-dir=/usr/local/bin/pcre-config #perl的正則庫案安裝位置
--disable-dmalloc
--with-gdbm #dba的gdbm支持
--enable-sigchild
--enable-sysvsem
--enable-sysvshm
--enable-zend-multibyte #支持zend的多字節(jié)
--enable-mbregex
--enable-wddx
--enable-shmop
--enable-soap編譯PHP的時候慎用 –with-curlwrappers參數(shù)
編譯
在這里可以用各種各樣的參數(shù)來自定義 PHP,例如啟動哪些擴(kuò)展功能包的支持等兄春。用./configure --help命令可以列出當(dāng)前可用的所有參數(shù)澎剥。
./configure \
--prefix=/usr/local/php-7.4.1 \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-config-file-path=/usr/local/php-7.4.1/etc \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--enable-pdo \
--with-zlib \
--enable-xml \
--enable-session \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-ftp \
--with-zip \
--with-ffi \
--with-zlib-dir \
--with-openssl \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--with-gettext \
--enable-fileinfo \
--enable-opcache \
--enable-maintainer-zts \
--with-xsl \
--with-sodium \
--enable-tokenizer
如果報錯
error: Package requirements (sqlite3 > 3.7.4) were not met
處理yum install -y sqlite-devel
error: Package requirements (oniguruma) were not met
處理yum install -y oniguruma oniguruma-devel
,如果這兩個包安裝不了赶舆,安裝如下依賴庫yum -y install epel-release
注意:
編譯安裝(或升級PHP7.4)所遇到的主要就是這兩個問題哑姚,對于其他的報錯,都是依賴的問題芜茵,根據(jù)對應(yīng)的提示安裝相應(yīng)的依賴就行了叙量。
以下是預(yù)編譯完成后PHP所無法識別的擴(kuò)展,也就是九串,這些擴(kuò)展在預(yù)編譯的時候就不要選上了:
【警示】以下是不可用選項(xiàng)
configure: WARNING: unrecognized options:
--with-libxml-dir,
--with-pcre-regex,
--with-pcre-dir,
--with-gd,
--with-jpeg-dir,
--with-png-dir,
--with-freetype-dir,
--enable-mbregex-backtrack,
--with-onig,
--enable-wddx,
--with-libxml-dir,
--enable-zip
操作成功結(jié)果:
安裝
make && make install
配置PHP環(huán)境變量
vim /etc/profile #打開profile文件增加如下绞佩,然后使文件生效
PATH=/usr/local/php-7.4.1/bin:/usr/local/php-7.4.1/sbin:$PATH
使環(huán)境變量生效
source /etc/profile
檢查是否安裝成功
配置PHP-FPM
[root@php php-7.4.1]# pwd
/root/php-7.4.1
把所需的幾個配置文件放到需要的位置
cp php.ini-production /usr/local/php-7.4.1/etc/php.ini
cp /usr/local/php-7.4.1/etc/php-fpm.conf.default /usr/local/php-7.4.1/etc/php-fpm.conf
cp /usr/local/php-7.4.1/etc/php-fpm.d/www.conf.default /usr/local/php-7.4.1/etc/php-fpm.d/www.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
啟動PHP-FPM
/etc/init.d/php-fpm start #啟動
/etc/init.d/php-fpm restart #重新啟動
/etc/init.d/php-fpm reload #重新加載
/etc/init.d/php-fpm stop #停止
/etc/init.d/php-fpm status #查看狀態(tài)
檢測端口是否監(jiān)聽
ss -ntulp | grep php-fpm
演示YUM安裝PHP7版本
1、安裝前準(zhǔn)備
更新系統(tǒng)軟件倉庫不更新內(nèi)核蒸辆,安裝開發(fā)工具包
yum --exclude=kernel* update -y
yum groupinstall -y 'Development Tools'
2征炼、添加epel第三方依賴庫,安裝YUM源
yum -y install epel-release yum-utils
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
3躬贡、安裝PHP相關(guān)依賴庫和模塊
yum install -y --enablerepo=remi --enablerepo=remi-php74 mod_php php-gd* php php-opcache php-mbstring php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-fpm
yum install -y --enablerepo=remi --enablerepo=remi-php74 php-cli php-redis php-pecl-mcrypt php-process php-devel php-pear
yum install -y vim libevent-devel libmcrypt-devel libxml2 libxml2-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel libxslt libxslt-devel cyrus-sasl-plain cyrus-sasl cyrus-sasl-devel cyrus-sasl-lib openssl openssl-devel pcre pcre-devel zlib zlib-devel wget net-tools
4谆奥、檢查YUM安裝是否成功
安裝完,執(zhí)行如下命令:
php --version #查看版本
php -m #查看安裝模塊拂玻,需要用到的模塊酸些,沒有安裝的需要YUM安裝
systemctl enable --now php-fpm #設(shè)置開機(jī)啟動,并啟動檐蚜。
ss -ntulp | grep php-fpm
優(yōu)化PHP
1魄懂、調(diào)整PHP-FPM文件
vim /etc/php-fpm.conf
log_level = error # 指定要記錄的php-fpm日志級別
rlimit_files = 32768 # 為主進(jìn)程設(shè)置打開文件的限制,虛擬機(jī)無法設(shè)置闯第。
events.mechanism = epoll # 指定事件驅(qū)動模型
2市栗、調(diào)整PHP.ini文件
隱藏后端使用的真正腳本類型,擾亂入侵者的滲透思路,另外,切記不要把敏感數(shù)據(jù)直接明文存在session中,有泄露風(fēng)險。
vim /etc/php.ini
error_reporting = E_WARING & ERROR #設(shè)置php的錯誤報告級別,只需要報告警告和錯誤即可
error_log = /var/log/php_errors.log #指定php錯誤日志存放位置
log_errors_max_len = 2048 #指定php錯誤日志的最大長度
expose_php = off #隱藏php的詳細(xì)版本號
session.name = JSESSIONID #表示jsp程序,php的則是PHPSESSID
修改完配置文件需要重新啟動才可以生效
systemctl restart php-fpm