搭建企业级npm服务

为什么要搭建npm企业私服

  1、确保npm服务快速、稳定:对于企业来说,开发的时候,需要花半小时甚至更久等待npm模块依赖安装完毕,是不可接受的。部署镜像后,可以确保高速、稳定的npm服务。

  2、发布私有模块:官方的npm上的模块全部是开源的。一些与企业业务逻辑相关的模块可能不适合开源。这部分私有的模块放在私有NPM仓库中,使用起来各种方便。

  3、控制npm模块质量和安全:npm上的模块质量参差不齐,搭建私有仓库,可以更严格地控制模块的质量和安全

  4、有些npm上面的包你希望更改源码二次开发而无从下手的时候,你只需要修改源码后需改为更高版本发布到私服上就可以使用维护了

 mac上测试

sinopia

安装

cnpm i sinopia -g

启动

sinopia

 warn  --- config file  - /Users/**/.config/sinopia/config.yaml

 warn  --- http address - http://localhost:4873/

 

注意:上面输出的两条信息相当重要

  1. 服务器中 sinopia 的配置文件存放的位置,后期的配置都需要修改这个文件
  2. sinopia 提供服务的地址,默认4873

 

sinopia 就搭建完成了,还能更简单么?No。

配置 sinopia

sinopia 搭建完成后就可以发布 npm 包了,

npm adduser

npm publish --registry=http://localhost:4873

bogon:haha$ npm publish --registy=http://localhost:4873
npm notice 
npm notice 📦  haha@1.0.0
npm notice === Tarball Contents === 
npm notice 222B package.json
npm notice 0    hi-jj--kk.js
npm notice === Tarball Details === 
npm notice name:          haha                                    
npm notice version:       1.0.0                                   
npm notice package size:  280 B                                   
npm notice unpacked size: 222 B                                   
npm notice shasum:        1bccba5c7d647d6e984c1838bd00b85779967e7e
npm notice integrity:     sha512-WlA1hK5UVoYoF[...]4PHK9wuHBq+FA==
npm notice total files:   2                                       
npm notice 
+ haha@1.0.0

  显示就成功了,在localhost:4873就可以看到发布的包了

 

npm unpublish haha  --force

npm WARN using --force I sure hope you know what you are doing.

- haha

如果发布错误就可以删除了.这个和公共npm不同的是删除之后还可以继续发布相同名称的包.

 

主要还是说说如何个性化配置 sinopia。

编辑刚刚 log 中输出的配置文件,可以配置 sinopia。

参考https://github.com/rlidwka/sinopia/blob/master/conf/full.yaml进行配置

以下是简单的配置

#
# This is the default config file. It allows all users to do anything,
# so don't use it on production systems.
#
# Look here for more config file examples:
# https://github.com/rlidwka/sinopia/tree/master/conf
#

# path to a directory with all packages
storage: ./storage

auth:
  htpasswd:
    file: ./htpasswd
    # Maximum amount of users allowed to register, defaults to "+inf".
    # You can set this to -1 to disable registration.
    max_users: 1000

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: https://registry.npm.taobao.org

packages:
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated

  '*':
    # allow all users (including non-authenticated users) to read and
    # publish all packages
    #
    # you can specify usernames/groupnames (depending on your auth plugin)
    # and three keywords: "$all", "$anonymous", "$authenticated"
    access: $all

    # allow all known users to publish packages
    # (anyone can register by default, remember?)
    publish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: npmjs

# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: sinopia.log, level: info}

  

在linux上的布置

安装node

wget https://npm.taobao.org/mirrors/node/v8.11.1/node-v8.11.1-linux-x64.tar.xz
解压
tar xvf node-v6.11.5-linux-x64.tar.xz

当不能执行 tar命令当时候,要安装xz

yum install xz

  

重命名
mv  node-v6.11.5-linux-x64 node  

  

配置全局变量
vim /etc/profile
插入
NODE_HOME=/usr/local/node
PATH=$NODE_HOME/bin:$PATH 

  重新加载资源,就ok了.

source /etc/profile

  

由于CentOs默认gcc编译版本太低不能满足需求,

npm adduser 的时候报错,ctrys找不到,Canot find ‘ctrys’,而且在装载sinopia的时候,也会在装几个包的时候报错,是linux编译器版本太低了,升级之后就完美解决了

yum update....   yum install 

或者 添加yum 的 repo 文件 也不行, 只能更新到 4.4.7!

 只能手动编译安装了,那么开始第一步下载源代码吧,整体时间大约在一个小时左右

1、 获取安装包并解压

wget http://ftp.gnu.org/gnu/gcc/gcc-6.1.0/gcc-6.1.0.tar.bz2

tar -jxvf gcc-6.1.0.tar.bz2

当然,http://ftp.gnu.org/gnu/gcc  里面有所有的gcc版本供下载,最新版本已经有6.1.0啦.

建议下载.bz2的压缩包,文件更小,下载时间更少.

2、 下载供编译需求的依赖项

参考文献[1]中说:这个神奇的脚本文件会帮我们下载、配置、安装依赖库,可以节约我们大量的时间和精力。

cd gcc-6.1.0

./contrib/download_prerequisites 

3、 建立一个目录供编译出的文件存放

mkdir gcc-build-6.1.0

cd gcc-build-6.1.0

4、 生成Makefile文件

../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib

5、 编译

make -j4

-j4选项是make对多核处理器的优化,如果不成功请使用 make,相关优化选项可以移步至参考文献[2]。

(注意:此步骤非常耗时,我虚拟机耗时近3小时; 实体机近80分钟,CPU基本是满的,内存也使用不少)

6、 安装

make install

