编译前准备
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官网下载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,源代码方式安装,总需要配置很多参数。这里列出常用配置参数,并详细用中文解释说明了。
给大家一些参考:
--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文件目录,优化支持
--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 #关闭iconv函数,种字符集间的转换
--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字符串函数库
--with-curl #打开curl浏览工具的支持
--with-curlwrappers #运用curl工具打开url流
--with-ttf #打开freetype1.*的支持,可以不加了
--with-xsl #打开XSLT 文件支持,扩展了libXML2库,需要libxslt软件
--with-gettext #打开gnu 的gettext 支持,编码库用到
--with-pear #打开pear命令的支持,PHP扩展用的
--enable-calendar #打开日历扩展功能
--enable-mbstring #多字节,字符串的支持
--enable-bcmath #打开图片大小调整,用到zabbix监控的时候用到了这个模块
--enable-sockets #打开 sockets 支持
--enable-exif #图片的元数据支持
--enable-magic-quotes #魔术引用的支持
--disable-rpath #关闭额外的运行库文件
--disable-debug #关闭调试模式
--with-mime-magic=/usr/share/file/magic.mime #魔术头文件位置
CGI方式安装才用的参数
--enable-fpm #打上PHP-fpm 补丁后才有这个参数,CGI方式安装的启动程序
--enable-fastCGI #支持fastcgi方式启动PHP
--enable-force-CGI-redirect #同上 ,帮助里没有解释
--with-ncurses #支持ncurses 屏幕绘制以及基于文本终端的图形互动功能的动态库
--enable-pcntl #freeTDS需要用到的,可能是链接mssql 才用到
mhash和mcrypt算法的扩展
--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的多字节
--enable-mbregex
--enable-wddx
--enable-shmop
--enable-soap编译PHP的时候慎用 –with-curlwrappers参数
编译
在这里可以用各种各样的参数来自定义 PHP,例如启动哪些扩展功能包的支持等。用./configure --help命令可以列出当前可用的所有参数。
./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)所遇到的主要就是这两个问题,对于其他的报错,都是依赖的问题,根据对应的提示安装相应的依赖就行了。
以下是预编译完成后PHP所无法识别的扩展,也就是,这些扩展在预编译的时候就不要选上了:
【警示】以下是不可用选项
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
操作成功结果:
安装
make && make install
配置PHP环境变量
vim /etc/profile #打开profile文件增加如下,然后使文件生效
PATH=/usr/local/php-7.4.1/bin:/usr/local/php-7.4.1/sbin:$PATH
使环境变量生效
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 #查看状态
检测端口是否监听
ss -ntulp | grep php-fpm
演示YUM安装PHP7版本
1、安装前准备
更新系统软件仓库不更新内核,安装开发工具包
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相关依赖库和模块
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安装是否成功
安装完,执行如下命令:
php --version #查看版本
php -m #查看安装模块,需要用到的模块,没有安装的需要YUM安装
systemctl enable --now php-fpm #设置开机启动,并启动。
ss -ntulp | grep php-fpm
优化PHP
1、调整PHP-FPM文件
vim /etc/php-fpm.conf
log_level = error # 指定要记录的php-fpm日志级别
rlimit_files = 32768 # 为主进程设置打开文件的限制,虚拟机无法设置。
events.mechanism = epoll # 指定事件驱动模型
2、调整PHP.ini文件
隐藏后端使用的真正脚本类型,扰乱入侵者的渗透思路,另外,切记不要把敏感数据直接明文存在session中,有泄露风险。
vim /etc/php.ini
error_reporting = E_WARING & ERROR #设置php的错误报告级别,只需要报告警告和错误即可
error_log = /var/log/php_errors.log #指定php错误日志存放位置
log_errors_max_len = 2048 #指定php错误日志的最大长度
expose_php = off #隐藏php的详细版本号
session.name = JSESSIONID #表示jsp程序,php的则是PHPSESSID
修改完配置文件需要重新启动才可以生效
systemctl restart php-fpm