WordPress阿里云轻量应用服务器升级PHP版本

释放双眼,带上耳机,听听看~!

注意事项

  • 建议使用默认的环境操作
  • WordPress镜像用的是4.8.1这个版本

安装依赖

yum install -y libxml2-devel openssl-devel  curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel xslt libxslt-devel
yum update -y curl curl-devel
yum remove -y libzip

配置SWAP

如果内存是低于1GB的,则需要配置一下swap,编译过程需要依赖更多内存,内存太低会报错

dd if=/dev/zero of=/swap bs=1M count=2048
mkswap -f /swap
swapon /swap

安装libzip

PHP7.3要求libzip版本大于0.11,但yum安装版本达不到要求,所以这里手动编译一下,该项不满足报错对应如下:
checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11

wget https://nih.at/libzip/libzip-1.2.0.tar.gz
tar -zxvf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure
make && make install

编译好后还需要做一下软连接,否则会提示报错找不到zipconf.h文件

ln -s /usr/local/lib/libzip/include/zipconf.h /usr/local/include

编译安装PHP

wget https://www.php.net/distributions/php-7.3.9.tar.gz
tar -xvf php-7.3.9.tar.gz
cd php-7.3.9
./configure --prefix=/usr/local/php73  --enable-soap --enable-cgi --with-mysql=/usr/local/mysql --with-mysqli=mysqlnd --with-gd --with-pdo-mysql=mysqlnd --with-zlib --enable-zip --enable-fpm --without-pear --disable-phar --with-openssl --enable-mbstring=all --with-jpeg-dir=/usr --with-png-dir=/usr --with-curl --with-freetype-dir=/usr --enable-gd-native-ttf --with-xsl=/usr --enable-calendar --enable-exif --enable-ftp --with-iconv --enable-bcmath --with-mcrypt=/usr/local/libmcrypt --enable-opcache
make && make install

这个编译过程会比较长,编译完成后修改一下配置文件

# 将启动脚本复制到init.d中,${PHP73}对应是源码解压的目录
cp ${PHP73}/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm73
# 给启动脚本加上执行权限
chmod +x /etc/init.d/php-fpm73
# 将默认配置文件复制为.conf文件
cp /usr/local/php73/etc/php-fpm.conf.default ${PHP73_DIR}/etc/php-fpm.conf
# 添加pool的配置
cat << EOF > /usr/local/php73/etc/php-fpm.d/www.conf
[www]
listen = /home/www/logs/php73-fpm.sock
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 128
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 300
rlimit_files = 1024
slowlog = /home/www/logs/php73-fpm-slow.log
EOF

至此PHP已经编译配置完成,接下来需要修改apache使用新版的php

修改apache

镜像中默认是用的php-fpm,使用的是socket方式的监听,Apache对应配置文件/usr/local/apache/conf/httpd.conf,可以看到其中配置如下

WordPress阿里云轻量应用服务器升级PHP版本插图

我们只需要修改其中socket的文件路径指向到新版本的PHP就可以了,在/usr/local/php73/etc/php-fpm.d/www.conf有指定,新的配置到/home/www/logs/php73-fpm.sock即可,如图

WordPress阿里云轻量应用服务器升级PHP版本插图(1)

重启服务

# 停止旧版本的PHP(实际不停止也不影响,停止可以减少一些系统资源占用)
/etc/init.d/php-fpm stop
# 启动新版PHP-FPM
/etc/init.d/php-fpm73 start
# 重启apache
/etc/init.d/apachectl restart

验证

站点根目录是/home/www/htdocs/,放入phpinfo探针即可检测PHP版本

WordPress阿里云轻量应用服务器升级PHP版本插图(2)

至此轻量应用服务器wordpress镜像更新PHP版本操作已完成,可以愉快的更新wordpress到新版本了。为了操作更简单,这里总结成了一个脚本,可以复制到主机内一键执行

#!/bin/bash
WORKDIR=/tmp/
PHP73_DIR=/usr/local/php73
DEFAULT_SWAP=0

