首先建立Dockerfile,因为php需要安装很多扩展
FROM php:7.2-fpm
RUN apt-get update
RUN apt-get install -y build-essential \
git \
curl \
zip \
wget
RUN apt-get autoclean
RUN /usr/local/bin/docker-php-ext-install mysqli \
json \
mbstring
RUN wget https://github.com/phpredis/phpredis/archive/5.1.0.tar.gz \
&& tar -zxvf 5.1.0.tar.gz \
&& cd phpredis-5.1.0 \
&& phpize \
&& ./configure \
&& make \
&& make install
RUN wget https://github.com/swoole/swoole-src/archive/v4.4.12.tar.gz -O swoole-src-4.4.12.tar.gz \
&& tar -zxvf swoole-src-4.4.12.tar.gz \
&& cd swoole-src-4.4.12 \
&& phpize \
&& ./configure \
&& make \
&& make install
RUN wget https://github.com/swoole/ext-async/archive/v4.4.12.tar.gz -O ext-async-4.4.12.tar.gz \
&& tar -zxvf ext-async-4.4.12.tar.gz \
&& cd ext-async-4.4.12 \
&& phpize \
&& ./configure \
&& make -j 4 \
&& make install
RUN /usr/local/bin/docker-php-ext-enable swoole redis swoole_async
然后建立docker-compose.yml
version: "2"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./site.conf:/etc/nginx/conf.d/default.conf
networks:
- swooleim
php:
build: ./
volumes:
- ./code:/code
ports:
- "9000:9000"
networks:
- swooleim
mysql:
image: mysql:5.7
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=testedb
networks:
- swooleim
redis:
restart: always
image: redis:latest
ports:
- "6379:6379"
command: redis-server --appendonly yes
networks:
- swooleim
networks:
swooleim:
接著配置nginx
建立site.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /code;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /code;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /code;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
测试
新建index.php
$conn = new mysqli("mysql", 'root', '123456', 'testedb');
if(!$conn){
die($conn->connect_error);
}
$result = mysqli_query($conn, "show tables");
var_dump($result);
echo '-----------------------<br/>';
$redis = new redis();
$result = $redis->connect('redis', 6379);
var_dump($result); //结果:bool(true)
echo '-----------------------<br/>';
$client = new swoole_client(SWOOLE_SOCK_TCP);
//连接到服务器
if (!$client->connect('127.0.0.1', 9501, 0.5))
{
die("connect failed.");
}
//向服务器发送数据
if (!$client->send("hello world"))
{
die("send failed.");
}
//从服务器接收数据
$data = $client->recv();
if (!$data)
{
die("recv failed.");
}
echo $data;
//关闭连接
$client->close();
在新建swoole.php作为tcp服务端
//创建Server对象,监听 127.0.0.1:9501端口
$serv = new Swoole\Server("127.0.0.1", 9501);
//监听连接进入事件
$serv->on('Connect', function ($serv, $fd) {
echo "Client: Connect.\n";
});
//监听数据接收事件
$serv->on('Receive', function ($serv, $fd, $from_id, $data) {
$serv->send($fd, "Server: ".$data);
});
//监听连接关闭事件
$serv->on('Close', function ($serv, $fd) {
echo "Client: Close.\n";
});
//启动服务器
$serv->start();
运行swoole.php
docker exec -it fcaac86a4a7e /bin/bash -c "php /code/swoole.php"
运行docker
docker-compose up
在浏览器输入localhost:8080
如果没有报错,并输出了hello world,并且在swoole.php服务端下面能看到Client: Connect. Client: Close.字样则表示运行成功

1060

被折叠的 条评论
为什么被折叠?



