CentOS Linux release 7.6.1810 (Core)node
軟件:python2.7及如下 不建議3.0以上版本python
nodejs
安裝配置node及npmlinux
從官網下載https://nodejs.org/en/download/current/ 下載最新二進制壓縮包node-v12.2.0-linux-x64.tar.xz nginx
[root@server9 ~]# tar zvJf node-v12.2.0-linux-x64.tar.xz #解壓並更名node放到指定的目錄git
[root@server9 ~]# vim /etc/profile #添加環境變量github
PATH=$PATH:/shujuchu/data/node/binweb
[root@server9 ~]# source /etc/profilenpm
[root@server9 ~]# node -v #能夠查看版本即沒有問題json
v12.2.0vim
[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以下:
#
#
#
storage: /home/{user}/.local/share/sinopia/storage # 庫存路徑,須要考慮磁盤空間
web: # 自定義web項,即瀏覽器訪問頁面
#
true
if you want to experiment with web ui now;#enable: true
title: Sinopia
auth:
htpasswd:
file: ./htpasswd # 添加用戶(npm adduser)後自動建立,保存用戶信息,能夠初始化用戶
# You can set this to -1 to disable registration. #max_users: 1000 # 設置爲-1不能npm adduser
uplinks: # 能夠配置多個上游地址,後面packages中的proxy指定用哪一個
npmjs:
url: https://registry.npm.taobao.org/ # 更改此上游地址
# 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屬性進行匹配
access: $all publish: $authenticated
'*':
# 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
logs:
listen: 0.0.0.0:4873 # 設置監聽地址,0.0.0.0匹配本機地址.這裏咱們設爲172.25.135.9:4873
#url_prefix: https://dev.company.local/sinopia/
#https:
#http_proxy: http://something.local/ # 設置代理服務器
#https_proxy: https://something.local/
#no_proxy: localhost,127.0.0.1
#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中,下次安裝直接從本地獲取