MEAN.JS技术栈提供了"grunt build"任务来准备应用程序进行生产环境部署。不幸的是,关于接下来的步骤缺乏信息。实际上,如何部署应用程序到生产环境以及如何启动它还不清楚。
问题#1 除了在config/env/production.js中进行更改之外,项目中还必须配置什么?例如,如何使用自定义字体?
问题#2 好的,代码通过Git、rsync等方式部署到生产环境。仅运行它是否足够?
$NODE_ENV=production node server.js&
MEAN.JS技术栈提供了"grunt build"任务来准备应用程序进行生产环境部署。不幸的是,关于接下来的步骤缺乏信息。实际上,如何部署应用程序到生产环境以及如何启动它还不清楚。
问题#1 除了在config/env/production.js中进行更改之外,项目中还必须配置什么?例如,如何使用自定义字体?
问题#2 好的,代码通过Git、rsync等方式部署到生产环境。仅运行它是否足够?
$NODE_ENV=production node server.js&
我建议按照以下步骤部署到生产环境:
Double check the /config/env/production.js
config file and include all assets which you added manually into /config/env/all.js
. It's a good approach to use minified versions for production.
(Optional) If you have custom fonts in your app I do recommend to update gruntfile.js and add a task which will copy fonts into /public/dist/ folder. I did the following changes:
copy: {
main:{
expand: true,
flatten: true,
src: ['public/modules/core/fonts/*'],
dest: 'public/dist/',
filter: 'isFile'
}
},
...
// Build task(s).
grunt.registerTask('build', ['lint', 'loadConfig', 'ngmin', 'uglify', 'cssmin', 'copy']);
The copy task requires to install grunt-copy module
Now it's time to make single application.js, application.min.js & application.min.css files for production (see the /public/dist
folder). Run in the app folder
$grunt build
Copy files to production server. I prefer to use GIT push deployment. It sends to server only incremental changes. If you use GIT for the push-deployment it needs to add all files from `/public/dist/' folder into the repository.
Because you use express.js in your project it's enough to use command
$NODE_ENV=production node server.js&
I know some developers use forever (module for node.js), but I prefer to use UPSTART (event-based init daemon) which available by default on Ubuntu as system service. I create a config file in /etc/init
folder, e.g. /etc/init/my-app.conf
. After this I can start, stop, restart my app as a service. E.g.
service my-app start|stop|restart
The UPSTART will monitor and restart your service in case of failure (see respawn command in the UPSTART config). You can find detailed answer how to make UPSTART config file here: upstart script for node.js
(Optional, but recommended) Install nginx
in front of Node.js
. It's recommended to run you web app under unprivileged user due to security reasons, on the other hand if you want to use port 80 (it's a default port for the http protocol) for your web app (e.g. http://my-app-domain.com/ instead of http://my-app-domain.com:3000/) such configuration may be tricky. Because of this I use Nginx (port 80) in front of my web app which actually works on the port available for unprivileged user (e.g. 3000)
6a. Instead of Nginx you can use Varnish