打算開始學習swoole了(原來好像弄過: swoole環境搭建),不過那次只是接觸了一下,並未太過深刻,此次從新來過 (°ー°〃)
swoole雖然能在windows上搭建,不過我以爲意義不大....須要安裝CygWin這和在linux上有什麼區別呢┑( ̄Д  ̄)┍,恰好如今手上有一臺空閒的樹莓派zero,試試在上面搭建
之因此要編譯安裝是由於在swoole編譯的時候須要用到phpize,apt-get安裝的時候沒發現有
如今這個上面什麼東西都沒有,先安裝php,我選最新的php7.2.6,zero配置是真的好低....解壓和編譯cpu都100%了很慢....趁這個時間去幹點別的吧php
下載,解壓源碼,安裝依賴html
強烈建議使用國內鏡像....否則可能一些依賴lib按照失敗,致使編譯錯誤mysql
sudo -i wget http://hk1.php.net/get/php-7.2.6.tar.gz/from/this/mirror mv mirro php.tar.gz tar -zxvf php.tar.gz apt-get update apt-get install libxml2* libbz2-dev libjpeg-dev libmcrypt-dev libssl-dev openssl libxslt1-dev libxslt1.1 libcurl4-gnutls-dev libpq-dev build-essential git make
編譯配置,複製的網上的lnmp編譯- -...去掉了和Nginx有關的編譯項,我只須要編譯出php就行,不須要Nginx那些環境,固然若是你以前已經有了這些,這一部分就能夠跳過了linux
cd php-7.2.6 ./configure \ --prefix=/usr/local/php \ --exec-prefix=/usr/local/php \ --bindir=/usr/local/php/bin \ --sbindir=/usr/local/php/sbin \ --includedir=/usr/local/php/include \ --libdir=/usr/local/php/lib/php \ --mandir=/usr/local/php/php/man \ --with-config-file-path=/usr/local/php/etc \ --with-mysql-sock=/var/lib/mysql/mysql.sock \ --with-mcrypt=/usr/include \ --with-mhash \ --with-openssl \ --with-mysql=shared,mysqlnd \ --with-mysqli=shared,mysqlnd \ --with-pdo-mysql=shared,mysqlnd \ --with-gd \ --with-iconv \ --with-zlib \ --enable-zip \ --enable-inline-optimization \ --disable-debug \ --disable-rpath \ --enable-shared \ --enable-xml \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-mbregex \ --enable-mbstring \ --enable-ftp \ --enable-gd-native-ttf \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-soap \ --without-pear \ --with-gettext \ --enable-session \ --with-curl \ --with-freetype-dir \ --enable-opcache \ --enable-redis \ --enable-fpm \ --enable-fastcgi \ --disable-fileinfo
CPU 100% 有點怕,樹莓派zero性能確實是弱...編譯好慢....解決了編譯配置的問題後就開始編譯,我是真的睡了一覺(次日)纔起來make installgit
make && make install
設置一下php.ini文件github
cp php.ini-production /usr/local/php/etc/php.ini //我輸入php -v以後發現沒反應,可是php確實是成功了,在/usr/local/php/bin裏面./php -v也有反應,想到多是沒有連接到/usr/bin 目錄裏,用ln命令連接一下 ln -s /usr/local/php/bin/php /usr/bin/php //連接phpize ln -s /usr/local/php/bin/phpize /usr/bin/phpize
成功以後,老套路web
php -v
成功,終於能夠下一步了,進入swoole編譯配置redis
從git上下載源碼https://github.com/swoole/swoole-src/releases,開始編譯sql
wget https://github.com/swoole/swoole-src/archive/v4.0.0.zip unzip v4.0.0.zip mv swoole-src-4.0.0/ swoole cd swoole phpize
這裏我提示了一個錯誤...
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
解決辦法:windows
apt-get install m4 autoconf
phpize成功以後繼續運行編譯配置和開始編譯(希望此次不用那麼久了...)
開啓一些須要的:編譯配置項
./configure --with-php-config=/usr/local/php/bin/php-config --enable-sockets --enable-swoole-debug --enable-openssl --enable-mysqlnd --enable-coroutine make && make install
而後須要在php.ini中配置下
vi /usr/local/php/etc/php.ini //添加 extension=swoole.so
而後php -m
有這一項就表明成啦~
安裝編譯都完成以後,固然來試試是否是真的能用了
複製官方的例子,嘿嘿嘿~
<?php //建立websocket服務器對象,監聽0.0.0.0:9502端口 $ws = new swoole_websocket_server("0.0.0.0", 9502); //監聽WebSocket鏈接打開事件 $ws->on('open', function ($ws, $request) { var_dump($request->fd, $request->get, $request->server); $ws->push($request->fd, "hello, welcome\n"); }); //監聽WebSocket消息事件 $ws->on('message', function ($ws, $frame) { echo "Message: {$frame->data}\n"; $ws->push($frame->fd, "server: {$frame->data}"); }); //監聽WebSocket鏈接關閉事件 $ws->on('close', function ($ws, $fd) { echo "client-{$fd} is closed\n"; }); $ws->start();
php swoole.php
web:
<script> var ws = new WebSocket("ws://localhost:9502"); ws.onopen = function () { ws.send("send data"); }; ws.onmessage = function (evt) { var received_msg = evt.data; console.log(received_msg); }; ws.onclose = function () { console.log("鏈接關閉"); }; </script>
成了~
歷時一天,終於搞定了 編譯真的是漫長的過程=_=