Leanote 服务器搭建之二

来源:互联网 发布:模拟电子琴单片机程序 编辑:程序博客网 时间:2024/04/27 14:17

Leanote 完整服务器搭建

前期准备

一个可以解析的域名(预先检测下是否被墙,不然都是无功)
一台已经在服务器本地搭建了 Leanote 的服务器(可见Leanote 服务器搭建之一)
为mongodb数据库添加用户
  像mysql一样有root用户, mongodb初始是没有用户的, 这样很不安全, 所以要为leanote数据库新建一个用户来连接leanote数据库(注意, 并不是为leanote的表users里新建用户, 而是新建一个连接leanote数据库的用户, 类似mysql的root用户).
由于需要用到多个shell窗口,需要安装工具:

apt-get install screen

新建窗口用于运行mongodb:

screen -S mongodb

运行数据库:

mongod --dbpath /root/data

创建新的shell,并进入 mongodb 交互:

Ctrl + A + D # 暂离当前shellscreen -S leanote # 创建名为leanote的shell窗口mongo # 进入交互界面use leanote # 切换到leanote数据库下

添加一个用户root, 密码是abc123,自己创建

db.createUser({user: 'root',pwd: '123',roles: [{role: 'dbOwner', db: 'leanote'}]});

测试下是否正确

db.auth("root", "abc123");

返回1表示正确
用户添加好后重新运行下 mongodb , 并开启权限验证. 在mongod的终端按ctrl+c即可退出 mongodb.

screen -r mongodb # 切换到 mongodb 的shell窗口

按ctrl+c 关闭
启动mongodb, 开启验证:

mongod --dbpath /root/data --auth

还要修改配置文件 : 修改 leanote/conf/app.conf:

db.host=localhostdb.port=27017db.dbname=leanote # requireddb.username=root # if not exists, please leave blankdb.password=abc123 # if not exists, please leave blank

绑定域名
比如想绑定域名a.com到你运行Leanote服务器, 你需要将leanote以80端口运行, 请修改conf/app.conf的如下配置:

http.port=80site.url=http://a.com

然后启动Leanote。 当然你还需要将a.com绑定(解析)ip到Leanote服务器,可以在域名注册的地方进行解析。
如果服务器上已有其它程序运行了80端口, 怎么办呢? 请google或百度下 “使用nginx分发请求到不同端口”.

测试运行

screen -S mongodbmongod --dbpath /root/data --authCtrl + A + Dscreen -S leanotecd /root/leanote/bin/bash run.shCtrl + A + D

至此,可以在网页输入a.com进行访问你的leanote了。不过,为了完全起见,之后的步骤最好也试做一下。

为Leanote指定超级管理员帐户(admin用户)
Leanote默认超级管理员为admin,且一旦不小心修改了username则不能改回. 此时可修改配置文件app.conf,比如指定用户life为超级管理员, 修改或/添加一行:

adminUsername=life

超级管理员可以在登陆后进行后台管理。

为Leanote配置https(还未成功,有时间再试)
生成SSL证书
可以在网上买一个, 或者自己做一个. 这里有一个shell脚本可以自动生成证书: (新建cert.sh,内容如下)

#!/bin/sh# create self-signed server certificate:read -p "Enter your domain [www.example.com]: " DOMAINecho "Create server key..."openssl genrsa -des3 -out $DOMAIN.key 1024echo "Create server certificate signing request..."SUBJECT="/C=US/ST=Mars/L=iTranswarp/O=iTranswarp/OU=iTranswarp/CN=$DOMAIN"openssl req -new -subj $SUBJECT -key $DOMAIN.key -out $DOMAIN.csrecho "Remove password..."mv $DOMAIN.key $DOMAIN.origin.keyopenssl rsa -in $DOMAIN.origin.key -out $DOMAIN.keyecho "Sign SSL certificate..."openssl x509 -req -days 3650 -in $DOMAIN.csr -signkey $DOMAIN.key -out $DOMAIN.crt
bash cert.sh # 运行

假设得到了两个文件: a.com.crt, a.com.key

配置Nginx
假设Leanote运行的端口是9000, 域名为a.com, 那么nginx.conf可以配置如下:

# 本配置只有http部分, 不全http {    include /etc/nginx/mime.types;    default_type application/octet-stream;    upstream a.com {        server localhost:9000;    }    # http    server    {        listen 80;        server_name a.com;        # 强制https        # 如果不需要, 请注释这一行rewrite        rewrite ^/(.*) https://jp_linode2.com/$1 permanent;        location / {            proxy_pass http://a.com;            proxy_set_header Host $host;            proxy_set_header X-Real-IP $remote_addr;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        }    }    # https    server    {        listen 443 ssl;        server_name a.com;        ssl_certificate /root/a.com.crt; # 修改路径, 到a.com.crt, 下同        ssl_certificate_key /root/a.com.key;        location / {            proxy_pass http://a.com;            proxy_set_header Host $host;            proxy_set_header X-Real-IP $remote_addr;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        }    }}