像其他程序语言都支持了多线程~!
那么PHP支持多线程吗?多线程是安全线程吗?
PHP7.2 已经支持安全线程,其他版本的线程是非安全线程。
php中多线程使用是需要安装扩展的,下面说一下php7.2 是怎么安装的
1、下载PHP7.2
2、解压并进入目录
wget http://www.xiaosongit.com/Public/Upload/file/20181118/1542458624299757.tar.gz tar -xf 1542458624299757.tar.gz cd php-7.2.12/
3、编译安装
./configure --prefix=/usr/local/php/php7.2.12 --with-config-file-path=/usr/local/php/php7.2.12/etc --with-mysqli=/usr/local/mysql5.5/bin/mysql_config --with-pdo-mysql=/usr/local/mysql5.5 -enable-mbstring --enable-ftp --enable-fpm --with-curl --with-mhash --enable-zip --with-pcre-regex --with-gd --with-jpeg-dir -with-gettext --enable-xml --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-opcache --enable-maintainer-zts
make && make install
备注:
--enable-sysvsem 信号量
--enable-sysvmsg
4、修改配置文件
如果你是开发环境复制
复制 php.ini-development 到 /usr/local/php/php7.2.12/etc/php.ini
如果你是生产环境 复制
复制 php.ini-production 到 /usr/local/php/php7.2.12/etc/php.ini
修改两个默认值: date.timezone=PRC 正八时 或 Asia/Shanghai
5、验证PHP是否正常安装
进入 进入/usr/local/php/php7.2.12/bin 执行一下 ./php -v
测试时区设置是否正确:
好的,就是时间点。 PHP 安装完毕。
安装pthreads扩展
访问github : https://github.com/krakjoe/pthreads
我这里点击的是 Download ZIP 到本地
我本地也提供了一个下载,这个安装包和github上是一样的。
现在开始安装 pthreads
1、解压、并进入目录
unzip pthreads-master.zip cd pthreads-master
2、编译安装
#这下面没有configure #我们需要 phpize 然后 安装 [pthreads-master]# /usr/local/php/php7.2.12/bin/phpize Configuring for: PHP Api Version: 20170718 Zend Module Api No: 20170718 Zend Extension Api No: 320170718 [pthreads-master]# ./configure --with-php-config=/usr/local/php/php7.2.12/bin/php-config checking for grep that handles long lines and -e... /usr/bin/grep checking for egrep... /usr/bin/grep -E checking for a sed that does not truncate output... /usr/bin/sed checking for cc... cc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether cc accepts -g... yes checking for cc option to accept ISO C89... none needed checking how to run the C preprocessor... cc -E checking for icc... no checking for suncc... no checking whether cc understands -c and -o together... yes checking for system library directory... lib checking if compiler supports -R... no checking if compiler supports -Wl,-rpath,... yes checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking target system type... x86_64-unknown-linux-gnu checking for PHP prefix... /usr/local/php/php7.2.12 checking for PHP includes... -I/usr/local/php/php7.2.12/include/php -I/usr/local/php/php7.2.12/include/php/main -I/usr/local/php/php7.2.12/include/php/TSRM -I/usr/local/php/php7.2.12/include/php/Zend -I/usr/local/php/php7.2.12/include/php/ext -I/usr/local/php/php7.2.12/include/php/ext/date/lib checking for PHP extension directory... /usr/local/php/php7.2.12/lib/php/extensions/no-debug-zts-20170718 checking for PHP installed headers prefix... /usr/local/php/php7.2.12/include/php checking if debug is enabled... no checking if zts is enabled... yes checking for re2c... no configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers. checking for gawk... gawk checking whether to enable pthreads... yes, shared checking whether to enable AddressSanitizer for pthreads... no checking whether to enable dmalloc for pthreads... no checking for ZTS... ok checking for ld used by cc... /usr/bin/ld checking if the linker (/usr/bin/ld) is GNU ld... yes checking for /usr/bin/ld option to reload object files... -r checking for BSD-compatible nm... /usr/bin/nm -B checking whether ln -s works... yes checking how to recognize dependent libraries... pass_all checking for ANSI C header files... yes checking for sys/types.h... yes checking for sys/stat.h... yes checking for stdlib.h... yes checking for string.h... yes checking for memory.h... yes checking for strings.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for unistd.h... yes checking dlfcn.h usability... yes checking dlfcn.h presence... yes checking for dlfcn.h... yes checking the maximum length of command line arguments... 1572864 checking command to parse /usr/bin/nm -B output from cc object... ok checking for objdir... .libs checking for ar... ar checking for ranlib... ranlib checking for strip... strip checking if cc supports -fno-rtti -fno-exceptions... no checking for cc option to produce PIC... -fPIC checking if cc PIC flag -fPIC works... yes checking if cc static flag -static works... no checking if cc supports -c -o file.o... yes checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes checking whether -lc should be explicitly linked in... no checking dynamic linker characteristics... GNU/Linux ld.so checking how to hardcode library paths into programs... immediate checking whether stripping libraries is possible... yes checking if libtool supports shared libraries... yes checking whether to build shared libraries... yes checking whether to build static libraries... no creating libtool appending configuration tag "CXX" to libtool configure: creating ./config.status config.status: creating config.h [pthreads-master]# make && make install ---------------------------------------------------------------------- Libraries have been installed in: /usr/local/src/pthreads-master/modules If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the `-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the `LD_RUN_PATH' environment variable during linking - use the `-Wl,--rpath -Wl,LIBDIR' linker flag - have your system administrator add LIBDIR to `/etc/ld.so.conf' See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- Build complete. Don't forget to run 'make test'. Installing shared extensions: /usr/local/php/php7.2.12/lib/php/extensions/no-debug-zts-20170718/ #至此,安装成功 [pthreads-master]# ls /usr/local/php/php7.2.12/lib/php/extensions/no-debug-zts-20170718/ opcache.a opcache.so pthreads.so [root@iZ25vpndgurZ pthreads-master]#
3、修改php的配置文件
vim /usr/local/php/php7.2.12/etc/php.ini
在文件最底部加上
extension=pthreads.so
4、测试是否安装成功
[root@iZ25vpndgurZ bin]# ./php -m | grep pthreads pthreads
安装已经成功
呵呵,到此为止。