fpm + nginx alpine docker 运行

就是一个简单的php docker 环境

环境准备

  • 项目结构
├── Dockerfile
├── README.md
├── app
│   ├── app.php
│   ├── composer.json
│   ├── composer.lock
│   ├── index-xhprof.php
│   ├── index.php
│   ├── perf
│   └── vendor
├── docker-compose.yaml
├── nginx.conf
├── php.conf
├── php.ini
└── supervisor.conf
  • Dockerfile
    集成了perf,可以进行perf 分析
 
FROM alpine
ENV TZ Asia/Shanghai
# first add mirror for alpine repos
RUN set -x \
    && /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories
RUN apk update && apk add  --no-cache nginx nginx-debug php7-fpm  php7-json  perf  tzdata  && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY --from=ochinchina/supervisord:latest /usr/local/bin/supervisord /usr/local/bin/supervisord
CMD ["/usr/local/bin/supervisord","-c","/etc/supervisor.conf"]
  • docker-compose 文件
version: "3"
services:
  web: 
     build: ./
     volumes:
     - "./php.conf:/etc/nginx/conf.d/default.conf"
     - "./nginx.conf:/etc/nginx/nginx.conf"
     - "./app:/opt/app"
     - "./supervisor.conf:/etc/supervisor.conf"
     privileged: true
     cap_add:
       - ALL
     ports:
     - "80:80"
     - "8080:8080"
     - "9001:9001"
  • supervisor.conf
    使用了一个golang 版本的supervisord,简单方便
 
[program:nginx]
command = nginx -g  "daemon off;"
autorestart = true
user = root
depends_on = mkdir-nginx
[program:fpm]
command = php-fpm7 -F
autorestart = true
[inet_http_server]
port = :9001
[program:mkdir-nginx]
command = mkdir -p /run/nginx
  • nginx 配置
    主要是php-fpm 配置
 
server {
       listen 80;
       charset utf-8;
       default_type text/html;
       location / {
            root /opt/app; 
            index index.php;
        }
       location ~ \.php$ {
           fastcgi_pass 127.0.0.1:9000;
           root           /opt/app; 
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
           include        fastcgi_params; 
        }
        location ~ /\.ht {
                deny all;
        }
        error_page 404 /404.html;
}
  • php 代码
    index.php
 
<!DOCTYPE html>
<html>
<body>
<?php
include "app.php";
echo "<div>Hello World!</div>"
?>
</body>
</html>

app.php
集成shortid 基于composer 管理

 
<?php
require_once 'vendor/autoload.php';
use PUGX\Shortid\Shortid;
$id = Shortid::generate();
echo $id;
?>
 

composer.json

{
    "name": "dalong/app",
    "type": "project",
    "require": {
        "pugx/shortid-php": "^0.7.0"
    },
    "authors": [
        {
            "name": "rong",
            "email": "1141591465@qq.com"
        }
    ]
}

参考资料

https://github.com/rongfengliang/php-nginx-docker

posted on 2020-12-30 19:52  荣锋亮  阅读(285)  评论(0编辑  收藏  举报

导航