一、下载安装包
wget https://mirrors.sohu.com/php/php-7.4.9.tar.gz
二、解压并进入 解压目录
tar zxvf php-7.4.9.tar.gz && cd php-7.4.9
三、预编译、编译、安装
./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc --with-mhash --with-openssl --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib --enable-bcmath --enable-gd --with-jpeg --with-freetype --enable-mbstring --enable-ftp --enable-sockets --with-gettext --enable-session --with-curl --enable-fpm --with-fpm-user=php --with-fpm-group=php --enable-pdo -enable-tokenizer --with-zip
make && make install
注意:在第三步预编译过程中通常会有很多报错,其中比较难处理的是
configure: error: Package requirements (oniguruma) were not met
oniguruma是一个处理正则表达式的库,在编译安装php时,如果使用–enable-mbstring 参数, 开启mbstring扩展,则会出现这个错误。
原因:mbstring的正则功能需要oniguruma的支持,系统中却没有oniguruma库。
解决办法:
1、在’–enable-mbstring’参数后添加’–disable-mbregex’参数,意为不使用mbstring的正则功能,不再需要oniguruma库,或者去掉该参数也可以。
2、安装oniguruma库
wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
tar -zxf oniguruma-6.9.4.tar.gz
cd oniguruma-6.9.4
./autogen.sh && ./configure --prefix=/usr --libdir=/lib64
make && make install
在使用./configure时添加‘–libdir=/lib64’参数,重新编译PHP,如果不使用该参数则仍会报错。
其它报错,如缺XXX包,基本上通过yum install -y XXX-devel安装成功,然后重新预编译即可解决
四、给配置文件改名,否则启动时会报错
cd /usr/local/php7/etc/
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf
五、给php命令设置软链接
ln -s /usr/local/php7/bin/* /usr/local/bin
ln -s /usr/local/php7/sbin/* /usr/local/sbin
六、查看php的版本和支持的模块
php -v
php -m
七、配置nginx的php网页
vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}