服務器端環境配置請點擊:CentOS配置Docker+NodeJS服務
Mac的一些安裝
安裝 NodeJS
https://nodejs.org/en/download/
安裝loopback-cli
npm install -g loopback-cli
安裝zsh作為默認SHELL
http://blog.csdn.net/w670328683/article/details/49782601
brew install zsh
zsh --version
chsh -s /bin/zsh
echo $SHELL
安裝oh-my-zsh美化zsh
https://github.com/robbyrussell/oh-my-zsh
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
安裝tmux分屏幕
https://github.com/gpakosz/.tmux
brew install tmux
安裝mosh
brew install mosh
安裝mongodb數據庫(只在docker使用的話不用裝)
brew update
brew install mongodb
遇到一個問題
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
這樣解決
sudo chown -R $(whoami) /usr/local
brew install mongodb
安裝redis數據(只在docker使用的話不用裝)
tar xzf redis-4.0.1.tar
cd redis-4.0.1
make
make install
安裝mongo-express(只在docker使用的話不用裝)
https://github.com/mongo-express/mongo-express
npm install -g mongo-express
安裝Vim的插件janus
The distribution also requires ack, ctags, git, ruby and rake.
https://github.com/carlhuda/janus
curl -L https://bit.ly/janus-bootstrap | bash
安裝pm2
npm install pm2 -g
npm install pm2@latest -g
安裝docker,docker-compose
brew search docker
brew update
brew install docker
brew install docker-compose
安裝httpie
brew install httpie
Mac的代碼管理:
git clone https://git.coding.net/zbsas/webstore-backend.git
git checkout next
git submodule init
git submodule update
npm install
發現子模塊代碼下不來,這樣解決:
查看公鑰私鑰
cd ~/.ssh
.pub 文件是你的公鑰,另一個則是私鑰。如果找不到這樣的文件,你可以通過運行 程序來創建它們:
ssh-keygen
然后在coding上設上公鑰,子目錄代碼可以下來了
git submodule update
上傳服務器代碼
rsync -avuzb xiaoyx root@dlin.top:~/
過濾沒有必要的文件和目錄
rsync -avuzb --exclude=.git --exclude=node_modules webstore-backend/* root@dlin.top:~/
下載服務器代碼
rsync -avuzb root@dlin.top:/root/xiaoyx/ .
scp root@dlin.top:/root/xiaoyx/docker-compose.yml .
代碼調試:
開啟redis
src/redis-server
也可以用pm2開啟redis
pm2 start src/redis-server
開啟mongodb
cd /Code-Backend/mongodb
mkdir data
mkdir log
mongod --dbpath data --logpath log/mongod.log --logappend --fork
關閉mongodb
mongo
use admin
db.shutdownServer();
pm2 看log
cd /Code-Backend/webstore-backend
pm2 start development.json
pm2 logs 1
pm2 stop all
docker調試
開啟和關閉
docker ps
docker-compose up -d
docker-compose stop
docker-compose restart
docker-compose down
重置一個docker
docker-compose down
docker ps -a
docker rm codebackend_ws_1
docker ps -a
docker image ls
docker image rm codebackend_ws
docker-compose up -d
啟動并看log
docker-compose up
看其中某一個log
docker ps
docker exec -it codebackend_ws_1 sh
pm2 list
pm2 logs 1
批處理腳本
# 關閉所有正在運行容器
docker ps | awk '{print $1}' | xargs docker stop
# 刪除所有容器應用
docker ps -a | awk '{print $1}' | xargs docker rm
# 或者
docker rm $(docker ps -a -q)
mongo-express
https://github.com/mongo-express/mongo-express
先建立用戶
docker exec -it codebackend_mongodb_1 mongo
show dbs;
use admin
管理員用戶
db.createUser({
user : "用戶名",
pwd : "密碼",
roles : [
{
role : "userAdminAnyDatabase",
db : "admin"
}
]
})
特定數據庫管理權限的用戶
use "數據庫名"
db.createUser({
user : "用戶名",
pwd : "密碼",
roles : [
{
role : "userAdmin",
db : "數據庫名"
}
]
})
一般用戶
db "數據庫名"
db.createUser({
user : "用戶名",
pwd : "密碼",
roles: [
{
role : "read", # or "readWrite"
db : "數據庫名",
}
]
})
例:創建一個數據庫用戶,對該數據具有讀寫權限
創建一個對數據庫具有讀寫權限的數據庫用戶
use dbname ;
db.createUser({user: “dbuser”, pwd: “dbuseradmin”, roles:[{role: “readWrite”, db: “dbname”}] })數據庫用戶登錄
mongo dbname -u dbuser -p dbduseradmin
cd YOUR_PATH/node_modules/mongo-express/ && node app.js
mongo-express -u user -p password -d database
mongo-express -u dlin -p dlin -d webstore-prod
遠程
mongo-express -u user -p password -d database -H mongoDBHost -P mongoDBPort
其他
mongo-express -a -u superuser -p password
mongo-express -h
Usage (Docker)
docker build -t mongo-express .
docker run -it --rm -p 8081:8081 --link YOUR_MONGODB_CONTAINER:mongo mongo-express
例子:
docker run -it --rm -p 8081:8081 --link codebackend_mongodb_1:mongo mongo-express
You can use the following environment variables to modify the container's configuration:
Name | Default | Description |
---|---|---|
ME_CONFIG_MONGODB_SERVER |
mongo or localhost
|
MongoDB host name or IP address. The default is localhost in the config file and mongo in the docker image. If it is a replica set, use a comma delimited list of the host names. |
ME_CONFIG_MONGODB_PORT |
27017 |
MongoDB port. |
ME_CONFIG_MONGODB_URL |
mongodb://admin:pass@localhost:27017/db?ssl=false |
|
ME_CONFIG_MONGODB_ENABLE_ADMIN |
false |
Enable administrator access. Send strings: "true" or "false" . |
ME_CONFIG_MONGODB_ADMINUSERNAME |
|
Administrator username. |
ME_CONFIG_MONGODB_ADMINPASSWORD |
|
Administrator password. |
ME_CONFIG_MONGODB_AUTH_DATABASE |
db |
Database name (only needed if ENABLE_ADMIN is "false" ). |
ME_CONFIG_MONGODB_AUTH_USERNAME |
admin |
Database username (only needed if ENABLE_ADMIN is "false" ). |
ME_CONFIG_MONGODB_AUTH_PASSWORD |
pass |
Database password (only needed if ENABLE_ADMIN is "false" ). |
ME_CONFIG_SITE_BASEURL |
/ |
Set the express baseUrl to ease mounting at a subdirectory. Remember to include a leading and trailing slash. |
ME_CONFIG_SITE_COOKIESECRET |
cookiesecret |
String used by cookie-parser middleware to sign cookies. |
ME_CONFIG_SITE_SESSIONSECRET |
sessionsecret |
String used to sign the session ID cookie by express-session middleware. |
ME_CONFIG_BASICAUTH_USERNAME |
admin |
mongo-express web login name. Sending an empty string will disable basic authentication. |
ME_CONFIG_BASICAUTH_PASSWORD |
pass |
mongo-express web login password. |
ME_CONFIG_REQUEST_SIZE |
100kb |
Used to configure maximum mongo update payload size. CRUD operations above this size will fail due to restrictions in body-parser. |
ME_CONFIG_OPTIONS_EDITORTHEME |
rubyblue |
Web editor color theme, more here. |
ME_CONFIG_SITE_SSL_ENABLED |
false |
Enable SSL. |
ME_CONFIG_MONGODB_SSLVALIDATE |
true |
Validate mongod server certificate against CA |
ME_CONFIG_SITE_SSL_CRT_PATH |
|
SSL certificate file. |
ME_CONFIG_SITE_SSL_KEY_PATH |
|
SSL key file. |
ME_CONFIG_SITE_GRIDFS_ENABLED |
false |
Enable gridFS to manage uploaded files. |
Example:
docker run -it --rm \
--name mongo-express \
--link web_db_1:mongo \
-p 8081:8081 \
-e ME_CONFIG_OPTIONS_EDITORTHEME="ambiance" \
-e ME_CONFIG_BASICAUTH_USERNAME="" \
mongo-express
設置dockor運行的配置文件
docker-compose.yml
mongo-express:
image: mongo-express
depends_on:
- mongodb
restart: always
links:
- mongodb:mongo
ports:
- "8081:8081"
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME = admin
- ME_CONFIG_MONGODB_ADMINPASSWORD = pass
設置登錄頁賬號密碼
https://caddyserver.com/docs/basicauth
Caddyfile
http://db.local.dlin.top {
basicauth / admin pass
proxy / mongo-express:8081 {
transparent
}
}