# Swoft環境要求
現在我們來介紹如何安裝Swoft需要的環境。
### 環境要求
* PHP > 7.1
* Swoole > 4.3.0
* PHP包管理器Composer
* 連接迭代器依賴 pcre 庫
* OpenSSL PHP 擴展
* JSON PHP 擴展
* PDO PHP 擴展 (如需要使用到 MySQL 客戶端)
* Redis PHP 擴展 (如需要使用到 Redis 客戶端)
### 課程實驗環境
* Linux(本文使用Centos6.8 64位)
* 安裝git
* PHP7.3.8
* Swoole4.4
### 1、安裝依賴
這個根據用戶自己的需求安裝相關依賴,依賴的安裝可以根據php編譯的時候帶上的參數決定。
```shell
yum -y install \
gcc gcc-c++ wget vim make cmake automake autoconf kernel-devel ncurses-devel \
libxml2-devel pcre-devel openssl openssl-devel curl-devel libjpeg-devel \
libpng-devel pcre-devel libtool-libs freetype-devel gd zlib-devel file \
bison bison-devel patch mlocate flex diffutils readline-devel glibc-devel \
glib2-devel bzip2-devel gettext-devel libcap-devel libmcrypt-devel gmp-devel \
libxslt-devel git libevent libevent-devel perl-ExtUtils-MakeMaker package xz
```
### 2、安裝Git
```shell
yum -y remove git
wget https://www.kernel.org/pub/software/scm/git/git-2.6.0.tar.gz --no-check-certificate
tar zxvf git-2.6.0.tar.gz
cd git-2.6.0
./configure --prefix=/usr/local/git
make
make install
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile
source /etc/profile
```
給當前登陸的linux用戶設置環境變量,當你使用的賬號是非root賬號設置環境變量使用git
```shell
echo "export PATH=$PATH:/usr/local/git/bin" >> ~/.bashrc
source ~/.bashrc
```
如果使用git clone出現 Peer certificate cannot be authenticated with known CA certificates
則可以關閉證書驗證解決
```shell
git config --global http.sslVerify false
```
### 3、安裝php
我編譯的參數大家可以進行參考不一定需要跟我的一摸一樣。
```shell
wget https://www.php.net/distributions/php-7.3.8.tar.gz --no-check-certificate
tar zxvf php-7.3.8.tar.gz
cd php-7.3.8
./configure --prefix=/usr/local/php \
--enable-fpm \
--with-config-file-path=/etc \
--with-libxml-dir --with-openssl \
--with-mysqli \
--enable-bcmath \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--with-pcre-dir \
--enable-ftp \
--with-openssl-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-mbstring \
--with-onig \
--with-pdo-mysql \
--with-readline \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-xmlrpc \
--with-xsl \
--with-pear \
--enable-shared \
--enable-inline-optimization \
--disable-debug \
--enable-xml \
--with-sqlite3 \
--with-iconv \
--with-cdb \
--enable-dom \
--enable-fileinfo \
--enable-filter \
--enable-json \
--enable-mbregex \
--enable-mbregex-backtrack \
--enable-pdo \
--with-pdo-sqlite \
--enable-session \
--enable-simplexml \
--enable-opcache \
--enable-mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-maintainer-zts
make
make install
```
建立php相關命令的軟連接
```shell
ln -s /usr/local/php/bin/php /usr/bin/php
ln -s /usr/local/php/bin/phpize /usr/bin/phpize
ln -s /usr/local/php/bin/php-config /usr/bin/php-config
```
### 4、安裝Swoole擴展
我這里使用的Swoole擴展是目前最新的,編譯最新Swoole需要gcc4.8以上,如果你的gcc低于這個版本則需要升級。
```shell
gcc --version #查看版本
```
升級gcc的教程 升級gcc
```shell
git clone https://gitee.com/swoole/swoole.git
cd swoole
phpize
./configure \
--with-php-config=/usr/local/php/bin/php-config \
--enable-openssl \
--enable-sockets \
--enable-mysqlnd
make clean
make
make install
```
關于參數問題可以參考 編譯參數
### 5、讓php支持swoole
```shell
echo "extension=swoole.so" >> /etc/php.ini
```
### 6、安裝Redis的php擴展
Redis擴展的源碼地址:http://pecl.php.net/package/redis
```shell
wget http://pecl.php.net/get/redis-5.0.2.tgz
tar -zxvf redis-5.0.2.tgz
cd redis-5.0.2
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
```
### 錯誤解決
```shell
configure: error: Please reinstall the libzip distribution
```
需要重新安裝 `libzip`
```shell
wget https://libzip.org/download/libzip-1.5.1.tar.gz --no-check-certificate
tar -zxvf libzip-1.5.1.tar.gz
cd libzip-1.5.1
mkdir build
cd build
cmake ..
make && make install
```
注意:如果提示cmake版本過低,需新版本,則需要重新安裝cmake。
```shell
yum remove cmake
wget https://cmake.org/files/v3.10/cmake-3.10.2-Linux-x86_64.tar.gz --no-check-certificate
tar -zxvf cmake-3.10.2-Linux-x86_64.tar.gz
mv cmake-3.10.2-Linux-x86_64 /usr/local/cmake
echo "export CMAKE_HOME=/usr/local/cmake" >> /etc/profile
echo "export PATH=$PATH:$CMAKE_HOME/bin" >> /etc/profile
source /etc/profile
```
```shell
configure: error: off_t undefined; check your library configuration
```
安裝swoole錯誤
```shell
configure.ac:3: error: Autoconf version 2.68 or higher is required
```
需要重新安裝更高版本的 `autoconf`
```shell
yum remove autoconf -y
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
tar -zxvf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure
make
make install
```
- 第一章:基礎知識
- 課程簡介
- PHP-FPM過渡常駐內存
- 進程
- 實戰:實現Master-Worker
- 線程
- 實戰:CC攻擊器
- 協程
- 實戰:實現waitGroup功能
- 進程、線程、協程的區別
- 第二章:初識Swoft2.0
- Swoft介紹
- Swoft環境安裝
- gcc升級
- 安裝Swoft框架
- 目錄結構介紹
- SwoftCli工具
- Swoft配置
- 第三章:Swoft2.0核心
- 上下文
- 常駐內存沒有上下文隔離
- 實戰:手寫swoole框架上下文管理
- Bean容器
- 實戰:根據容器原理實現容器
- 實戰:通過容器實現依賴注入
- Bean容器定義與使用
- 配置文件定義Bean
- 容器類型
- 面向接口的容器
- 注解
- 實戰:實現注解
- 自定義Swoft注解類
- 事件
- 連接池
- 實戰:Swoole實現連接池
- 第四章:Http服務器
- Http Server生命周期
- Http Server配置
- 控制器
- 路由
- 請求對象Request
- 響應對象Response
- Http異常處理
- 中間件
- 實戰:中間件實現JWT登陸授權
- 第五章:驗證器
- 內置驗證類型
- 驗證器的使用
- 自定義驗證器
- 第六章:數據庫操作
- 連接數據庫
- 實體模型
- 模型事件
- 查詢器
- 事務處理
- 連接池配置
- 讀寫分離
- 多數據庫切換
- Models分層結構
- 實戰:實現用戶CURD API
- 第七章:Redis
- 連接redis和使用
- Redis連接池
- Redis集群配置(單機版)
- Redis集群配置(多服務器)
- Redis連接集群
- Redis實戰:實現延時任務
- 第八章:AOP編程
- AOP概念
- AOP實現原理
- 實戰實現AOP:靜態代理
- 實戰實現AOP:動態代理
- 切面注解介紹
- PointExecution切面
- PointBean切面
- PointAnnotation切面
- 實戰:使用AOP實現日志記錄
- 第九章:任務處理
- 進程使用
- 進程池使用
- 實戰:進程消費隊列
- 實戰:進程實現RabbitMQ延時隊列
- 異步任務
- 協程任務
- 定時任務