简介

PHP是一种流行的通用脚本语言,特别适合于Web开发。快速,灵活和实用的PHP为您的博客到世界上最受欢迎的网站提供了强大的支持。

个人笔记方便下次使用,各种面板用户请勿阅读!

以下操作均在 root 用户下

安装编译器、依赖

安装EPEL软件源

yum install -y epel-release

安装 GCC 编译器

yum install -y gcc gcc-c++ 

安装系统依赖

yum install -y libxml2-devel openssl-devel libcurl-devel libicu-devel libxslt-devel postgresql-devel sqlite-devel libc-client libc-client-devel oniguruma-devel libsodium-devel enchant-devel gmp-devel net-snmp-devel libtidy-devel libzip-devel bzip2-devel libffi-devel libwebp-devel openldap-devel gd-devel freetype-devel

下载 PHP7.4.9 源码

php源码官网:

下载并解压php源码

wget https://www.php.net/distributions/php-7.4.9.tar.xz
tar xf php-7.4.9.tar.xz

编译安装

进入源码目录,生成 Makefile (只要严格按照上面说明安装依赖,此步骤应该不会出错)

cd php-7.4.9

./configure \
  --prefix=/opt/php \
  --with-config-file-path=/opt/php/etc \
  --with-config-file-scan-dir=/opt/php/etc/php.d \
  --enable-fpm \
  --with-fpm-user=www \
  --with-fpm-group=www \
  --enable-mysqlnd \
  --with-mysqli=mysqlnd \
  --with-pdo-mysql=mysqlnd \
  --with-pdo-sqlite \
  --with-pdo-pgsql \
  --with-mysql-sock=/var/lib/mysql/mysql.sock \
  --with-pgsql \
  --enable-pdo \
  --with-zlib \
  --enable-xml \
  --enable-session \
  --enable-bcmath \
  --enable-shmop \
  --enable-sysvsem \
  --with-curl \
  --enable-mbregex \
  --enable-mbstring \
  --enable-intl \
  --enable-ftp \
  --enable-gd \
  --with-webp \
  --with-jpeg \
  --with-freetype \
  --with-mhash \
  --enable-pcntl \
  --enable-sockets \
  --enable-soap \
  --with-gettext \
  --enable-opcache \
  --with-xsl \
  --enable-tokenizer \
  --with-imap \
  --with-imap-ssl \
  --with-libdir=lib64 \
  --with-kerberos \
  --with-ffi \
  --with-ldap \
  --with-sodium \
  --with-openssl-dir \
  --with-openssl \
  --enable-dba \
  --with-enchant \
  --with-pcre-jit \
  --with-bz2 \
  --enable-calendar \
  --with-gmp \
  --enable-exif \
  --with-snmp \
  --with-tidy

编译 & 安装

make -j$(nproc --all)
make install

拷贝配置文件

cp php.ini-production /opt/php/etc/php.ini
mv /opt/php/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf
mv /opt/php/etc/php-fpm.d/www.conf.default /opt/php/etc/php-fpm.d/www.conf

# 删除pid路径注释符,防止 reload 服务出错
sed -i 's/^;pid =/pid =/' /opt/php/etc/php-fpm.conf

拷贝 systemd 服务脚本

cp sapi/fpm/php-fpm.service /lib/systemd/system/php-fpm.service

开启 OPcache 以下均为命令行操作,复制执行即可

mkdir -p /opt/php/etc/php.d/

cat > /opt/php/etc/php.d/10-opcache.ini <<EOF
; Enable Zend OPcache extension module
zend_extension=opcache

; Determines if Zend OPCache is enabled
opcache.enable=1

; The OPcache shared memory storage size.
opcache.memory_consumption=128

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=8

; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 1000000 are allowed.
opcache.max_accelerated_files=10000

; Enables or disables copying of PHP code (text segment) into HUGE PAGES.
; This should improve performance, but requires appropriate OS configuration.
opcache.huge_code_pages=0
EOF

添加 imagick 扩展(可选)

这个是图像处理扩展,用于图床等程序使用,可根据实际情况选择安装

pecl imagick 源码地址:

安装 ImageMagick 依赖 & autoconf 脚本生成工具

yum install -y autoconf ImageMagick-devel

安装 imagick 扩展

# 下载并解压源码
wget https://pecl.php.net/get/imagick-3.4.4.tgz
tar xvf imagick-3.4.4.tgz

# 进入源码目录
cd imagick-3.4.4

# 生成 configure 配置文件
/opt/php/bin/phpize

# 编译安装
./configure --with-php-config=/opt/php/bin/php-config
make -j$(nproc --all) && make install

启用 imagick 扩展

cat > /opt/php/etc/php.d/20-imagick.ini <<EOF
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

extension=imagick
EOF

大功告成!!!

php 配置相关设置都在 /opt/php/etc 目录,根据自己的要求进行配置即可,或啥都不改直接用!

systemctl start php-fpm  # 启动 php-fpm 服务
systemctl stop php-fpm  # 停止 php-fpm 服务
systemctl restart php-fpm  # 重启 php-fpm 服务
systemctl reload php-fpm  # 重载 php-fpm 服务

systemctl enable php-fpm  # 添加开机启动
最后修改:2021 年 09 月 08 日
如果觉得我的文章对你有用,请随意赞赏