分享我的发现、想法与心得

0%

ubuntu 部署手札

适用于ubuntu 16.04

命令行

进程

1
2
#表示查看所有进程里CMD是java的进程信息, -aux 显示所有状态, grep 是搜索
ps -aux | grep java

解决运行shell存在\r问题,win下编辑的文档会包含\r
实际上它安装了两个工具:todos(相当于unix2dos),和fromdos(相当于dos2unix)
安装完即可,现在你已经可以进行文本格式的转换了。
1
sudo apt-get install tofrodos

安装mongo

1
2
3
4
5
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
#Ubuntu 16.04
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
sudo apt-get update
sudo apt-get install -y mongodb-org

添加服务
(Ubuntu 16.04-only) Create systemd service file
Create a new file at /lib/systemd/system/mongod.service with the following contents:

1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

让mongo可以远程连接

查看ip使用情况

1
$ sudo netstat -nputl

修改 mongodb.conf
1
$ vi /etc/mongodb.conf

将以下配置

1
2
bind_ip: 127.0.0.1
port: 27017

改为

1
2
3
bind_ip: 0.0.0.0
port: 27017
auth: true

重启mongo

1
2
$ sudo service mongodb restart
$ sudo service mongodb status

MongoDB的启动和关闭
https://docs.mongodb.com/master/tutorial/install-mongodb-on-ubuntu/?_ga=1.50307174.1164144610.1470663341

添加管理权限

by mongoDB 3.0
添加管理员

1
2
3
4
5
6
7
8
9
$ mongo
> use admin
> db.createUser(
{
user: "buru",
pwd: "12345678",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

查看用户

1
> show users

修改服务,添加上--auth参数让mongo启动用权限管理

1
2
# /lib/systemd/system/mongod.service
ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf

重启mongo

1
2
$ sudo service mongodb restart
$ sudo service mongodb status

http://ibruce.info/2015/03/03/mongodb3-auth/

安装nginx

创建文件/etc/apt/sources.list.d/nginx.list并键入

1
2
deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx

更新并安装nginx
1
2
sudo apt-get update
sudo apt-get install nginx

如果提示W: GPG error: http://nginx.org/packages/ubuntu xenial Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY $key
需要运行以下代码($key是错误提示中有提及的)
1
2
3
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $key
sudo apt-get update
sudo apt-get install nginx

nodeJS前面搭建一层nginx的场景和必要性

当80端口已经给nginx占用,而已服务器存在多个网站和服务时还是有必要用nginx的

编辑nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# vi /etc/nginx/nginx.conf

http {
include mime.types;
default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;


server {
listen 80;
server_name uxfeel.com www.uxfeel.com;

location / {
proxy_pass http://localhost:8080;
}
}

}

查看nginx状态

1
$ systemctl status nginx.service

启动nginx
1
$ systemctl start nginx.service

反向代理websockets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
http {

// ...省略

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

server {

listen 3102; #监听3102
server_name localhost;

location / {
proxy_pass http://localhost:3100; #代理3100

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}

// ...省略
}

Nginx反向代理Websocket
nginx配置location总结及rewrite规则写法
nginx proxy_pass 后面的url 加与不加/的区别
绝对详细!Nginx基本配置、性能优化指南
nodejs博客的nginx配置
(转)Nginx反向代理设置 从80端口转向其他端口
Nginx配置upstream实现负载均衡

搭建hexo博客

hexo 服务器
How do I configure pm2 to run hexo?

遭遇的问题’s

npm 安装的全局命令无法使用问题

原因就是node的路径没有加入到系统变量

1
2
3
4
5
6
7
//得到node路径
$ npm config get prefix
$ vim /etc/profile
//在文档最后,添加:
export PATH="/opt/STM/STLinux-2.3/devkit/sh4/bin:$PATH"
//保存,退出,然后运行:
$ source /etc/profile

http://www.520bu.com/index.php/archives/32/

mongod使用service启动不能的问题

sudo service mongod start不能有效生效,问题可能是文件夹/var/log/mongodb/var/lib/mongodb没设置mongodb:mongodb用户组权限
设置了估计就可以

1
2
3
4
cd /var/log/mongodb
sudo chown -R mongodb:mongodb .
cd /var/lib/mongodb
sudo chown -R mongodb:mongodb .

http://stackoverflow.com/questions/12232166/mongodb-on-ubuntu-wont-start-as-a-service-nothing-in-the-log