简体   繁体   English

在自己的服务器(Debian)和Nginx上部署我的MEAN应用程序?

[英]Deploy my MEAN app on own server (Debian) and nginx?

I've created my first app with MEAN stack (Mongo, Express, Angular 2/4, Node) but it only works on "local enviorment" I am starting client (front end) part by ng serve and it's works on localhost:4200 我用MEAN堆栈(Mongo,Express,Angular 2/4,Node)创建了我的第一个应用程序,但它仅适用于“本地环境”,我通过ng serve来启动客户端(前端),并且适用于localhost:4200

Also I am starting server part by node server.js and it works on localhost:4000 我也通过节点server.js启动服务器,它可以在localhost:4000上运行

Also starting mongodb. 同时启动mongodb。

All works perfect but on localhost. 一切正常,但在本地主机上。

How can I deploy app to production on my own server I don't want any hosting like heroku etc. 我该如何在自己的服务器上将应用程序部署到生产环境中,我不希望任何主机,例如heroku等。

I have installed debian + mongo and node. 我已经安装了debian + mongo和node。

This is my server.js file 这是我的server.js文件

require('rootpath')();
var express = require('express');
var app = express();
var cors = require('cors');
var bodyParser = require('body-parser');
var expressJwt = require('express-jwt');
var config = require('config.json');

app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// use JWT auth to secure the api
app.use(expressJwt({ secret: config.secret }).unless({ path: ['/users/authenticate', '/users/register'] }));

// routes
app.use('/users', require('./controllers/users.controller'));

// start server
var port = process.env.NODE_ENV === 'production' ? 80 : 4000;
var server = app.listen(port, function () {
    console.log('Server listening on port ' + port);
});

config.json config.json

{
    "connectionString": "mongodb://localhost:27017/mymean",
    "apiUrl": "http://localhost:4000",
    "secret": "TOP SECRET"
}

How can I start it on my own machine? 我如何在自己的机器上启动它?

As for the front-end, if you still want any dev-features you can use 至于前端,如果您仍然想要任何开发功能,则可以使用

ng serve --host=somedomain/ip --port=80

to bind to whatever ip/port you want (0.0.0.0 should also work). 绑定到您想要的任何IP /端口(0.0.0.0也应该起作用)。 If you just want the files then you should use use ng build or ng build --environment="production" . 如果只需要文件,则应使用ng buildng build --environment="production" (The latter triggers some extra optimizations from webpack.) Building your app will generate a dist -folder (by default anyway) and the contents are simple html/javascript files, so you need to serve that folder by some other means (nginx perhaps). (后者触发了webpack的一些额外优化。)构建您的应用程序将生成dist -folder(无论如何默认情况下),并且内容是简单的html / javascript文件,因此您需要通过其他方式(例如nginx)来提供该文件夹。

I use nginx to make my api (server-side) publicly available as well. 我也使用nginx使我的api(服务器端)也可以公开使用。 A nginx config can be as simple as: Nginx配置可以很简单:

server { 
    listen 80;

    # Web
    root /var/www/myapp/dist
    location / {
        try_files $uri /index.html;
    }

    # Api
    location /api {
        proxy_pass http://127.0.0.1:4000;
    }
}

Also, many uses something to keep the server process alive. 而且,许多人使用某些东西来保持服务器进程的生命。 Instead of just NODE_ENV=production node server.js , there are tools like nodemon , forever or pm2 . 而不是仅仅NODE_ENV=production node server.js ,有喜欢的工具nodemon永远PM2 I'm biased towards pm2, but it's all very subjective. 我偏向于pm2,但这都是非常主观的。 Those process managers are very handy if you run your server on some VPS. 如果您在某些VPS上运行服务器,这些流程管理器将非常方便。 Probably less so if you run it on your own machine. 如果您在自己的计算机上运行它,可能会更少。 ( node server.js is fine too). node server.js也可以)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM