Ubuntu Apache PHP Pthreads

支持PHP7.0

安装前如果需要删除旧的话,如下操作
sudo apt-get autoremove php7*
sudo find /etc -name "*php*" |xargs rm -rf
sudo apt purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`

查看是否删除干净
dpkg -l | grep php7

下载PHP依赖库

sudo apt update && \
sudo apt install -y libzip-dev bison autoconf build-essential pkg-config git-core \
libltdl-dev libbz2-dev libxml2-dev libxslt1-dev libssl-dev libicu-dev \
libpspell-dev libenchant-dev libmcrypt-dev libpng-dev libjpeg8-dev \
libfreetype6-dev libmysqlclient-dev libreadline-dev libcurl4-openssl-dev libtidy-dev

安装Apache支持PHP配置 --with-apxs2=/usr/bin/apxs2

apt-get install apache2-dev


找到对应apxs位置,apxs是Apache1,apxs2是Apache2

find / -name '*apxs*'

编译安装PHP

-------------------------------------------------------------------------------------->

安装php7.3以上需要添加依赖

配置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

安装libsqlite3-dev

sudo apt-get install libsqlite3-dev

<--------------------------------------------------------------------------------------

下载PHP
在网站 https://www.php.net/releases/,需要哪个下载对应版本就行了,本地下载会比服务器快:
PHP5.6:
wget https://www.php.net/distributions/php-5.6.1.tar.gz

PHP7.2:
wget https://www.php.net/distributions/php-7.2.34.tar.gz

tar -zxvf php-7.2.34.tar.gz && cd php-7.2.34


如果要在安装的时候就自带其它扩展,则可编译前下载到扩展目录里,编译后就自动安装了

cd ext
获取pthreads扩展,对应PHP版本,这里下载pthreads3.0,下载其它分支使用
PHP5.6:
git clone -b PHP5 https://github.com/krakjoe/pthreads.git

PHP7.2:
git clone --recursive https://github.com/krakjoe/pthreads.git

cd ..

配置编译PHP,前三个配置对应的 /etc/php 是自定义目录
定义变量
PHP_VERSION=5
echo ${PHP_VERSION}

mkdir /etc
mkdir /etc/php${PHP_VERSION}
mkdir /etc/php${PHP_VERSION}/cli

./buildconf --force

./configure \
--prefix=/etc/php${PHP_VERSION} \
--with-config-file-path=/etc/php${PHP_VERSION}/cli \
--with-config-file-scan-dir=/etc/php${PHP_VERSION}/conf.d \
--disable-cgi \
--with-bz2 \
--with-zlib \
--enable-soap \
--enable-intl \
--with-openssl \
--with-curl \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-pcntl \
--enable-exif \
--with-xsl \
--enable-bcmath \
--enable-mbstring \
--enable-calendar \
--with-tidy \
--enable-maintainer-zts \
--enable-pthreads=shared \
--with-apxs2=/usr/bin/apxs2

$(nproc) 是动态获取核心数

make -j$(nproc) && sudo make install

sudo cp ./php.ini-production /etc/php${PHP_VERSION}/cli/php.ini

echo "extension=pthreads.so" | sudo tee -a /etc/php${PHP_VERSION}/cli/php.ini
echo "zend_extension=opcache.so" | sudo tee -a /etc/php${PHP_VERSION}/cli/php.ini

sudo ln -s /etc/php${PHP_VERSION}/bin/php /usr/bin/php${PHP_VERSION}

查看对应PHP版本
php5 -v
查看对应PHP版本支持的扩展
php5 -r "var_dump(class_exists('Thread'));"

<--------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------->

善后工作安装完成后,可能Apache会默认指向新安装的PHP,配置Apache指向PHP相关
先禁用旧的对应PHP版本
sudo a2dismod php5
启用需要的PHP版本
sudo a2enmod php7

sudo /etc/init.d/apache2 restart

重启 Apache 这个时候发现,HTML 解析正常 PHP 却输出了源码,这表示 PHP 并无被正确的解析。这个时候去

cd /etc/apache2/mods-available/

看发现只有 php7.load 没有发现 php7.conf (这里是经过对比个人 Ubuntu Desktop 发现的,上面是经过 apt 安装的);因此在目录下建立文件 php7.conf

<FilesMatch ".+\.ph(p[3457]?|t|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch ".+\.phps$">
    SetHandler application/x-httpd-php-source
    # Deny access to raw php sources by default
    # To re-enable it's recommended to enable access to the files
    # only in specific virtual host or directory
    Require all denied
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(p[3457]?|t|tml|ps)$">
    Require all denied
</FilesMatch>

# Running PHP scripts in user directories is disabled by default

# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_flag engine Off
    </Directory>
</IfModule>

重复善后配置
<--------------------------------------------------------------------------------------


配置Redis

git clone https://github.com/phpredis/phpredis.git
(PHP5对应redis:https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz)
cd phpredis
/usr/bin/phpize
(给对应版本PHP配置的话对应PHP执行:/etc/php5/bin/phpize)
./configure --with-php-config=/usr/bin/php-config && make && make install
(给对应版本PHP配置的话对应PHP执行:/etc/php5/bin/php-config)
echo "extension=redis.so" | sudo tee -a /etc/php/cli/php.ini
(给对应版本PHP配置的话对应PHP执行:/etc/php5/cli/php.ini)

查看对应PHP版本支持的扩展
php5 -r "var_dump(class_exists('Redis'));"

参考来源:

Ubuntu 编译安装 PHP7.2 + 编译 pthreads - JavaShuo

Ubuntu 20.04 LTS下编译PHP 7.2 ZTS并启用pthread线程库 – 悠然品鉴-

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值