使用sinopia 搭建私有npm服务器的教程
简介 效果展示 使用 sinopia 的好处是,node系的工程师,内部协作时,使用自有 npm 包,会非常方便;另外,sinopia,会缓存已经下载过的包,可以在相当程度上,加速 npm install 相关命令的执行。 工作中,确实有需要用到 sinopia 来作为私有 npm 服务器的场景。原来一直在自己电脑上开启 sinopia。这样做最大的问题是,sinopia 后台一直开着,会越来越耗费资源,电脑最后会变得很卡。偶尔,还会因为忘记开启或关闭 sinopia,带来各种不便利。 今天我试着直接在树莓派上搭建一个 sinopia 服务。最终实现的效果较为完整,基本满足需要了。包含用户权限管理,外网使用自定义域名访问,sinopia服务开机自启等特性。 注意:以下 shell 命令,默认在树莓派的shell中执行,而不是在本机电脑上。 安装最新长期支持版 node 环境 树莓派自带的 node 环境是 v4.8.2,有必要升级下。 安装 nvm 建议安装 nvm,以方便管理多个版本的 node 环境。 # 安装 nvm wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install.sh | bash # 重启shell # 验证 nvm 安装 command -v nvm 使用 nvm 安装最新长期支持版 node 环境 # 安装 Node nvm install --lts #验证安装 --> v8.9.1 node -v 安装和配置 sinopia 安装 sinopia # 安装 npm install -g sinopia # 验证是否安装成功 --> 这一步会输出自动生成的配置文件路径等信息。 sinopia 解决端口 4873 占用问题 sinopia 启动时,默认使用 4873端口,可能会遇到端口冲突问题。 # 安装 lsof 命令 sudo apt-get update sudo apt-get install lsof # 查看端口占用进程 PID lsof -i :4873 # 杀死占用 4873 端口的进程。4649,要换为实际的 PID。 kill -9 4649 注册一个默认账户 为了提高安全性,我们稍后会禁用 sinopia 的用户注册功能,所以先注册一个默认的 sinopia 账户。需要在当前 shell 中执行 sinopia 命令开启服务之后,再重新打开一个 shell 执行: npm set registry http://localhost:4873/ npm adduser --registry http://localhost:4873/ 用户名,密码,邮箱等,要记牢,适当设置的复杂点。 ### 升级安装 vim 感觉树莓派自带的 vim 不太好使了,我也顺便升级了下。 ``` # 配置支持vim中鼠标右键复制 在 .vimrc 此文件中增加如下一行: shell set mouse=v 配置 sinopia 配置文件路径可以在执行 sinopia 命令时,从其输出中查看,一般应是 /home/pi/.config/sinopia/config.yaml 基于我的使用使用经验和文档说明,主要配置了以下内容: max_users: -1 :禁用注册。 *npmjs: url: https://registry.npm.taobao.org* : 设置 npm 镜像为淘宝源,一来可以加速 npm 公共包的安装,二来借助淘宝源的只读特性,避免误操作发布私有 npm 包到外网上。 access: $authenticated:禁止匿名用户访问。配置后,未登录用户看不到 sinopia 上私有包的任何信息。 max_body_size: '200mb':这样设置,会提高安装超级 npm 包的成功率,比如 react-native 。 完整配置内容如下。如果你不是在树莓派上配置,请把 /home/pi 替换为自己真实的用户路径名。 # # 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: /home/pi/.local/share/sinopia/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: -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" 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} max_body_size: '200mb' 可以在本地编辑器中修改好配置,然后直接复制到树莓派上: # 打开配置文件 vim /home/pi/.config/sinopia/config.yaml 使用粘贴命令。直接粘贴,格式会错乱。 :set paste i # 右键粘贴即可。 配置frpc 远程访问 关于 frp 的配置问题,详见:【小技巧解决大问题】使用 frp 突破阿里云主机无弹性公网 IP 不能用作 Web 服务器的限制。此处只贴出 frpc 的关键配置变更: [web-sinopia] type = http local_port = 4873 subdomain = sinopia 使用 Systemd 实现 sinopia 服务开机自启 树莓派,默认是带有 Systemd 的,直接使用即可: sudo vim /usr/lib/systemd/system/sinopia.service sinopia.service 具体内容如下,其中/home/pi/.config/sinopia/config.yaml 要替换为自己的 config.yaml 地址: [Unit] DescrIPtion=sinopia After=network.target [Service] TimeoutStartSec=30 ExecStart=/home/pi/.nvm/versions/node/v8.9.1/bin/sinopia /home/pi/.config/sinopia/config.yaml ExecStop=/bin/kill $MAINPID Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target 启动 sinopia 并设置开机启动: systemctl enable sinopia systemctl start sinopia systemctl status sinopia 其他可能有用的命令 # 禁用服务 systemctl disable sinopia # 重新启动服务 systemctl restart sinopia 在另一台电脑上使用 sinopia 私有 npm 服务器功能 假定,最终的 sinopia 服务器的外网地址是: *http://sinopia.example.com* 真正想使用,需要在终端中配置下: npm set registry http://sinopia.example.com npm adduser --registry http://sinopia.example.com npm login 配置完毕后,你可以试着发布一个私有 npm 包: # 在某个文件夹初始化一个新的 npm 包 npm init # 发布到私有 sinopia 服务器: npm publish 发布成功后,在浏览器中登录 *http://sinopia.example.com*,刷新页面,应该就能看到自己刚发布的那个包了。 注意,其他用户在使用私有库上的包时,也应该先登录,否则会报错: unregistered users are not allowed to access package 以上这篇使用sinopia 搭建私有npm服务器的教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |