Swoft| source code interpretation series 1: how difficult! Swoft demo can’t run. how can it be broken? Docker, get to know it ~

  composer, docker, php, swoft, swoole

date: 2018-8-01 14:22:17
Title: swoft| Source Code Interpretation Series 1: How Difficult! Swoft demo can’t run. how can it be broken? Docker, get to know it ~
Description: read sowft framework source code, swoft first step, fix the environment.

The little buddy will feel when he first touches swoft.The pressure is a little high., a more intuitive statement isDifficultThe development team does not approveDifficultAccording to this statement, swoft code is implemented in php, and php isThe best language in the worldThe swoft code is easy to read.

The development team will useSource Code Interpretation SeriesBlog, in-depth interpretation of swoft. We believe that this will be an easy journey.

Swoft source code interpretation series 1: how difficult! Swoft demo can’t run. how can it be broken? Docker, get to know it ~

A quick reading of this article:

  • I don’t back the pot of the environment: learning and using swoft need to prepare these.Basic knowledge.
  • Docker: handle swoft environment
  • Docker-compose: Get More Services
  • Package Management Using composer: Daily Activities of Development Groups

I don’t back the pot of the environment.

This is really not swoft’s pot, it is the pot of the environment. swoft’s official documents have already provided quite detailed information.Environmental constructionExplain that if you have been unable to succeed:

  • The recommended operating environment for swoole linux, linux. Have you mastered some basic concepts and operations of Linux?
  • Swoole works as a php extension. Do you know some basic concepts and installation of php extension
  • Swoft uses composer for package management. Have you mastered some basic concepts and operations of composer
  • Using swoft to develop back-end applications, do you have some basic knowledge of the server (tcp/ip four-layer network protocol, ip port domain name, etc.)

Learning and using swoft requires preparation of these.Basic knowledge.

docker

So, how to quickly match the development environment? The answer isdocker!

Docker is easier to get started than expected, and the development team providedOfficial mirror swoft/swoft, mirror details inSwoft project dockerfile, extract the part where swoole is configured:

# Swoole extension
RUN wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz \
    && mkdir -p swoole \
    && tar -xf swoole.tar.gz -C swoole --strip-components=1 \
    && rm swoole.tar.gz \
    && ( \
        cd swoole \
        && phpize \
        && ./configure --enable-async-redis --enable-mysqlnd --enable-openssl --enable-http2 \
        && make -j$(nproc) \
        && make install \
    ) \
    && rm -r swoole \
    && docker-php-ext-enable swoole

If you haven’t mastered the configuration of the environment required for swoole operation, you can refer to the source code of this dockerfile file.

Of course, for the convenience of development, we may need to build different environments, such as specifying different versions of php, using different versions of swoole, setting up Chinese image acceleration, etc. You can also refer togitee.com/daydaygo/dockerDockerfile under

FROM php:7.2.5-cli-alpine3.7
# FROM php:7.1.13-cli-alpine3.4
LABEL maintainer="1252409767@qq.com"
RUN echo -e "http://mirrors.ustc.edu.cn/alpine/v3.7/main\nhttp://mirrors.ustc.edu.cn/alpine/v3.7/community" > /etc/apk/repositories && \
    apk update
RUN apk add tzdata && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" >  /etc/timezone
RUN apk add $PHPIZE_DEPS

# docker-php-ext
RUN docker-php-ext-install bcmath mysqli pcntl pdo_mysql

# pecl
# http://pecl.php.net/package/mongodb
RUN curl -O http://pecl.php.net/get/redis-4.0.2.tgz && \
    pecl install redis-4.0.2.tgz && \
    docker-php-ext-enable redis
RUN curl -O http://pecl.php.net/get/mongodb-1.4.3.tgz && \
    apk add openssl-dev && \
    pecl install mongodb-1.4.3.tgz && \
    docker-php-ext-enable mongodb

# swoole
RUN curl -O https://gitee.com/swoole/swoole/repository/archive/v4.0.3.zip && unzip v4.0.3.zip && \
    apk add linux-headers openssl-dev nghttp2-dev hiredis-dev && \
    cd swoole && \
    phpize && \
    ./configure --enable-coroutine --enable-openssl --enable-async-redis --enable-http2 && make && make install && \
    docker-php-ext-enable swoole && \
    rm -rf v4.0.3.zip swoole

