前提
- git
- node version >= 8.* (推荐: 8.11.4)
使用cnpmjs搭建私服
笔者在centos进行操作
- 下载cnpmjs.org
# 拉取cnpmjs.org
git clone https://github.com/cnpm/cnpmjs.org.git
# 安装所需依赖包
cd cnpmjs.org && npm install
2.搭建数据库
2.1 搭建mysql
# 下载mysql的repo源(这里使用的版本为5.7)
wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
# 安装刚下载的rpm源
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
# 安装mysql
yum -y install mysql-server
2.2 mysql默认配置文件路径
配置文件:/etc/my.cnf 日志文件:/var/log/var/log/mysqld.log 服务启动脚本:/usr/lib/systemd/system/mysqld.service socket文件:/var/run/mysqld/mysqld.pid
2.3 配置my.cnf
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server_id = 1
expire_logs_days = 3# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
2.4 mysql服务
service mysqld start/restart/stop
2.5 重置密码
注意: 密码必须满足数字、小写字母、大写字母 、特殊字符、长度至少8位
# 获取mysql随机生成的密码
grep "password" /var/log/mysqld.log
# 登录mysql(第一次以mysql随机密码登录)
mysql -u root -p
# 修改密码
alter user '用户名(默认root)'@'Ip地址(默认localhost)' identified by '密码';
# 刷新
flush privileges;
# 退出再次登录, 测试已经修改成功
show databases;
cnpmjs.org配置文件(config/index.js)
- 启用集群模式,可选项
enableCluster: true
- 端口设置,
registryPort
为注册服务端口,webPort
为Web访问端口,默认即可
registryPort: 7001,
webPort: 7002
- 管理员设置
admins: {// name: emailfengmk2: 'fengmk2@gmail.com',admin: '改成您的邮箱即可',dead_horse: 'dead_horse@qq.com',
}
- 数据库配置
db: 'cnpmjs',
username: 'cnpmjs',
password: 'Password',
dialect: 'mysql',
host: '127.0.0.1',
port: 3306,
storage: path.join(dataDir, 'data.mysql')
- 绑定监听地址,可选项。若非本机需要访问,则添加符号//注释此行。亦可在本机架设Nginx做反向代理,则无须修改此行
bindingHost: '127.0.0.1'
- 注册主机名修改为对应IP地址或域名(笔者使用默认配置, 在客户端拉取包时会出现404问题, 这边将该配置修改为
当前主机
+registryPort的端口号 默认7001
)
registryHost: 'xxx.xxx.xxx.xxx:7001'
- 开启私有模式,只允许管理员发布包,默认公有模式允许所有用户发布包
enablePrivate: true
- 允许发布私有包必须具有的命名前缀
scopes: [ '@xxx' ]
- 同步模式,none不同步只代理源公有模块;exist只同步已存在的模块;all同步所有模块
syncModel: 'exist'
3.1 系统配置脚本,可通过创建config.js文件来覆盖默认配置(config/config.js)
// 笔者-配置文件
module.exports = {debug: false,admins: {admin: 'xxx@qq.com'},database: {db: 'cnpmjs',username: 'root',password: 'xxx',dialect: 'mysql',host: '127.0.0.1',port: 3306},bindingHost: '',registryHost: 'xxx.xxx.xxx.xxx:7001',syncModel: 'exist'
}
客户端使用
- 安装cnpm
npm install -g cnpm
- 修改cnpm的registry
cnpm config set registry=http://xxx.xxx.xxx.xxx:7001
- 添加用户, 因为只有admin用户才能发布,所以就添加admin用户
cnpm adduser# 命令行下
> Username: admin
> Password: 设置密码
> Email: (this IS public) 这个与config/config.js管理员admin一致
- cnpm登录
cnpm login# 命令行下
> Username: (admin)
> Password: (or leave unchanged)
> Email: (this IS public) (你的邮箱)
5.测试是否可以发布、拉取、撤销
5.1 vim hello.js
module.exports = function() {return 'Hello World'
}
5.2 npm init (一路回车即可)
5.3 发布
cnpm publish
5.4 拉取
cnpm unpublish
5.5 撤销
cnpm unpublish xxx@版本号