美文网首页
Linux上通过Docker搭建Swoft2.x

Linux上通过Docker搭建Swoft2.x

作者: haokeed | 来源:发表于2019-11-08 14:30 被阅读0次

    主要参考:https://www.jianshu.com/p/37d2e9dadd33
    参考之后发现还是有的地方不一样的。我是基于centos7.6(http://archive.kernel.org/centos-vault/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso)安装的。

    一.先把centos的基本环境清理一番

    1.更新update到最新版本
    # yum  update
    2.卸载老版本的docker
    # yum  remove docker  docker-common docker-selinux  docker-engine
    3.安装需要的软件包
    # yum install -y yum-utils  device-mapper-persistent-data lvm2
    4.设置yum源(还是马爸爸的镜像源好用)
    # yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    

    二.安装docker

    1.查看可选的docker版本
    # yum list docker-ce --showduplicates|sort -r 
    2.安装docker
    # yum  install  docker-ce-18.03.1.ce -y
    3.启动docker
    # systemctl start docker
    4.配置国内镜像
    vi  /etc/docker/daemon.json
    {
     "registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"]
    }
    5.重载配置
    # systemctl reload docker
    6.重启docker
    # systemctl restart docker
    

    三.安装docker-compose

    1.安装epel-release,这个安装是为了防止直接运行python-pip 报【No package python-pip available】
    # yum -y install epel-release
    2.安装docker-compose
    # yum install python-pip -y && pip install --upgrade pip && pip install -U docker-compose
    

    以上方式安装发现因为python版本和系统版本问题的影响较大,每次安装都报不同的错误,都要逐个去解决,所以十分麻烦。后面找了另外一种安装方式,使用过来还是比较好的:

    1. 拉取docker-compose
    # wget https://github.com/docker/compose/releases/download/1.23.0-rc3/docker-compose-Linux-x86_64
    2.将docker-compose移动至系统执行目录中
    # mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
    3. 给docker-compose添加可执行权限
    # chmod +x /usr/local/bin/docker-compose
    4.查看docker-compose版本
    # docker-compose --version
    

    四.修改官方默认docker-compose.yml文件

    通过命令 git clone https://github.com/swoft-cloud/swoftGithub克隆(clone) Swoft 项目。
    docker-compose.yml修改如下:

    version: '3.4'
    services:
      swoft:
        image: swoft/swoft
    #    for local develop
        command: php -S 127.0.0.1:13300 # 设置为需要手动启动
        container_name: swoft-srv # 容器名
        environment:
          - APP_ENV=dev
          - TIMEZONE=Asia/Shanghai
        restart: always
        depends_on:
          - mysql
          - redis
        ports:
          - "18306:18306"
          - "18307:18307"
          - "18308:18308"
        volumes:
          - ./:/var/www/swoft  #将宿主机当前所在项目映射到容器/var/www/swoft目录中
    

    五.安装基本配置文件

    composer install --no-dev # 不安装 dev 依赖会更快一些
    cp .env.example .env
    vim .env # 根据需要调整启动参数
    

    六.开启容器

    在docker-compose.yml 同级目录下,执行 docker-compose up -d
    命令执行后我们可以看到 Starting swoft ... done 即表示启动容器成功。如果设置了手动启动(command: php -S 127.0.0.1:13300),是没有这个提示的。

    七.进入容器

    执行docker ps -a命令可以查看启动的容器信息,下面为示例信息:


    image.png

    得知 容器名称(Container Name)为 swoft-srv,我们可以执行 docker exec -it swoft-srv bash 进入到容器内。
    通过 docker exec 命令进入容器后,我们留意到光标左侧的内容root@cd4c36fa7cf7: 即为已进入容器内,其中 cd4c36fa7cf7为对应的 容器ID(Container ID)。

    八.安装 Composer 依赖及生成 自动加载(Autoload) 文件

    由于 Swoft 官方镜像 swoft/swoft 配置的工作目录为 /var/www/swoft,而 docker-compose.yml 又将项目当前目录关联了容器 /var/www/swoft 目录,即通过 docker exec 进入的目录已经为 /var/www/swoft 目录,即项目目录,所以我们可以直接执行 composer install 命令来加载 Composer 的依赖并生成 自动加载(Autoload) 文件。
    考虑到国内的网络环境,我们在执行 composer install 命令前可以先执行 composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ 命令配置 Composer 中国镜像源 加速安装速度。
    当然你也可以换成其他的源,这里我列出了常用的源,当然swoft官方也有说明了。

    九.启动 Swoft 服务

    安装完 Composer 依赖后,便可以执行 php bin/swoft http:start 启动服务了。
    我们可以打开浏览器访问一下 http://192.168.229.129:18306
    (自己虚拟机域名),当你看到下图即大功告成了!


    image.png

    十.附上相关运行服务命令

    1.帮助命令

    
    # php bin/swoft -h
    Console application description (Version: 2.0.0)
    
    Usage:
      bin/swoft COMMAND [arg0 arg1 arg2 ...] [--opt -v -h ...]
    
    Options:
          --debug      Setting the application runtime debug level(0 - 4)
          --no-color   Disable color/ANSI for message output
      -h, --help       Display this help message
      -V, --version    Show application version information
          --expand     Expand sub-commands for all command groups
    
    Available Commands:
      agent      This is an agent for Apllo config center
      app        There are some help command for application[by devtool]
      dclient    Provide some simple tcp, ws client for develop or testing[by devtool]
      demo       Class DemoCommand
      dinfo      There are some commands for application dev[by devtool]
      entity     Generate entity class by database table names[by devtool]
      http       Provide some commands to manage the swoft HTTP server(alias: httpsrv)
      issue      There are some commands for application dev[by devtool]
      migrate    Manage swoft project database migration commands[by devtool](alias: mig)
      process    Provides some command for manage process pool
      rpc        Provide some commands to manage swoft RPC server
      tcp        Provide some commands to manage swoft TCP server(alias: tcpsrv)
      test       Class TestCommand
      ws         Provide some commands to manage swoft websocket server(alias: wsserver,websocket)
    
    More command information, please use: bin/swoft COMMAND -h
    
    reload只是重新释放一下内存,只在开发过程中有用,线上请勿使用。
    
    更新代码时可以放心使用restart,swoole底层做了优化,不会中断正在处理的请求
    

    2.http服务器

    // 启动服务
    php bin/swoft http:start
    
    // 守护进程启动
    php bin/swoft http:start -d
    
    // 重启
    php bin/swoft http:restart
    
    // 重新加载
    php bin/swoft http:reload
    
    // 关闭服务
    php bin/swoft http:stop
    

    3.WebSocket服务器
    启动WebSocket服务器,可选是否 同时支持http处理

    // 启动服务
    php bin/swoft ws:start
    
    // 守护进程启动
    php bin/swoft ws:start -d
    
    // 重启
    php bin/swoft ws:restart
    
    // 重新加载
    php bin/swoft ws:reload
    
    // 关闭服务
    php bin/swoft ws:stop
    

    4.rpc服务器
    使用独立的RPC服务器

    // 启动服务
    php bin/swoft rpc:start
    
    // 守护进程启动
    php bin/swoft rpc:start -d
    
    // 重启
    php bin/swoft rpc:restart
    
    // 重新加载
    php bin/swoft rpc:reload
    
    // 关闭服务
    php bin/swoft rpc:stop
    

    相关文章

      网友评论

          本文标题:Linux上通过Docker搭建Swoft2.x

          本文链接:https://www.haomeiwen.com/subject/clpibctx.html