Ps: The dockerfile here uses alpine linux as the basic image, with a size of less than 10m. It is a very simple and pure linux distribution. It is recommended to try it out. Domestic sources and gitee are used for acceleration.

docker-compose

Here we already have the swoft operating environment, according toDocument-Service StartupThe instructions in the chapter, executionphp bin/swoft startSwoft demo can be put into operation.

If we need more services, mysql, redis, and even nginx, docker-compose can help us. docker-compose is used to organize (manage) each of our docker services, for examplegitee.com/daydaygo/docker:

    nginx:
        build:
            context: nginx
            dockerfile: Dockerfile
        volumes:
            - ../:/var/www
            - ./logs/nginx/:/var/log/nginx
        links:
            - swoft
        ports:
           - "80:80"
           - "443:443"
    swoft:
        # container_name: swoft
        image: swoft/swoft
        volumes:
            - ../:/var/www
        links:
            - mysql
            - redis
        ports:
            - "8001:8001"
            - "9501:9501"
        working_dir: /var/www/swoole/swoft
        stdin_open: true
        command: php -a
        tty: true
            - redis
    mysql:
        build:
            context: mysql
            dockerfile: Dockerfile
        volumes:
            - ./data/mysql:/var/lib/mysql
        ports:
            - "3306:3306"
        environment:
            MYSQL_ROOT_PASSWORD: root
    redis:
        build:
            context: redis
            dockerfile: Dockerfile
        volumes:
            - ./data/redis:/data
            - ./logs/redis:/var/log/redis
        ports:
            - "6379:6379"

Here we started 4 services: NGINX/SW OFT/MySQL/Redis:

  • links: The relationship between services, such as mysql and redis, can be used in swoftmysqlAs ahostAccess to myql service
  • ports: port mapping
  • volumes: File Mount

If you know what these labels do, you can understand and use docker-compose.

Package Management Using composer

Usegit cloneAfter downloading the source code of swoft, you need to usecomposer installThe swoft framework was installed. Before that, swoft carried outComponent splitTo further decouple the framework and make it easy to use. Ordinary users rarely modify it.composer installThe swoft framework is installed, but the development team will frequently update the swoft framework and then verify it in the swoft demo project. how is that achieved?

The answer is provided by composer.repositoriesFunction, directly introduced into the code base:

{
    "name": "swoft/swoft",
    "type": "project",
    "keywords": [
        "php",
        "swoole",
        "swoft"
    ],
    "description": "Modern High performance AOP and Coroutine PHP Framework, base on Swoole 2",
    "license": "Apache-2.0",
    "require": {
        "php": ">=7.0",
        "ext-swoole": ">=2.1",
        "swoft/framework": "^1.0",
        "swoft/rpc": "^1.0",
        "swoft/rpc-server": "^1.0",
        "swoft/rpc-client": "^1.0",
        "swoft/http-server": "^1.0",
        "swoft/http-client": "^1.0",
        "swoft/websocket-server": "^1.0",
        "swoft/task": "^1.0",
        "swoft/http-message": "^1.0",
        "swoft/view": "^1.0",
        "swoft/db": "^1.1",
        "swoft/cache": "^1.0",
        "swoft/redis": "^1.0",
        "swoft/console": "^1.0",
        "swoft/devtool": "^1.0",
        "swoft/session": "^1.0",
        "swoft/i18n": "^1.0",
        "swoft/process": "^1.0",
        "swoft/memory": "^1.0",
        "swoft/service-governance": "^1.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "app/Swoft.php"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Swoft\\Test\\": "test/"
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "test": "./vendor/bin/phpunit -c phpunit.xml",
        "cs-fix": "./vendor/bin/php-cs-fixer fix $1"
    },
    "repositories": [
        {
            "type": "path", // 修改在此
            "url": "../swoft-component"
        }
    ]
}

I am used to deleting itrequire-devUnder the configuration of the package, and choose the local global installation, this depends on personal preferences

Write at the end

I hope you can see the first page of swoft demo.swoftWhen, can be as happy as us