createSwap(){
  if [ `cat /proc/meminfo | grep SwapTotal | awk -F " " '{print $2}'` -ne 0 ]
  then
    return 0
  fi
  if [ `cat /proc/meminfo | grep MemTotal | awk -F " " '{print $2}'` -le 1048576 ]
  then
    echo "Mem lower than 1GB,creating swap..."
    dd if=/dev/zero of=/swap bs=1M count=2048
    mkswap -f /swap
    swapon /swap && echo "SWAP create success."
    DEFAULT_SWAP=1
  fi
}

installDependence(){
  yum install -y libxml2-devel openssl-devel  curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel xslt libxslt-devel
  yum update -y curl curl-devel
  yum remove -y libzip
}

compileLibzip(){
  echo "start install libzip."
  cd ${WORKDIR}
  if [ -f libzip-1.2.0.tar.gz ]
  then
    rm -rf libzip-1.2.0.tar.gz
  fi
  wget https://nih.at/libzip/libzip-1.2.0.tar.gz
  tar -zxvf libzip-1.2.0.tar.gz
  cd libzip-1.2.0
  ./configure
  make && make install
  if [ $? -ne 0 ]
  then
    echo "libzip install failed."
    exit 127
  fi
  ln -s /usr/local/lib/libzip/include/zipconf.h /usr/local/include
}

installPHP(){
  echo "Install PHP 7.3"
  cd ${WORKDIR}
  if [ -f php-7.3.9.tar.gz ]
  then
    rm -rf php-7.3.9.tar.gz
  fi
  wget https://www.php.net/distributions/php-7.3.9.tar.gz
  tar -xvf php-7.3.9.tar.gz
  cd php-7.3.9
  ./configure --prefix=/usr/local/php73  --enable-soap --enable-cgi --with-mysql=/usr/local/mysql --with-mysqli=mysqlnd --with-gd --with-pdo-mysql=mysqlnd --with-zlib --enable-zip --enable-fpm --without-pear --disable-phar --with-openssl --enable-mbstring=all --with-jpeg-dir=/usr --with-png-dir=/usr --with-curl --with-freetype-dir=/usr --enable-gd-native-ttf --with-xsl=/usr --enable-calendar --enable-exif --enable-ftp --with-iconv --enable-bcmath --with-mcrypt=/usr/local/libmcrypt --enable-opcache
  make && make install
  if [ $? -ne 0 ]
  then
    echo "PHP install failed."
    exit 127
  fi
  cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm73
  cp ${PHP73_DIR}/etc/php-fpm.conf.default ${PHP73_DIR}/etc/php-fpm.conf
  chmod +x /etc/init.d/php-fpm73
}

createConfig(){

cat << EOF > ${PHP73_DIR}/etc/php-fpm.d/www.conf
[www]
listen = /home/www/logs/php73-fpm.sock
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 128
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 300
rlimit_files = 1024
slowlog = /home/www/logs/php73-fpm-slow.log
EOF

}

modifyApache(){
  sed -i 's#ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/home/www/logs/php-fpm.sock|fcgi://127.0.0.1/home/www/htdocs#ProxyPassMatch ^/(.*\\.php(/.*)?)$ unix:/home/www/logs/php73-fpm.sock|fcgi://127.0.0.1/home/www/htdocs#g' /usr/local/apache/conf/httpd.conf
}

restartService(){
  chkconfig php-fpm off
  chkconfig php-fpm73 on
  /etc/init.d/php-fpm stop
  /etc/init.d/php-fpm73 start
  /etc/init.d/apachectl restart
}

delSwap(){
  if [ ${DEFAULT_SWAP} -eq 1 ]
  then
    swapoff /swap
    rm -rf /swap
  fi
}

createSwap
installDependence
compileLibzip
installPHP
createConfig
modifyApache
restartService
delSwap
WordPress阿里云轻量应用服务器升级PHP版本插图(3)
WordPress阿里云轻量应用服务器升级PHP版本插图(4)

阿里云新用户建站,如何享受九大优惠

我是一个新手站长,在建站过程中也摸索了不少经验,其中就包括买服务器、买域名、搭建自己的网站、服务器管理,等等,对于一个不懂的人来说可能要花费很多时间,...

人已赞赏
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