PHPY是什么?
phpy 是识沃团队最新推出的开源项目,目标是为 PHP 引入 Python 生态,来弥补 PHP 生态的空缺和不足。phpy 使得 PHP 可以调用所有 Python 的包。
包括当下非常流行的 AI 库,如:PyTorch、transformers、TensorFlow 等包括当下非常流行的 AI 库,如 PyTorch、transformers、TensorFlow 等,以及科学计算库,如 Numpy、Pandas、Scikit 等,还可以使用图形界面库,如 PyQt、wxPython 等。
不建议在 php-fpm/apache 短生命周期运行环境下使用,频繁地导入/销毁模块的开销会消耗大量资源
环境
- Linux环境:Ubuntu 22.04.3 LTS
- PHP版本:PHP 8.1 或以上版本
- Python 3.10 或以上版本
安装PHP8.3
下载
代码语言:javascript复制wget wget https://www.php.net/distributions/php-8.3.0.tar.gz
tar -zxvf php-8.3.0.tar.gz
下载地址:https://www.php.net/downloads
安装依赖包
代码语言:javascript复制sudo apt-get install libfcgi-dev libfcgi0ldbl libjpeg-turbo8-dev libmcrypt-dev libssl-dev libc-client2007e libc-client2007e-dev libxml2-dev libbz2-dev libcurl4-openssl-dev libjpeg-dev libpng-dev libfreetype6-dev libkrb5-dev libpq-dev libxml2-dev libxslt1-dev libzip-dev libsqlite3-dev libonig-dev pkg-config libxml2-dev libkrb5-dev libssl-dev libsqlite3 libbz2-dev libpng-dev libjpg-dev libfreetype6-dev libc-client2007e-dev libonig-dev libreadline-dev libxslt-dev libzip-dev
如果安装的依赖包不存在,请通过命令:
apt-cache search freetype
查找相应的安装包安装即可
编译
代码语言:javascript复制./configure
--prefix=/usr/local/php-8.3
--with-config-file-path=/usr/local/php-8.3/etc
--with-zlib-dir
--with-freetype
--enable-mbstring
--enable-soap
--enable-calendar
--with-curl
--with-zlib
--enable-gd
--disable-rpath
--enable-inline-optimization
--with-bz2
--with-zlib
--enable-sockets
--enable-sysvsem
--enable-sysvshm
--enable-pcntl
--enable-mbregex
--enable-exif
--enable-bcmath
--with-mhash
--with-zip
--with-pdo-mysql
--with-mysqli
--with-mysql-sock=/var/run/mysqld/mysqld.sock
--with-jpeg
--with-openssl
--with-fpm-user=www
--with-fpm-group=www
--with-libdir=/lib/x86_64-linux-gnu
--enable-ftp
--with-kerberos
--with-gettext
--with-xmlrpc
--with-xsl
--enable-opcache
--enable-intl
--with-pear
--enable-fpm
编译安装
代码语言:javascript复制make
make install
代码语言:javascript复制~/build/php-8.3.0$ sudo make install
[sudo] password for www:
Installing shared extensions: /usr/local/php-8.3/lib/php/extensions/no-debug-non-zts-20230831/
Installing PHP CLI binary: /usr/local/php-8.3/bin/
Installing PHP CLI man page: /usr/local/php-8.3/php/man/man1/
Installing PHP FPM binary: /usr/local/php-8.3/sbin/
Installing PHP FPM defconfig: /usr/local/php-8.3/etc/
Installing PHP FPM man page: /usr/local/php-8.3/php/man/man8/
Installing PHP FPM status page: /usr/local/php-8.3/php/php/fpm/
Installing phpdbg binary: /usr/local/php-8.3/bin/
Installing phpdbg man page: /usr/local/php-8.3/php/man/man1/
Installing PHP CGI binary: /usr/local/php-8.3/bin/
Installing PHP CGI man page: /usr/local/php-8.3/php/man/man1/
Installing build environment: /usr/local/php-8.3/lib/php/build/
Installing header files: /usr/local/php-8.3/include/php/
Installing helper programs: /usr/local/php-8.3/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php-8.3/php/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /usr/local/php-8.3/lib/php/
[PEAR] Archive_Tar - installed: 1.4.14
[PEAR] Console_Getopt - installed: 1.4.3
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.4.5
warning: pear/PEAR dependency package "pear/Archive_Tar" installed version 1.4.14 is not the recommended version 1.4.4
[PEAR] PEAR - installed: 1.10.13
Wrote PEAR system config file at: /usr/local/php-8.3/etc/pear.conf
You may want to add: /usr/local/php-8.3/lib/php to your php.ini include_path
Installing PDO headers: /usr/local/php-8.3/include/php/ext/pdo/
配置文件
代码语言:javascript复制核心 php.ini 配置文件
cp php.ini-production /usr/local/php-8.3/etc/php.ini
代码语言:javascript复制查看PHP版本
/usr/local/php-8.3/bin/php -v
PHP 8.3.0 (cli) (built: Dec 5 2023 20:03:56) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.0, Copyright (c) Zend Technologies
安装Python
安装Anaconda
Anaconda 是一个开源的Anaconda是专注于数据分析的Python发行版本,包含了conda、Python等190多个科学包及其依赖项。
Anaconda就是可以便捷获取包且对包能够进行管理,包括了python和很多常见的软件库和一个包管理器conda。常见的科学计算类的库都包含在里面了,使得安装比常规python安装要容易,同时对环境可以统一管理的发行版本。
下载地址:https://www.anaconda.com/download#downloads
代码语言:javascript复制wget https://repo.anaconda.com/archive/Anaconda3-2023.09-0-Linux-x86_64.sh
命令行中切换到anaconda文件所在目录
代码语言:javascript复制sh Anaconda3-2022.05-Linux-x86_64.sh
accept the license terms--yes
默认安装路径,
/home/用户名/anaconda3
查看Anaconda版本
终端输入conda --version
或者conda -V
/home/www/anaconda3/condabin/conda --version
conda 23.7.4
创建虚拟环境
代码语言:javascript复制$ conda create -n tinywan-python310 python=3.10
Collecting package metadata (current_repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 23.7.4
latest version: 23.11.0
Please update conda by running
$ conda update -n base -c defaults conda
Or to minimize the number of packages updated during conda update use
conda install conda=23.11.0
## Package Plan ##
environment location: /home/www/anaconda3/envs/tinywan-python310
added / updated specs:
- python=3.10
The following packages will be downloaded:
package | build
---------------------------|-----------------
pip-23.3.1 | py310h06a4308_0 2.7 MB
python-3.10.13 | h955ad1f_0 26.8 MB
setuptools-68.0.0 | py310h06a4308_0 936 KB
wheel-0.41.2 | py310h06a4308_0 109 KB
------------------------------------------------------------
Total: 30.5 MB
The following NEW packages will be INSTALLED:
_libgcc_mutex pkgs/main/linux-64::_libgcc_mutex-0.1-main
_openmp_mutex pkgs/main/linux-64::_openmp_mutex-5.1-1_gnu
bzip2 pkgs/main/linux-64::bzip2-1.0.8-h7b6447c_0
ca-certificates pkgs/main/linux-64::ca-certificates-2023.08.22-h06a4308_0
ld_impl_linux-64 pkgs/main/linux-64::ld_impl_linux-64-2.38-h1181459_1
libffi pkgs/main/linux-64::libffi-3.4.4-h6a678d5_0
libgcc-ng pkgs/main/linux-64::libgcc-ng-11.2.0-h1234567_1
libgomp pkgs/main/linux-64::libgomp-11.2.0-h1234567_1
libstdcxx-ng pkgs/main/linux-64::libstdcxx-ng-11.2.0-h1234567_1
libuuid pkgs/main/linux-64::libuuid-1.41.5-h5eee18b_0
ncurses pkgs/main/linux-64::ncurses-6.4-h6a678d5_0
openssl pkgs/main/linux-64::openssl-3.0.12-h7f8727e_0
pip pkgs/main/linux-64::pip-23.3.1-py310h06a4308_0
python pkgs/main/linux-64::python-3.10.13-h955ad1f_0
readline pkgs/main/linux-64::readline-8.2-h5eee18b_0
setuptools pkgs/main/linux-64::setuptools-68.0.0-py310h06a4308_0
sqlite pkgs/main/linux-64::sqlite-3.41.2-h5eee18b_0
tk pkgs/main/linux-64::tk-8.6.12-h1ccaba5_0
tzdata pkgs/main/noarch::tzdata-2023c-h04d1e81_0
wheel pkgs/main/linux-64::wheel-0.41.2-py310h06a4308_0
xz pkgs/main/linux-64::xz-5.4.5-h5eee18b_0
zlib pkgs/main/linux-64::zlib-1.2.13-h5eee18b_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate tinywan-python310
#
# To deactivate an active environment, use
#
# $ conda deactivate
激活虚拟环境
代码语言:javascript复制conda activate tinywan-test
查看已有的虚拟环境
代码语言:javascript复制$ conda env list
# conda environments:
#
base /home/www/anaconda3
tinywan-test * /home/www/anaconda3/envs/tinywan-test
进入虚拟环境查看Python版本
代码语言:javascript复制(tinywan-python310) www@xxxxxxx:~/anaconda3$ python --version
Python 3.10.13
安装phpy
下载
代码语言:javascript复制git clone https://github.com/swoole/phpy.git
生成 ./configure 配置文件
代码语言:javascript复制/usr/local/php-8.3/bin/phpize --with-php-config=/usr/local/php-8.3/bin/php-config
指定配置文件
代码语言:javascript复制./configure --with-php-config=/usr/local/php-8.3/bin/php-config --with-python-dir=/home/www/anaconda3/envs/tinywan-python310 --with-python-version=3.10
参数说明
--with-php-config
PHP配置文件--with-python-dir
Python安装目录--with-python-version
Python版本号(只能使用次版本好,如:3.10.15
,则填写3.10
)
编译安装
代码语言:javascript复制make -j4
sudo make install
php.ini
扩展添加
安装成功后,修改 php.ini
,加入 extension=phpy.so
vim /usr/local/php-8.3/etc/php.ini
// 添加
extension=phpy.so
执行 php -m
检查是否成功加载扩展。
$ php -m |grep phpy
phpy
使用
案例 os.php
<?php
function main() {
$m = PyCore::import("os");
var_dump($m instanceof PyObject);
$rs = $m->uname();
echo $rs;
echo $rs->version;
}
main()
查看当前操作系统版本
执行结果
代码语言:javascript复制/phpy/examples$ /usr/local/php-8.3/bin/php os.php
bool(true)
posix.uname_result(sysname='Linux', nodename='iZbp1cqx6cq0t2gpl995gqZ', release='4.15.0-137-generic', version='#141-Ubuntu SMP Fri Feb 19 13:46:27 UTC 2021', machine='x86_64')#141-Ubuntu SMP Fri Feb 19 13:46:27 UTC 2021(tinywan-python310)
其他
gcc 版本升级
ubuntu18.04的Gcc7.5.0升级到9.4.0
1、添加Ubuntu的测试工具链 (Toolchain) PPA。这个PPA包含了最新版本的GCC,包括GCC 9:
代码语言:javascript复制sudo add-apt-repository ppa:ubuntu-toolchain-r/test
2、更新你的包列表:
代码语言:javascript复制sudo apt-get update
sudo apt-get upgrade
3、查看gcc所有版本
代码语言:javascript复制sudo apt-cache search gcc
4、安装GCC-9:
代码语言:javascript复制sudo apt install gcc-9
5、为了让你的系统默认使用GCC-9,你需要更新你的update-alternatives。首先,安装GCC-9为一个可选项
代码语言:javascript复制sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
这个命令告诉
update-alternatives
系统GCC-9
是一个可选项,并给它一个优先级90
。优先级最高的版本将成为默认版本。
如果安装成功会是这样的:
代码语言:javascript复制gcc -v
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~18.04)