CentOS 8.X安装PHP

系统:CentOS 8.4
PHP:PHP 7.4

1.安装依赖

yum -y install gcc gcc-c++ libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel libicu-devel freetype-devel openldap-devel openldap openldap-devel git sqlite-devel autoconf automake libtool

2.创建用户

# 如果和nginx配置使用,php和nginx保持一致
groupadd www
useradd -g www -s /sbin/nologin www

3.编译前准备

# 拷贝lib库
cp -frp /usr/lib64/libldap* /usr/lib/
# 安装oniguruma,见下文参见问题栏目
oniguruma

4.下载和安装PHP

# 下载
wget https://www.php.net/distributions/php-7.4.2.tar.gz --no-check-certificate
# 解压
tar -zxvf php-7.4.2.tar.gz

cd php-7.4.2
# 编译前配置
./configure --prefix=/usr/local/php \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd-compression-support \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--with-mcrypt \
--with-libmbfl \
--enable-ftp \
--with-gd \
--enable-gd-jis-conv \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--with-pear \
--enable-maintainer-zts \
--with-ldap=shared \
--without-gdbm

# 编译安装
make && make install

5.建立软链

ln -s /usr/local/php/sbin/* /usr/local/sbin/
ln -s /usr/local/php/bin/* /usr/local/bin/

6.添加配置文件

# 默认php.ini配置文件
cp php.ini-development /usr/local/php/etc/php.ini
# 默认php-fpm.conf配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

7.配置php-fpm

vim /usr/local/php/etc/php-fpm.d/www.conf

# 用户和nginx中使用的用户要保持一致
listen.owner = www
listen.group = www
listen.mode = 0660
# 修改监听
listen = /tmp/php-cgi.sock

# listen = 127.0.0.1:9000
# 验证php-fpm启动是否成功
# telnet 127.0.0.1 9000

8.开机启动

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on

#启动php-fpm
service php-fpm start

nginx配置

location ~ \.php$ {
        # 配置网站目录
    root           /usr/local/nginx/html;
    # 配置php-fpm地址
    fastcgi_pass unix:/tmp/fastcgi.socket;
    fastcgi_index  index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

测试

1.phpinfo测试(phpinfo.php)

<?php
phpinfo();
?>

2.php连接MySQL测试(php_mysql_test.php)

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "111111";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>

将要phpinfo.phpphp_mysql_test.php放到网站目录下

卸载php

rm -rf /usr/local/php /bin/php /usr/local/sbin/php-fpm /usr/local/bin/php /usr/local/bin/pear /usr/local/bin/peardev /usr/local/bin/pecl /usr/local/bin/phar /usr/local/bin/phar.phar /usr/local/bin/php-cgi /usr/local/bin/php-config /usr/local/bin/phpdbg /usr/local/bin/phpize

常见错误:

1.提示错误:`configure: error: Package requirements (oniguruma) were not met:
Package 'oniguruma', required by 'virtual:world', not found`

解决办法:

# 下载oniguruma
git clone https://github.com/kkos/oniguruma.git
# 国内镜像
git clone https://gitee.com/mirrors/oniguruma.git

# 删除yum安装的oniguruma
yum remove onigurma*

# 编译安装
cd oniguruma/
./autogen.sh
./configure --prefix=/usr
make && make install

# 配置环境变量(可选)
vim /etc/profile
export ONIG_CFLAGS=/usr/local/oniguruma/bin
export ONIG_LIBS=/usr/local/oniguruma/lib
source /etc/profile

如果通过访问github很慢,配置下hosts

vim /etc/hosts

13.229.188.59   github.com

2.报错configure: error: Cannot find ldap libraries in /usr/lib.

cp -frp /usr/lib64/libldap* /usr/lib/

3.启动php-fpm报错(service php-fpm start

WARNING: Nothing matches the include pattern '/usr/local/php/etc/php-fpm.d/*.conf' from /usr/local/php/etc/php-fpm.conf at line 143.
ERROR: No pool defined. at least one pool section must be specified in config file
ERROR: failed to post process the configuration
ERROR: FPM initialization failed

解决方法:

cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
/usr/local/php/sbin/php-fpm -t

4.提示ERROR: [pool www] cannot get uid for user 'www'

没创建www用户

5.提示connect() to unix:/tmp/php-cgi.sock failed (13: Permission denied) while connecting to upstream

vim /usr/local/php/etc/php-fpm.d/www.conf

和nginx使用的用户保持一致

# 和nginx的nginx.conf配置的user  www www;保持一致
listen.owner = www
listen.group = www
listen.mode = 0660

重启php-fpm

service php-fpm restart

参考文章:

Centos8(Liunx) 中安装PHP7.4 的三种方法和删除它的三种方法
centos7 编译安装php 5.6.40

如果觉得我的文章对你有用,请随意赞赏