1.mac下通過docker進入centos容器
docker run -it centos /bin/bash
來通過命令查看當前centos版本:
cat /etc/redhat-release
結果:
CentOS Linux release 7.6.1810 (Core)
2.下載PHP7以上版本的源碼:
wget https://www.php.net/distributions/php-7.2.23.tar.gz
如果沒有wget ,可以 yum install wget
如果安裝失敗可以修改yum源為清華的yum源
首先備份 CentOS-Base.repo
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
清除緩存
yum clean all
rm -rf /var/cache/yum/
生成緩存
yum makecache
3.進入php目錄
先解決安裝前面可能的依賴
yum install gcc gcc-c++ libxml2-devel make
編譯安裝:
./configure \ 檢查環(huán)境
--prefix=/usr/local/php7_2 \ 指定php安裝路徑
--with-config-file-path=/etc 配置文件路徑
make && make install 編譯并安裝
將php加到環(huán)境變量中:
vi ~/.bashrc 編輯當前用戶環(huán)境變量文件
alias php7=/usr/bin/php 增加一行
source ~/.bashrc 使環(huán)境變量生效
將php.ini配置文件加到/etc/目錄下面
cp php.ini-development /etc/php.ini
可以去到 /usr/local/php7_2查看一下php的目錄

image.png
ok,完成。
3.我們來下載swoole的源碼
wget https://github.com/swoole/swoole-src.git
進入到swoole-src目錄
/usr/local/php7_2/bin/phpize
有是如果遇到:
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
可以執(zhí)行一下:
yum install autoconf
查看當前目錄到了一個configure文件
./configure \
--with-php-config=/usr/local/php7_2/bin/php-config
make && make install
安裝完成之后,打開/etc/php.ini
加入
extension=swoole.so
執(zhí)行php7 -m可以查看swoole是否安裝成功。

image.png
4將容器打包成鏡像
現(xiàn)在容器中exit命令退出容器
docker commit fb4ac22da9b0 centos_php7_swoole
查看打包好的鏡像:
docker images | grep php72_swoole

image.png
5.在docker 中新建一個http_server.php文件測試
<?php
$http= new Swoole\Http\Server("0.0.0.0", 9501);
$http->on('request', function ($request, $response) {
var_dump($request->get, $request->post);
$response->header("Content-Type", "text/html; charset=utf-8");
$response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});
$http->start();
退出docker
exit;
后臺運行centos_php7_swoole容器
docker run -d -p 9501:9501 centos_php7_swoole /usr/local/php7_2/bin/php /usr/local/work/http_server.php

image.png
6.瀏覽器訪問,看到如下,就成功了!

image.png