關於 Meteor.js
[Meteor.js] (http://meteor.com)是JavaScript的一个框架,允许网络开发者一次性地写出JavaScript代码并重新使用它同时使用客户端和服务器侧. 这可以归功于Meteor独特的构建过程(阅读更多关于[构建您的应用程序代码和代码共享] (http://docs.meteor.com/# structuringyourapp)). 这也解决了在_development mode_(开发者代码和调试模式)和_出品模式_(生产模式)之间需要一个复杂的部署过程的问题,后者对于该应用的公制版本足够安全. Meteor框架为客户端编码和服务器编码以及开发和制作提供了一种密切关联的方式. 这也许是客户端开发者开始使用服务器端代码的简单方法!
要在行动中看到这一点,你可能想在Meteor的网站上查看介绍 视频。
Meteor.js 允许您开发网站(Web 应用程序),基于 HTML5 的 Web 浏览器应用程序(使用 AppCache)或移动应用程序(通过与 PhoneGap 集成)等项目。您所需的只是 Javascript 和 HTML 知识。 Meteor 包括对 MongoDB (NoSQL 数据库)的支持。 Atmosphere托管包,可为您的应用程序提供完整的构建块,以进一步加速开发。
在本教程结束时,我们将有:
- 安装了 Meteor.js * 创建了一个部署包,包含了一个完整的 Meteor 应用程序的生产准备的格式(减一个网页服务器和数据库后端) * 安装了 Nginx作为我们的网页服务器传输HTTP请求到Meteor * 安装了MongoDB作为我们的数据库引擎 * 管理了我们的应用程序与 Upstart * 配置了每日数据库备份的 Meteor 数据库
在本教程中,如果您还没有自己的 Meteor 应用程序,您可以从 Meteor 网站使用Todo List
示例应用程序。
在你开始之前
你应该有:
- 联合国 单独的开发计算机上已有的Meteor app( 您可以查看) app [这里] ([ LINK0] ); 在教程后面提供指令 。
- 新的 Ubuntu 14.04** 服务器;现有气象装置在大多数情况下应起作用 *root 访问服务器以执行命令
- 更新的软件包清单。 执行 :
. ` , ` 获取更新
.,
- 替换待办事宜。 网络与您实际使用的域名( 如果您没有域名, 将会使用 IP 地址)
- 将待办事宜(不含.net)替换为应用程序的名称
第1步:设置 Nginx Web 服务器
我们将安装和设置 Nginx,因为它允许我们使用SSL加密网络流量,这是Meteor内置的Web服务器不提供的功能。
在我们的配置中,我们将用SSL证书保护我们的网站,并将所有流量从HTTP转移到HTTPS,我们还将使用一些新的安全实践来增强SSL连接的安全性。
为了安装 Nginx,我们执行:
1apt-get install nginx
在/etc/nginx/sites-available
中创建虚拟主机配置文件。
下面是一个注释的配置文件,我们可以创建为 /etc/nginx/sites-available/todos
包含以下内容。
1server_tokens off; # for security-by-obscurity: stop displaying nginx version
2
3# this section is needed to proxy web-socket connections
4map $http_upgrade $connection_upgrade {
5 default upgrade;
6 '' close;
7}
8
9# HTTP
10server {
11 listen 80 default_server; # if this is not a default server, remove "default_server"
12 listen [::]:80 default_server ipv6only=on;
13
14 root /usr/share/nginx/html; # root is irrelevant
15 index index.html index.htm; # this is also irrelevant
16
17 server_name todos.net; # the domain on which we want to host the application. Since we set "default_server" previously, nginx will answer all hosts anyway.
18
19 # redirect non-SSL to SSL
20 location / {
21 rewrite ^ https://$server_name$request_uri? permanent;
22 }
23}
24
25# HTTPS server
26server {
27 listen 443 ssl spdy; # we enable SPDY here
28 server_name todos.net; # this domain must match Common Name (CN) in the SSL certificate
29
30 root html; # irrelevant
31 index index.html; # irrelevant
32
33 ssl_certificate /etc/nginx/ssl/todos.pem; # full path to SSL certificate and CA certificate concatenated together
34 ssl_certificate_key /etc/nginx/ssl/todos.key; # full path to SSL key
35
36 # performance enhancement for SSL
37 ssl_stapling on;
38 ssl_session_cache shared:SSL:10m;
39 ssl_session_timeout 5m;
40
41 # safety enhancement to SSL: make sure we actually use a safe cipher
42 ssl_prefer_server_ciphers on;
43 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
44 ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:RC4-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK';
45
46 # config to enable HSTS(HTTP Strict Transport Security) https://developer.mozilla.org/en-US/docs/Security/HTTP_Strict_Transport_Security
47 # to avoid ssl stripping https://en.wikipedia.org/wiki/SSL_stripping#SSL_stripping
48 add_header Strict-Transport-Security "max-age=31536000;";
49
50 # If your application is not compatible with IE <= 10, this will redirect visitors to a page advising a browser update
51 # This works because IE 11 does not present itself as MSIE anymore
52 if ($http_user_agent ~ "MSIE" ) {
53 return 303 https://browser-update.org/update.html;
54 }
55
56 # pass all requests to Meteor
57 location / {
58 proxy_pass http://127.0.0.1:8080;
59 proxy_http_version 1.1;
60 proxy_set_header Upgrade $http_upgrade; # allow websockets
61 proxy_set_header Connection $connection_upgrade;
62 proxy_set_header X-Forwarded-For $remote_addr; # preserve client IP
63
64 # this setting allows the browser to cache the application in a way compatible with Meteor
65 # on every applicaiton update the name of CSS and JS file is different, so they can be cache infinitely (here: 30 days)
66 # the root path (/) MUST NOT be cached
67 if ($uri != '/') {
68 expires 30d;
69 }
70 }
71}
如果您想将配置文件适应您的需求,并获得更多解释,请查看本教程在 Nginx 虚拟主机。
正如在虚拟主机配置文件中所看到的, Nginx 将预期在 `/etc/nginx/ssl 中有有效的 SSL 证书和密钥。
1mkdir /etc/nginx/ssl
2chmod 0700 /etc/nginx/ssl
然后,我们可以创建包含证书(以及必要时链证书)和密钥的文件,在我们在上面的配置中定义的位置:
- 证书:
/etc/nginx/ssl/todos.pem
* 键:/etc/nginx/ssl/todos.key
如果您还没有SSL 证书和密钥, 您现在应该使用此证书创建自签名的证书 [关于为 Nginx 创建自签名的 SSL 证书] (https://andsky.com/tech/tutorials/how-to-create-a-ssl-certificate-on-nginx-for-ubuntu-12-04) 。 记住,您会希望使用配置文件中的同名名称,比如todos.key 作为关键名称,以及** todos. pem** 作为证书名称. 自签证书可作检验罚款,但建议使用商业,签名证书作生产用. 自签名的证书会触发连接到 ssl_stapling 的 Nginx 警告, 并在网页浏览器中发出安全警告 .
当您完成创建或获取证书时,请确保您有上面提到的todos.pem
和todos.key
文件。
接下来,我们应该禁用 default vhost:
1rm /etc/nginx/sites-enabled/default
并启用我们的 Meteor vhost:
1ln -s /etc/nginx/sites-available/todos /etc/nginx/sites-enabled/todos
测试 vhost 配置是否没有错误(如果您有自签证书,您将看到与 ssl_stapling 相关的错误;这是正常的):
1nginx -t
如果一切看起来都很好,我们可以将更改应用到 Nginx:
1nginx -s reload
在此时,您可以使用您的网页浏览器访问 https://todos.net(或您的IP地址)。它将向我们显示 502 Bad Gateway . 这是好的,因为我们还没有Meteor运行!
步骤二:设置MongoDB数据库
我们将从常规的 Ubuntu 存储库安装 MongoDB. 标准配置应该是好的. 无需进行身份验证才能连接到数据库,但只能从 localhost 进行连接. 这意味着没有外部连接是可能的,因此数据库是安全的,只要我们没有不受信任的 SSH 用户访问系统。
安装 MongoDB 服务器包:
1apt-get install mongodb-server
为了确保从外部主机访问是不可能的,我们执行以下操作,以确保 MongoDB 绑定到 127.0.0.1 。
1netstat -ln | grep -E '27017|28017'
预期产量:
1tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN
2tcp 0 0 127.0.0.1:28017 0.0.0.0:* LISTEN
3unix 2 [ ACC ] STREAM LISTENING 6091441 /tmp/mongodb-27017.sock
为了在发生错误的情况下每天备份,我们可以 可选 安装一个简单的命令作为每日 cron 工作。
1@daily root mkdir -p /var/backups/mongodb; mongodump --db todos --out /var/backups/mongodb/$(date +'\%Y-\%m-\%d')
步骤3 — 安装 Meteor 应用程序
首先,我们需要安装 Node.js. 因为Meteor 通常需要比标准存储库中可用的版本更新的 Node.js,所以我们会使用自定义 PPA (在写作时,Ubuntu 14.04 提供 nodejs=0.10.25~dfsg2-2ubuntu1,而 Meteor 0.8.3 需要 Node.js 0.10.29 或更高版本)。
执行以下操作以在 Node.js 中添加 PPA,并通过按 Enter 来确认:
1add-apt-repository ppa:chris-lea/node.js
输出:
1Evented I/O for V8 javascript. Node's goal is to provide an easy way to build scalable network programs
2 More info: https://launchpad.net/~chris-lea/+archive/ubuntu/node.js
3Press [ENTER] to continue or ctrl-c to cancel adding it
4
5gpg: keyring `/tmp/tmphsbizg3u/secring.gpg' created
6gpg: keyring `/tmp/tmphsbizg3u/pubring.gpg' created
7gpg: requesting key C7917B12 from hkp server keyserver.ubuntu.com
8gpg: /tmp/tmphsbizg3u/trustdb.gpg: trustdb created
9gpg: key C7917B12: public key "Launchpad chrislea" imported
10gpg: Total number processed: 1
11gpg: imported: 1 (RSA: 1)
12OK
现在我们必须更新存储库缓存,然后我们可以安装 Node.js 和 npm (Node.js 包管理器):
1apt-get update
2apt-get install nodejs
运行我们的 Meteor 应用程序作为常规用户是很好的做法,因此,我们将为此目的创建一个新的系统用户:
1adduser --disabled-login todos
输出:
1Adding user `todos' ...
2Adding new group `todos' (1001) ...
3Adding new user `todos' (1001) with group `todos' ...
4Creating home directory `/home/todos' ...
5Copying files from `/etc/skel' ...
6Changing the user information for todos
7Enter the new value, or press ENTER for the default
8 Full Name []:
9 Room Number []:
10 Work Phone []:
11 Home Phone []:
12 Other []:
13Is the information correct? [Y/n]
第四步 - 配置升级
现在我们已经准备好创建 Upstart 服务来管理我们的 Meteor 应用程序 Upstart 将自动启动应用程序,并在死亡的情况下重新启动 Meteor。您可以在 [本教程] 中阅读有关创建 Upstart 服务文件的更多信息(https://andsky.com/tech/tutorials/the-upstart-event-system-what-it-is-and-how-to-use-it)。
创建文件 /etc/init/todos.conf
. 再次,它被注明在线:
1# upstart service file at /etc/init/todos.conf
2description "Meteor.js (NodeJS) application"
3author "Daniel Speichert <[email protected]>"
4
5# When to start the service
6start on started mongodb and runlevel [2345]
7
8# When to stop the service
9stop on shutdown
10
11# Automatically restart process if crashed
12respawn
13respawn limit 10 5
14
15# we don't use buil-in log because we use a script below
16# console log
17
18# drop root proviliges and switch to mymetorapp user
19setuid todos
20setgid todos
21
22script
23 export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
24 export NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
25 # set to home directory of the user Meteor will be running as
26 export PWD=/home/todos
27 export HOME=/home/todos
28 # leave as 127.0.0.1 for security
29 export BIND_IP=127.0.0.1
30 # the port nginx is proxying requests to
31 export PORT=8080
32 # this allows Meteor to figure out correct IP address of visitors
33 export HTTP_FORWARDED_COUNT=1
34 # MongoDB connection string using todos as database name
35 export MONGO_URL=mongodb://localhost:27017/todos
36 # The domain name as configured previously as server_name in nginx
37 export ROOT_URL=https://todos.net
38 # optional JSON config - the contents of file specified by passing "--settings" parameter to meteor command in development mode
39 export METEOR_SETTINGS='{ "somesetting": "someval", "public": { "othersetting": "anothervalue" } }'
40 # this is optional: http://docs.meteor.com/#email
41 # commented out will default to no email being sent
42 # you must register with MailGun to have a username and password there
43 # export MAIL_URL=smtp://[email protected]:[email protected]
44 # alternatively install "apt-get install default-mta" and uncomment:
45 # export MAIL_URL=smtp://localhost
46 exec node /home/todos/bundle/main.js >> /home/todos/todos.log
47end script
如果你在启动Meteor的开发模式时使用meteor --settings config.json
,那么你应该将config.json
的内容粘贴为变量在METEOR_SETTINGS
。
MAIL_URL
必须是有效的SMTP URL **仅如果您打算使用Meteor的电子邮件包。
从文件中可以看出,日志将被保存到`/home/todos/todos.log'。 此文件不会在一段时间内旋转并 WILL GROW 。 善能观察. 理想的情况是,其中不应有很多内容(错误)。 可选的是,您可以在 Upstart 脚本的结尾设置 [log room] (https://andsky.com/tech/tutorials/how-to-configure-logging-and-log-rotation-in-nginx-on-an-ubuntu-vps) 或将 QQ 替换为 QQ,以覆盖整个文件,而不是附加到它的结尾.
请不要启动此服务,因为我们还没有实际的 Meteor 应用程序文件!
步骤五:部署 Meteor 应用程序
可选:如果您还没有 Meteor 项目
如果您还没有Meteor项目,并且想使用演示应用程序,那就没有问题了!
在您的家庭计算机或开发 Linux 服务器上执行下一步。 命令可能因您的操作系统而异。
1cd ~
首先,安装 Meteor 的开发版本:
1curl https://install.meteor.com | /bin/sh
然后从一个示例中创建一个应用程序,称为 Todo List:
1meteor create --example todos
现在输入你的应用程序目录,你准备继续:
1cd todos
所有气象项目
现在是时候从我们的Meteor应用程序创建一个生产版本包了,以下命令应该在您的家庭计算机或开发 Linux服务器上执行,无论您的Meteor应用程序在哪里。
1cd /app/dir
并执行:
1meteor build .
这将创建一个档案文件,如 todos.tar.gz
在目录 /app/dir
. 将此文件复制到您的 Droplet 的 ~
目录。
1scp todos.tar.gz [email protected]:~
现在回到您的 Droplet. 创建一个项目目录并将档案项目文件移动到其中. 请注意,这是我们之前创建的项目用户的主文件夹,而不是您的 root 主文件夹:
1mkdir /home/todos
2mv todos.tar.gz /home/todos
进入项目目录并卸载它:
1cd /home/todos
2tar -zxf todos.tar.gz
让我们来看看项目 README:
1cat /home/todos/bundle/README
包包含一个包含内容的README
文件:
1This is a Meteor application bundle. It has only one external dependency:
2Node.js 0.10.29 or newer. To run the application:
3
4 $ (cd programs/server && npm install)
5 $ export MONGO_URL='mongodb://user:password@host:port/databasename'
6 $ export ROOT_URL='http://example.com'
7 $ export MAIL_URL='smtp://user:password@mailhost:port/'
8 $ node main.js
9
10Use the PORT environment variable to set the port where the
11application will listen. The default is 80, but that will require
12root on most systems.
13
14Find out more about Meteor at meteor.com.
这个食谱反映在我们的 /etc/init/todos.conf
文件中.在 README 中还有一个我们需要做的事情。
现在我们需要安装一些必要的npm模块,才能构建其中的一些模块,我们还需要安装g++并制作:
1apt-get install g++ make
2cd /home/todos/bundle/programs/server
3npm install
你应该看到这样的输出:
1npm WARN package.json [email protected] No description
2npm WARN package.json [email protected] No repository field.
3npm WARN package.json [email protected] No README data
4
5> [email protected] install /home/todos/bundle/programs/server/node_modules/fibers
6> node ./build.js
7
8`linux-x64-v8-3.14` exists; testing
9Binary is fine; exiting
10[email protected] node_modules/underscore
11
12[email protected] node_modules/semver
13
14[email protected] node_modules/source-map-support
15└── [email protected] ([email protected])
16
17[email protected] node_modules/fibers
我们需要这样做的原因是,我们的应用程序包不包含基于平台的模块和库。
我們幾乎已經準備好執行該應用程式,但因為我們在根檔案上運作,而且這些檔案應該由「todos」用戶擁有,我們需要更新項目目錄的所有權:
1chown todos:todos /home/todos -R
第六步 - Showtime
在此时刻,我们拥有运行我们的 Meteor 应用程序所需的一切:
- Node.js 环境安装 * 应用程序安装在其项目目录中 * Upstart 服务配置以运行应用程序 * MongoDB 数据库 * Nginx 代理服务器在我们的 Meteor 应用程序前面提供 SSL 加密
要启动我们的应用程序,让我们从项目目录中执行这个命令:
1start todos
现在你应该能够在浏览器中查看你的应用程序在https://todos.net。
重新部署应用程序
当你对开发模式进行更改时(你会;我们毕竟是开发人员!),你可以简单地重复步骤五(从),并通过大多数步骤,直到命令重新启动 todos
,通过Upstart重新加载你的应用程序。
客户(访问您的网站的访问者)将自动引导新版本的代码,并更新他们的页面 - 这是Meteor的魔力!
如果你想测试这一点,你可以简单地更改你在家庭计算机或开发服务器上的应用程序开发副本中的todos/client/todos.html
页面的文本。
开发服务器:**
建造:
1meteor build /app/dir
加载:
1scp todos.tar.gz [email protected]:/home/todos
生产服务器:**
扩展:
1tar -zxf /home/todos/todos.tar.gz
进入项目文件夹:
1cd /home/todos/bundle/programs/server
更新 npm 模块(您可能会看到一些警告):
1npm install
重新启动 app:
1restart todos
解决问题
如果有什么不对劲,这里有一些提示在哪里寻找问题:
- 如果您的应用程序启动或死亡,请检查
/home/todos/todos.log;如果应用程序启动或死亡,请检查
/var/log/nginx/error.log’;如果您认为应用程序存在 HTTP 错误,请检查 `/var/log/mongodb/mongodb.log。
最后,检查所有服务是否正在运行:
1status todos
2service nginx status
3status mongodb