Skip to content

Latest commit

 

History

History
226 lines (158 loc) · 8.23 KB

项目服务器部署.md

File metadata and controls

226 lines (158 loc) · 8.23 KB

项目服务器部署

  1. 阿里云-系统选择——Centos8.2

    优势:

    1. Linux比window速度更快,系统进程内存占用更小;
    2. Centos比Ubuntu 的系统进程内存占用更小,更适合于企业级开发。
  2. node、npm安装及全局环境配置

    1. 下载最新稳定版node;

    2. 选择任意路径如,/app/software/解压上传

      tar -xvf  node-v6.10.0-linux-x64.tar.xz
      mv node-v6.10.0-linux-x64  nodejs
      // 确认一下nodejs下bin目录是否有node 和npm文件,
      // 如果有执行软连接,如果没有重新下载执行上边步骤;
      
    3. 建立软连接,变为全局:

       ln -s /app/software/nodejs/bin/npm /usr/bin/
       ln -s /app/software/nodejs/bin/node /usr/bin/
      
    4. 检验node、npm版本,是否已变为全局

      node -v
      npm -v
      
    5. 查看node和node_modules所在位置

      npm root -g		//查看node_modules的目录
      npm bin -g		//查看npm的可执行文件所在目录
      npm config set prefix /usr/local	//修改npm的工作目录
      
  3. 安装puppeteer+Chromium

    npm i puppeteer -g  //会报错
    ERROR: Failed to set up Chromium r856583! Set "PUPPETEER_SKIP_DOWNLOAD" env variable to skip download.
    

    安装淘宝的cnpm,自动使用国内源

    npm install -g cnpm --registry=https://registry.npm.taobao.org
    cnpm i puppeteer -g
    

    测试无头模式:

    //上传testLinux.js、相关库

    const puppeteer = require("puppeteer");
    const sleep = require("./src/sleep");
    
    (async () => {
      /* 浏览器启动配置 */
      const browser = await puppeteer.launch({
        headless: true,
        defaultViewport: {
          width: 1600,
          height: 1024,
        },
        ignoreDefaultArgs: ["--enable-automation"],
      });
      async function mouseDown(selector, page) {
        try {
          const allselector = await page.waitForSelector(selector);
          const allinfo = allselector.boundingBox();
          await page.mouse.move((await allinfo).x + 2, (await allinfo).y);
          await page.mouse.down();
          await page.waitForTimeout(80 + Math.random() * 40);
          await page.mouse.up();
        } catch (error) {
          console.log("Again");
        }
      }
      const page = await browser.newPage();
      await page.goto("https://www.baidu.com/");
      page.waitForSelector(login);
      await mouseDown(login, page);
      sleep(800);
      console.log("successfuly!");
    })();
    
    const login = "#u1 > a";
    node testLinux //报错
    
    (node:804034) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
    /root/coinEx/node_modules/_puppeteer@8.0.0@puppeteer/.local-chromium/linux-856583/chrome-linux/chrome: error while loading shared libraries: libxshmfence.so.1: cannot open shared object file: No such file or directory
    
    
    TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
    
        at onClose (/root/coinEx/node_modules/_puppeteer@8.0.0@puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:193:20)
        at Interface.<anonymous> (/root/coinEx/node_modules/_puppeteer@8.0.0@puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:183:68)
        at Interface.emit (events.js:327:22)
        at Interface.close (readline.js:424:8)
        at Socket.onend (readline.js:202:10)
        at Socket.emit (events.js:327:22)
        at endReadableNT (internal/streams/readable.js:1327:12)
        at processTicksAndRejections (internal/process/task_queues.js:80:21)
    (Use `node --trace-warnings ...` to show where the warning was created)
    (node:804034) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
    (node:804034) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    

    查到网上:原来puppet虽然帮你下了一个Chromium,但并没有帮你把依赖都装好。于是你要自己把那些so都装好。

    #依赖库
    yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 -y
    
    #字体
    yum install ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc -y
    

    再此执行,还是同样报错——仔细看错误原因是chromium无法调用lib动态库.so文件

    error while loading shared libraries: libxshmfence.so.1: cannot open shared object file: No such file or directory
    
    yum install libxshmfence.so.1 //无效,报错
    whereis libxshmfence.so.1 //查看库文件libxshmfence.so: /usr/lib/libxshmfence.so.1,能找得到
    

    再次用网上的解决办法

    //法一:无效
    # vim /etc/ld.so.conf      //在新的一行中加入库文件所在目录
      /usr/lib  
    # ldconfig                 //更新/etc/ld.so.cache文件
    
    //法二:无效
    ldd $(which /root/coinEx/node_modules/puppeteer/.local-chromium/linux-856583/chrome-linux/chrome)
    ln -s /usr/local/lib64/libxshmfence.so.1 /lib/libxshmfence.so.1//上面这个好像写反了
    ln -s /lib/libxshmfence.so.1 /usr/local/lib64/libxshmfence.so.1
    

    看着libxshmfence.so.1就在我眼前,然而chromium这傻孩子完全眼瞎了似的:

    image-20210227172248182

    终极解决办法

    找到chromium-browser的快捷方式,强制设为启动路径即可!

    image-20210302234018870

    /usr/bin/chromium-browser
    
  4. 无头模式下登录OKEx

    无头模式登录后会遇到人工滑块验证,鼠标轨迹很难模拟,难以通过。。。

    还是决定先使用centos8图形化后登录

    图形化界面参考阿里云安装教程:

    https://help.aliyun.com/knowledge_detail/151830.html?spm=5176.smartservice_service_chat.0.0.7df83f1bLaSuz0

    注意:新增VNC配置文件~/.vnc/config的~千万不要漏,就是因为这个坑卡了我好几天,吃了大亏,血与泪的教训啊!!!

    chromium-browser --no-sandbox //图形界面终端启动,鼠标点击运行没反应,可能是没有nosandbox且在root下运行的原因
    

    Chromium登录账号,使得userdata存档,一直保持自动登录状态。

    获取userdata路径

    image-20210303114951334

    image-20210303115027487

    通常是/root/.config/chromium/Default

    完成后,暂时关闭centos8的图形界面,减少内存占用,往后有需要再开启:

    init 3 // 关闭图形界面,重启会自动开启
    init 5 // 开启图形界面
    
  5. 上传项目并测试

    image-20210303125142371

  6. ssh session在断开连接后保持运行

开发中的问题

  • 图形界面中无法启动Chrome和Chromium,但可以启动FireFox;
  • Linux-FireFox中无法登录OKEx账号 VS 2008 IE网速极慢,内存占用过大;
  • BinanceAPI路线需翻墙,不现实,故暂时还是采用Chromium+Puppeteer完成量化交易;
  • Linux-Chormium+puppeteer可以实现无头浏览,但缺少user data,无法从window下迁移;