(安装需要root权限!)

查看安装

ls /usr/local/bin | grep gcc

7、 重启,然后查看gcc版本

gcc -v

8、 写个C++11 特性的程序段 测试

tryCpp11.cc 代码省略....

g++ -std=c++11 -o tryCpp11 tryCpp11.cc

 

9、升级gcc,生成的动态库没有替换老版本gcc的动态库

源码编译升级安装了gcc后,编译程序或运行其它程序时,有时会出现类似/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found的问题。这是因为升级gcc时,生成的动态库没有替换老版本gcc的动态库导致的,将gcc最新版本的动态库替换系统中老版本的动态库即可解决。

9.1 运行以下命令检查动态库:

strings /usr/lib64/libstdc++.so.6 | grep GLIBC

从输出可以看出,gcc的动态库还是旧版本的。说明出现这些问题,是因为升级gcc时,生成的动态库没有替换老版本gcc的动态库。

9.2 执行以下命令,查找编译gcc时生成的最新动态库:

find / -name "libstdc++.so*"

将上面的最新动态库libstdc++.so.6.0.22复制到/usr/lib64目录下

cd /usr/lib64

cp /root/Downloads/gcc-6.1.0/gcc-build-6.1.0/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.22 ./

9.3 删除原来软连接:

rm -rf libstdc++.so.6

9.4 将默认库的软连接指向最新动态库:

ln -s libstdc++.so.6.0.22 libstdc++.so.6

9.5 默认动态库升级完成。重新运行以下命令检查动态库:

strings /usr/lib64/libstdc++.so.6 | grep GLIBC

可以看到 输出有"GLIBCXX_3.4.21" 了

 

安装cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
安装sinopia,通过cnpm
cnpm i sinopia -g
执行
sinopia

warn --- config file - /root/.config/sinopia/config.yaml
warn --- http address - http://localhost:4873/

  

web管理界面端口是4873;现在只有本机可以访问,其他ip访问不了;

现在修改配置,找到/root/.config/sinopia目录下config.yaml文件;

#
# This is the default config file. It allows all users to do anything,
# so don't use it on production systems.
#
# Look here for more config file examples:
# https://github.com/rlidwka/sinopia/tree/master/conf
#

# path to a directory with all packages
storage: /usr/local/sinopia/storage # 这是资源存储目录

auth:
  htpasswd:
    file: /usr/local/sinopia/htpasswd # 这是授权文件,里面存放用户和密码
    # Maximum amount of users allowed to register, defaults to "+inf".
    # You can set this to -1 to disable registration.
    max_users: -1

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: https://registry.npm.taobao.org/

packages: # 是资源控制
  '@*/*':
    # scoped packages
    access: $authenticated
    publish: $authenticated

  '*':
    # allow all users (including non-authenticated users) to read and
    # publish all packages
    #
    # you can specify usernames/groupnames (depending on your auth plugin)
    # and three keywords: "$all", "$anonymous", "$authenticated"
    # 访问权限,三种:"$all"所有人, "$anonymous"匿名, "$authenticated"授权人
    access: $authenticated

    # allow all known users to publish packages
    # (anyone can register by default, remember?)
    # 发布权限
    publish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: npmjs

# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: sinopia.log, level: info}
# 把之前localhost:4873改0.0.0.0:4873,开发访问
listen: 0.0.0.0:4873
# 绑定域名,不然只通过nginx会报错未设置www啥的
#url_prefix: http://npm.bjnja.com

  这样就可以使用ip访问了 如果需要域名当,申请一个用nginx转发就ok了.

通过pm2管理sinopia

安装

cnpm i pm2 -g

启动

pm2 start sinopia

nginx配置代理

 server {
        listen       80;
        server_name  npm.bjnja.com;

         location / {
                 proxy_pass     http://127.0.0.1:4873;
        }
    }

 

客户使用使用nrm管理 

安装
cnpm i nrm -g

添加
nrm add nja http://npm.**.com

查看源
nrm ls  

npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
  taobao - https://registry.npm.taobao.org/
  nj ----- https://registry.nodejitsu.com/
  rednpm - http://registry.mirror.cqupt.edu.cn/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/
* nja ---- http://npm.**.com/

  补充:现在sinopia作者不维护了,推荐使用verdaccio,配置与sinopia类似

#
# This is the default config file. It allows all users to do anything,
# so don't use it on production systems.
#
# Look here for more config file examples:
# https://github.com/verdaccio/verdaccio/tree/master/conf
#

# path to a directory with all packages
storage: ./storage

auth:
  htpasswd:
    file: ./htpasswd
    # Maximum amount of users allowed to register, defaults to "+inf".
    # You can set this to -1 to disable registration.
    max_users: 3

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: https://registry.npm.taobao.org/

packages:
  '@*/*':
    # scoped packages
    access: $authenticated
    publish: $authenticated
    proxy: npmjs

  '**':
    # allow all users (including non-authenticated users) to read and
    # publish all packages
    #
    # you can specify usernames/groupnames (depending on your auth plugin)
    # and three keywords: "$all", "$anonymous", "$authenticated"
    access: $authenticated

    # allow all known users to publish packages
    # (anyone can register by default, remember?)
    publish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: npmjs

# To use `npm audit` uncomment the following section
middlewares:
  audit:
    enabled: true

# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: verdaccio.log, level: info}
listen: 0.0.0.0:4874

  

参考https://www.jianshu.com/p/659fb418c9e3

linux上面搭建npm,参考文档https://my.oschina.net/Lady/blog/1555895

 

 

posted @ 2018-06-28 09:59  地铁程序员  阅读(393)  评论(0编辑  收藏  举报