我有一个在 EC2 (Centos) 实例上运行的 NodeJS 服务器。我已经通过 AWS CodePipeline 设置了构建和部署管道。NodeJS 服务器设置为 systemd 服务。发布管道的部署阶段,服务器无法连接到我设置的 MongoDB。当我通过命令行使用以下命令运行服务时:
sudo systemctl restart myserver.service
服务器连接到数据库。我无法弄清楚为什么它在我手动运行时有效,但在通过 CodeDeploy 部署后无效。以下是相关文件的内容:
应用规范.yaml
version: 0.0
os: linux
files:
- source: /
destination: /home/centos/my-server
hooks:
BeforeInstall:
- location: scripts/cleanup.sh
timeout: 300
runas: root
AfterInstall:
- location: scripts/install_dependencies.sh
timeout: 300
runas: root
- location: scripts/update_service.sh
timeout: 300
runas: root
ApplicationStart:
- location: scripts/start_server.sh
timeout: 300
runas: root
ApplicationStop:
- location: scripts/stop_server.sh
timeout: 300
runas: root
启动服务器.sh
#!/bin/bash
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 9080
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 9443
sudo systemctl daemon-reload
sudo systemctl restart myserver.service
我的服务器服务
[Unit]
Description=Node.js My server
[Service]
ExecStart=/usr/bin/node /home/centos/my-server/index.js
WorkingDirectory=/home/centos/my-server/
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=my-server-log
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
index.js
const winston = require('winston');
const express = require('express');
const logging = require('./startup/logging');
const config = require('./startup/config');
const routes = require('./startup/routes');
const db = require('./startup/db');
const validation = require('./startup/validation');
const redirect = require('./startup/redirect');
const app = express();
const successMessage = async () => {
const message = 'my-server is up and running and ready for requests..';
winston.info(message);
console.log(message);
};
const startServer = async () => {
await logging();
await config();
await routes(app);
await db();
await validation();
await redirect(app);
await successMessage();
};
startServer();
数据库.js
const mongoose = require('mongoose');
const winston = require('winston');
const nconf = require('nconf');
const fs = require('fs');
module.exports = async function () {
// Get the mongodb credentials from config memory
const ipAddress = fs.existsSync('/.dockerenv')
? nconf.get('mongodb:dockerIPAddress')
: nconf.get('mongodb:ipAddress');
const port = nconf.get('mongodb:port');
const username = encodeURIComponent(nconf.get('mongodb:username'));
const password = encodeURIComponent(nconf.get('mongodb:password'));
const database = nconf.get('mongodb:database');
const mongoUrl = `mongodb://${username}:${password}@${ipAddress}:${port}/${database}?authSource=admin`;
mongoose
.connect(mongoUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => winston.info('Connected to mongodb successfully..'))
.catch((err) => winston.error('Could not connect to MongoDB!!!', err));
};
我检查了syslog
, journalctl
, codedeploy 日志,没有错误。并且当服务器没有连接到数据库时不会抛出任何错误。如果有人可以提供帮助,那就太棒了。