zoukankan      html  css  js  c++  java
  • tp5 + laradock 从零搭建小程序后端问题总结

    一、laradock首次up的时候,build过程非常慢

     由于拉取容器的镜像默认在国外,所以在docker-compose up 容器之前,在.env文件中查找以下文字

    CHANGE_SOURCE=false

     然后将false改为true,并查找以下文字

    #UBUNTU_SOURCE=aliyun
    

     把前面的 # 去掉,即去掉注释,把默认在国外的镜像源改为国内速度快的aliyun的镜像源

     然后把workspace的时区,改为中国时区,查找以下文字

    WORKSPACE_TIMEZONE=UTC

     把UTC改为PRC

     然后执行命令

    docker-compose up -d nginx mariadb
    

     自己选择需要启动的容器

     亲自对比过build的时间,在20M带宽下,不更改镜像源的话快则一小时,慢则一天,更改过后,仅需10min 

    二、laradock在启动容器 build workspace 的过程中报错

     若出现相关文字 "raw.githubusercontent.com",则在本地hosts绑定域名到ip

     以linux为例

    vim /etc/hosts
    

     在文件中输入 "199.232.28.133 raw.githubusercontent.com" 即可

    三、composer install 速度慢

    安装完laradock在worksapce容器中从第三方托管仓库用git拉取tp5框架下来,然后安装框架依赖,这个时候composer install很慢,需要更换国内源,执行下面命令

    composer config -g repo.packagist composer https://packagist.phpcomposer.com
    

     四、composer忽略版本号安装

     composer install 过程中遇到 Your requirements could not be resolved to an installable set of packages. 错误提示,则可以忽略版本号安装,执行下面命令

    composer install --ignore-platform-reqs
    

     五、composer安装tp5的数据库迁移工具

     tp5.0的迁移工具是 1.*,tp5.1是 2.*,若不指定版本号,则默认安装最新的迁移工具,执行下面命令

    composer require topthink/think-migration=1.*
    

     六、laradock中tp5配置数据库连接时,host填写容器名字

     我用的mariadb这个容器,所以如下配置

    'host'=>'mariadb'

     七、tp5给runtime全部权限

    chmod -r runtime 777

     八、tp5捕获mysql的抛出的异常

     在catch的参数Exception前面加个反斜线,表示从最底层的Exception开始捕获

    catch(Exception)
    

     九、做图片上传接口时,mkdir报错 no permission

     为了引用图片方便,我把图片存储目录指定为 public/uploads ,但是报错了,可以通过给public全部权限解决

    chmod -r public 777

     十、后端接口的时候权限验证

     oauth2时一个非常好的授权机制,php有一个很好的库 https://github.com/thephpleague/oauth2-server, 但是很无奈,不像laravel有passport,也支持Drupal,cakephp等框架

     使用Json-web-token也蛮好的 https://github.com/lcobucci/jwt

     十一、跨域处理

      在application目录下的tags.php中的添加应用初始化时执行的文件,例如我把跨域放在 applicationapi/behavior/CORS.php 文件中,在应用初始化过程中就发出header

        // 应用初始化
        'app_init'     => [
            'app\api\behavior\CORS'
        ],
    

     跨域文件

    <?php
    namespace appapiehavior;
     
    use thinkResponse;
     
    class CORS
    {
        public function appInit(&$params)
        {
            header('Access-Control-Allow-Origin: *');
            header("Access-Control-Allow-Headers: token,Origin, X-Requested-With, Content-Type, Accept");
            header("Access-Control-Allow-Methods:GET, POST");
            if (request()->isOptions()) {
                exit();
            }
        }
    }

     十二、git push/pull 需要验证身份, 多次输入用户名和密码

     linux环境中,在仓库目录下执行

    git config --global credential.helper store

     十三、git push 卡住

     设置发包无边界,http的请求换缓冲区设置得大一些

    git config --global sendpack.sideband false
    git config --global http.postBuffer 524288000

     十四、nginx不支持tp5的pathinfo

     更改项目对应的 .conf 文件配置

    location / {
             try_files $uri $uri/ /index.php$is_args$args;
             # 添加部分 ↓↓↓↓↓
             if (!-e $request_filename) {
                    rewrite ^(.*)$ /index.php?s=$1 last;
                    break;
             }
             # 添加部分 ↑↑↑↑↑
    }
     # 更改前 location ~ .php$ {
     location ~ .php { 
            try_files $uri /index.php =404;
            fastcgi_pass php-upstream;
            fastcgi_index index.php;
            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;
            # 添加部分↓↓↓↓
            # Set var PATH_INFO
            fastcgi_split_path_info ^(.+.php)(.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            #  添加部分↑↑↑↑
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;   
            #fixes timeouts
            fastcgi_read_timeout 600;
            include fastcgi_params;
    }

    具体选项待我找时间学习一下

    十四、Docker配置国内源

    sudo tee /etc/docker/daemon.json <<-'EOF'

    vim /etc/docker/daemon.json
    # 输入以下内容,地址可以自己额外选择
    {
    
      "registry-mirrors": ["https://uxk0ognt.mirror.aliyuncs.com"]
    
    }
    #然后执行下面的指令
    
    systemctl daemon-reload
    systemctl restart docker

    备注

    众所周知,从github克隆一直比较慢,可以选择用 码云 ,提前将常用的仓库导入到自己的 码云工作空间 ,需要使用的时候通过码云克隆

  • 相关阅读:
    从Oracle提供两种cube产品说开
    Sql Server DWBI的几个学习资料
    Unload Oracle data into text file
    初学Java的几个tips
    我常用的Oracle知识点汇总
    benefits by using svn
    如何在windows上使用putty来显示远端linux的桌面
    building commercial website using Microsoft tech stack
    Understand Thread and Lock
    Update google calendar by sunbird
  • 原文地址:https://www.cnblogs.com/YC-L/p/12535549.html
Copyright © 2011-2022 走看看