CentOS Linux release 7.6.1810 (Core)
软件:python2.7及以下 不建议3.0以上版本
nodejs
安装配置node及npm
从官网下载https://nodejs.org/en/download/current/ 下载最新二进制压缩包node-v12.2.0-linux-x64.tar.xz
[root@server9 ~]# tar zvJf node-v12.2.0-linux-x64.tar.xz #解压并改名node放到指定的目录
[root@server9 ~]# vim /etc/profile #添加环境变量
PATH=$PATH:/shujuchu/data/node/bin
[root@server9 ~]# source /etc/profile
[root@server9 ~]# node -v #可以查看版本即没有问题
v12.2.0
[root@server9 ~]# npm -v
6.9.0
[root@server9 ~]# npm set registry https://registry.npm.taobao.org/ #添加上游淘宝源
[root@server9 ~]# npm install -g sinopia
[root@server9 ~]# sinopia
Sinopia doesn't need superuser privileges. Don't run it under root.
warn --- config file - /root/.config/sinopia/config.yaml
warn --- http address - http://localhost:4873/
[root@server9 ~]# vim .config/sinopia/config.yaml
配置文件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: /home/{user}/.local/share/sinopia/storage # 库存路径,需要考虑磁盘空间
web: # 自定义web项,即浏览器访问页面
web interface is disabled by default in 0.x, will be enabled soon in 1.x
when all its issues will be fixed
#
set this to true
if you want to experiment with web ui now;
this has a lot of issues, e.g. no auth yet, so use at your own risk
#enable: true
title: Sinopia
logo: logo.png
template: custom.hbs
auth:
htpasswd:
file: ./htpasswd # 添加用户(npm adduser)后自动创建,保存用户信息,可以初始化用户
Maximum amount of users allowed to register, defaults to "+inf".
# You can set this to -1 to disable registration.
#max_users: 1000 # 设置为-1不能npm adduser
a list of other known repositories we can talk to
uplinks: # 可以配置多个上游地址,后面packages中的proxy指定用哪个
npmjs:
url: https://registry.npm.taobao.org/ # 更改此上游地址
amount of time to wait for repository to respond
# before giving up and use the local cached copy
#timeout: 30s # 请求上游地址超时时间
# maximum time in which data is considered up to date
#
# default is 2 minutes, so server won't request the same data from
# uplink if a similar request was made less than 2 minutes ago
#maxage: 2m # 包过期时间
# if two subsequent requests fail, no further requests will be sent to
# this uplink for five minutes
#max_fails: 2 # 容许依赖请求最大失败数
#fail_timeout: 5m # 依赖请求超时时间
packages: # 包的权限管理,$all为所有人,$authenticated为通过验证人
分布和安装两种权限,值可以特指某几人
'@/': # 跟package.json中的name属性进行匹配
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}
listen: 0.0.0.0:4873 # 设置监听地址,0.0.0.0匹配本机地址.这里我们设为172.25.135.9:4873
if you use nginx with custom path, use this to override links
#url_prefix: https://dev.company.local/sinopia/
Configure HTTPS, it is required if you use "https" protocol above.
#https:
key: path/to/server.key
cert: path/to/server.crt
you can specify proxy used with all requests in wget-like manner here
(or set up ENV variables with the same name)
#http_proxy: http://something.local/ # 设置代理服务器
#https_proxy: https://something.local/
#no_proxy: localhost,127.0.0.1
maximum size of uploaded json document
increase it if you have "request entity too large" errors
#max_body_size: 1mb # http请求body大小
用pm2托管sinopia
上面方式启动sinopia只是暂时的,退出命令行就没有了,因此需要一个长期开启sinopia方案,通过pm2托管,可以让sinopia进程永远活着,就算意外挂了也可自动重启。
安装pm2
[root@server9 ~]# npm install -g pm2
安装完后,使用pm2启动sinopia
[root@server9 ~]# pm2 start sinopia
pm2提供开机自启动功能
[181@server9 ~]$ pm2 startup centos
#npm 启动不建议用root用户,这里我们切到自定义用户然后运行会生成这个:
sudo env PATH=$PATH:/shujuchu/data/node/bin /shujuchu/data/node/lib/node_modules/pm2/bin/pm2 startup centos -u 181 --hp /home/181
复制运行
最后保存配置信息
[
181@server9 ~]$ pm2 save
到此为止,居于sinopia的npm私有库搭建完成了,当下次启动linux时,npm私有库已经自动运行了
客户端操作
npm set registry http://{服务器ip}:4873/ #切换到私有npm源
nrm是个方便的npm源管理工具,来做快速registry切换,可选
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/
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/
创建npm用户
在发布包之前,一般需要创建用户
$ npm adduser # 注册,根据提示创建完成
$ npm login
发布
简单创建一个npm包
$ mkdir publishtest
$ cd publishtest
$ npm init
$ vi index.js
$ ls
index.js package.json
$ npm publish
若要取消发布,执行
$ npm unpublish
升级
要升级包,只需更改package.json的version属性,version要比原来的大才行
安装
安装会先找本地有无该包,没有的话从上游地址下载,然后保存到storage中,下次安装直接从本地获取