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 \ 检查环境
--prefix=/usr/local/php7_2 \ 指定php安装路径
--with-config-file-path=/etc 配置文件路径
make && make install 编译并安装
将php加到环境变量中:
vi ~/.bashrc 编辑当前用户环境变量文件
alias php7=/usr/bin/php 增加一行
source ~/.bashrc 使环境变量生效
将php.ini配置文件加到/etc/目录下面
cp php.ini-development /etc/php.ini
可以去到 /usr/local/php7_2查看一下php的目录

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.
可以执行一下:
yum install autoconf
查看当前目录到了一个configure文件
./configure \
--with-php-config=/usr/local/php7_2/bin/php-config
make && make install
安装完成之后,打开/etc/php.ini
加入
extension=swoole.so
执行php7 -m可以查看swoole是否安装成功。

4将容器打包成镜像
现在容器中exit命令退出容器
docker commit fb4ac22da9b0 centos_php7_swoole
查看打包好的镜像:
docker images | grep php72_swoole

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

6.浏览器访问,看到如下,就成功了!

网友评论