<!-- toc -->
# 環境搭建
---
[TOC]
# Linux環境下安裝
Linux操作系統通常都有自己的包管理軟件(Ubuntu的apt-get,CentOS的yum,Mac OSX的HomeBrew等),因此一般情況下可以通過這些包管理軟件直接安裝PHP。但是這樣安裝的PHP不太適用于運行Swoole,因此本章將介紹如何通過源碼編譯安裝。
## 編譯環境
想要編譯安裝PHP首先需要安裝對應的編譯工具。
Ubuntu上使用如下命令安裝編譯工具和依賴包:
```shell
sudo apt-get install \
build-essential \
gcc \
g++ \
autoconf \
libiconv-hook-dev \
libmcrypt-dev \
libxml2-dev \
libmysqlclient-dev \
libcurl4-openssl-dev \
libjpeg8-dev \
libpng12-dev \
libfreetype6-dev \
```
## PHP安裝
[PHP下載地址 Download](http://php.net/)
在這里挑選你想用的版本即可。下載源碼包后,解壓至本地任意目錄(保證讀寫權限)。
使用如下命令編譯安裝PHP:
```shell
cd php-5.6.22/
./configure --prefix=/usr/local/php \
--with-config-file-path=/etc/php \
--enable-fpm \
--enable-pcntl \
--enable-mysqlnd \
--enable-opcache \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-shmop \
--enable-zip \
--enable-soap \
--enable-xml \
--enable-mbstring \
--disable-rpath \
--disable-debug \
--disable-fileinfo \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-pcre-regex \
--with-iconv \
--with-zlib \
--with-mcrypt \
--with-gd \
--with-openssl \
--with-mhash \
--with-xmlrpc \
--with-curl \
--with-imap-ssl
sudo make
sudo make install
sudo mkdir /etc/php
sudo cp php.ini-development /etc/php/php.ini
```
注意,以上PHP編譯選項根據實際情況可調整。
另外,還需要將PHP的可執行目錄添加到環境變量中。
使用Vim/Sublime打開~/.bashrc,在末尾添加如下內容:
```shell
export PATH=/usr/local/php/bin:$PATH
export PATH=/usr/local/php/sbin:$PATH
```
保存后,終端輸入命令:
```bash
source ~/.bashrc
```
此時即可通過`php --version`查看php版本。
# Mac環境下安裝
Mac系統自帶PHP,但是Mac上對于OpenSSL的相關功能做了一些限制,使用了一個`Secure Transport`來取代OpenSSL。因此仍然建議重新編譯安裝PHP環境。
## 安裝OpenSSL
Mac原裝的0.9.8版本的OpenSSL使用的時候會有些Warning,反正我看不慣……
安裝命令:
```shell
brew install openssl
```
安裝之后,還需要鏈接新的openssl到環境變量中。
```shell
brew link --force openssl
```
## 安裝Curl
Mac系統原裝的Curl默認使用了Secure Transport,導致通過option函數設置的證書全部無效。果斷重新安裝之。
```shell
brew install curl --with-openssl && brew link curl --force
```
## 安裝PHP
PHP官網上下載某個版本的PHP(我選擇的是5.6.22),使用如下命令編譯安裝。
```shell
cd /path/to/php/
./configure
--prefix=/usr/local/php
--with-config-file-path=/etc/php
--with-openssl=/usr/local/Cellar/openssl/1.0.2g/
--with-curl=/usr/local/Cellar/curl/7.48.0/
make && make install
```
這里我僅列出兩個需要特殊設置的選項`with-openssl`和`with-curl`。
安裝完成后,執行如下命令:
```shell
sudo cp /usr/local/php/bin/php /usr/bin/
sudo cp /usr/local/php/bin/phar* /usr/bin/
sudo cp /usr/local/php/bin/php-config /usr/bin/
sudo cp /usr/local/php/bin/phpize /usr/bin/
```
隨后,設置php.ini
```shell
sudo mkdir /etc/php
sudo cp php.ini.development /etc/php/php.ini
```
# Swoole擴展安裝
[Swoole擴展下載地址 Download](https://github.com/swoole/swoole-src/releases)
解壓源碼至任意目錄,執行如下命令:
```shell
cd swoole-src-swoole-1.7.6-stable/
phpize
./configure
sudo make
sudo make install
```
> swoole的./configure有很多額外參數,可以通過./configure --help命令查看,這里均選擇默認項)
安裝完成后,進入/etc/php目錄下,打開php.ini文件,在其中加上如下一句:
```bash
extension=swoole.so
```
隨后在終端中輸入命令`php -m`查看擴展安裝情況。如果在列出的擴展中看到了swoole,則說明安裝成功。
查看swoole版本
```
php --ri swoole
```
- swoole簡介
- swoole功能概述
- 序章
- 開發必讀
- 1 環境搭建
- 1.1 環境搭建
- 1.2 搭建Echo服務器
- 2 初識Swoole
- 2.1 Worker進程
- 2.2 TaskWorker進程
- 2.3 Timer定時器
- 2.4 Process進程
- 2.5 Table內存表
- 2.6 多端口監聽
- 2.7 sendfile文件支持
- 2.8 SSL支持
- 2.9 熱重啟
- 2.10 http_server
- 附錄*server配置
- 附錄*server函數
- 附錄*server屬性
- 附錄*server回調函數
- 附錄*server高級特性
- 心跳檢測
- 3 Swoole協議
- 3.1 EOF協議
- 3.2 固定包頭協議
- 3.3 Http協議
- 3.4 WebSocket協議
- 3.5 MTQQ協議
- 內置http_server
- 內置websocket_server
- Swoole\Redis\Server
- 4 Swoole異步IO
- 4.1 AsyncIO
- 異步文件系統IO
- swoole_async_readfile
- swoole_async_writefile
- swoole_async_read
- swoole_async_write
- 5 swoole異步客戶端
- ws_client
- http_client
- mysql_client
- redis_client
- tcp_client
- http2_client
- 6 swoole協程
- Swoole\Coroutine\Http\Client
- Swoole\Coroutine\MySQL
- Swoole\Coroutine\Redis
- Coroutine\PostgreSQL
- Swoole\Coroutine\Client
- Swoole\Coroutine\Socket
- Swoole\Coroutine\Channel
- Coroutine
- Swoole\Coroutine::create
- Swoole\Coroutine::resume
- Swoole\Coroutine::suspend
- Swoole\Coroutine::sleep
- Coroutine::getaddrinfo
- Coroutine::gethostbyname
- swoole_async_dns_lookup_coro
- Swoole\Coroutine::getuid
- getDefer
- setDefer
- recv
- Coroutine::stats
- Coroutine::fread
- Coroutine::fget
- Coroutine::fwrite
- Coroutine::readFIle
- Coroutine::writeFIle
- Coroutine::exec
- 7 swoole_process
- process::construct
- process::start
- process::name
- process::signal
- process::setaffinity
- process::exit
- process::kill
- process::daemon
- process->exec
- process::wait
- process::alarm
- 8 swoole定時器
- swoole_timer_tick
- swoole_timer_after
- swoole_timer_clear
- 9 swoole_event
- swoole_event_add
- swoole_event_set
- swoole_event_del
- swoole_event_wait
- swoole_event_defer
- swoole_event_write
- swoole_event_exit
- swoole提供的function
- 常見問題
- 客戶端鏈接失敗原因
- 如何設置進程數
- 如何實現異步任務
- 如何選擇swoole三種模式
- php中哪些函數是阻塞的
- 是否可以共用1個redis或mysql連接
- 如何在回調函數中訪問外部的變量
- 為什么不要send完后立即close
- 不同的Server程序實例間如何通信
- MySQL的連接池、異步、斷線重連
- 在php-fpm或apache中使用swoole
- 學習Swoole需要掌握哪些基礎知識
- 在phpinfo中有在php-m中沒有
- 同步阻塞與異步非阻塞選擇
- CURL發送POST請求服務器端超時
- 附錄
- 預定義常量
- 內核參數調優
- php四種回調寫法
- 守護進程程序常用數據結構
- swoole生命周期
- swoole_server中內存管理機制
- 使用jemalloc優化swoole內存分配性能
- Reactor、Worker、Task的關系
- Manager進程
- Swoole的實現
- Reactor線程
- 安裝擴展
- swoole-worker手冊
- swoole相關開源項目
- 寫在后面的話
- 版本更新記錄
- 4.0