【问题标题】:Create a startup script for meteor in linux server在 linux 服务器中为流星创建启动脚本
【发布时间】:2017-03-31 07:04:20
【问题描述】:

我也按照一些帖子和教程创建了一个脚本,以便在服务器重新启动时启动流星项目。我已经关注了How to run meteor on startup on Ubuntu server

中提到的答案

然后我使用“chmod +x meteor-server.sh”授予脚本可执行权限。

我已尝试将此脚本放在 /etc/init.d/etc/init 文件夹中,但重启后流星项目未启动。我正在使用 ubuntu 16.04

如果你能告诉我我所做的错误,我将不胜感激。以下代码是我的“meteor.server.sh”脚本文件。

     # meteorjs - meteorjs job file

     description "MeteorJS"
     author "Jc"

     # When to start the service
     start on runlevel [2345]

     # When to stop the service
     stop on runlevel [016]

     # Automatically restart process if crashed
     respawn

     # Essentially lets upstart know the process will detach itself to the background
     expect fork

     # Run before process
     pre-start script
    cd /home/me/projects/cricket
    echo ""
    end script

   # Start the process
   exec meteor run -p 4000 --help -- production

【问题讨论】:

  • 为什么不构建项目并运行生成的包?
  • 感谢您的回复,我只是想在服务器重启后自动启动我的应用程序
  • 但首先不推荐在生产环境中运行它。
  • 我尝试在 systemd 上放置一个服务并从中调用脚本。但是还是不行
  • 永远不要使用meteor run --production 在你的生产服务器上运行你的应用程序,真的——不要那样做。以下是一些你应该阅读的材料:stackoverflow.com/questions/21316344/…guide.meteor.com/deployment.html#never-use-production-flag

标签: linux meteor ubuntu-16.04


【解决方案1】:

首先,已经有一个很好的工具mupx,可以让你将meteor项目部署到自己的架构中,所以除非你有很好的,否则真的没必要自己动手。

如果您确实需要手动部署,这将需要几个步骤。我不会涵盖所有细节,因为您专门询问启动脚本,其余说明应该可以在 Internet 上轻松访问。

1。准备你的服务器

  • 除非您打算使用远程数据库,否则请安装 MongoDB。
  • 安装 NodeJS。
  • 安装反向代理服务器,例如Nginx 或 haproxy。
  • 安装 Meteor,我们将仅将其用作构建工具。

2。构建您的应用

我在这里假设您已经将应用程序的源代码放在您计划部署到的服务器上。转到您的项目根目录并运行以下命令:

meteor build /path/to/your/build --directory

请注意,如果/path/to/your/build 存在,它将首先递归删除,所以要小心。

3。安装应用依赖项

转到/path/to/your/build/bundle/programs/server 并运行:

npm install

4。准备一个run.sh 脚本

文件可以是以下格式:

export MONGO_URL="mongodb://127.0.0.1:27017/appName"
export ROOT_URL="http://myapp.example.com"
export PORT=3000
export METEOR_SETTINGS="{}"

/usr/bin/env node /path/to/your/build/bundle/main.js

我假设你把它放在/path/to/your/run.sh。这里有几点说明:

  • MONGO_URL 的这种形式假定您已在本地安装了 MongoDB。
  • 您需要指示反向代理服务器将您的应用流量指向端口3000
  • METEOR_SETTINGS 应该是您可能拥有的任何 settings 对象的 JSON.stringify(settings) 的输出。

5。准备upstart脚本

到目前为止,我们已经做好了所有准备工作,脚本可以很简单

description "node.js server"

start on (net-device-up and local-filesystems and runlevel [2345])
stop on runlevel [016]

respawn
script
exec /path/to/your/run.sh
end script

这个文件应该去/etc/init/appName.conf

【讨论】:

  • 感谢您的回复,它很好地描述了这个过程。我用另一种方式来满足我的要求。请仔细阅读我下面提到的答案。
【解决方案2】:

我终于让它工作了。我使用以下 2 个脚本在启动时运行流星。 首先我把这个服务文件(meteor.service)放在/etc/systemd/system

       [Unit]
       Description = My Meteor Application

       [Service]
       ExecStart=/etc/init.d/meteor.sh
       Restart=always

       StandardOutput=syslog
       StandardError=syslog
       SyslogIdentifier=meteor

      [Install]
      WantedBy=multi-user.target

我已经使用此服务调用了 scipt。我将以下脚本(meteor.sh)放在 /etc/init.d

  #!/bin/sh -

  description "Meteor Projects"
  author "Janitha"

  #start service on following run levels
  start on runlevel [2345]

  #stop service on following run levels
  stop on runlevel [016]

  #restart service if crashed
  respawn

  #set user/group to run as
  setuid janitha
  setgid janitha

  chdir /home/janitha/projects/cricket_app

  #export HOME (for meteor), change dir to plex requests dir, and run meteor
  script
    export HOME=/home/janitha
    exec meteor
  end script

我使用

使这两个文件都可执行
 chmod +x meteor.service
 chmod +x meteor.sh

我使用了以下两个命令来启用该服务

systemctl daemon-reload
systemctl enable meteor.service

【讨论】:

  • 我很高兴你终于成功地完成了这项工作。但是,请记住,在生产环境中使用meteor 命令仍然不是为您的应用程序提供服务的推荐方式。它可能在许多方面有害。归根结底,您自己决定使用什么,但要考虑到其他人也在阅读这些帖子,他们应该知道这种方法不是最佳的。如果我是你,我至少会在你的回答中添加一个简短的警告说明。
  • 是的,我明白了。非常感谢@apendua
【解决方案3】:

我成功使用了这个配置

在 /etc/init.d 中添加一个名为 meteor.sh 的文件


#!/bin/sh 
export HOME="/home/user"
cd /home/user/meteor/sparql-fedquest
meteor --allow-superuser

你必须给meteor.sh执行权限

sudo chmod 644 meteor.sh

你还必须在 /etc/systemd/system 中创建meteor.service

[Unit]
   Description =Portal of bibliographic resources of University of Cuenca
   Author = Freddy Sumba
   [Service]
   ExecStart=/etc/init.d/meteor.sh
   Restart=always
   StandardOutput=syslog
   StandardError=syslog
   SyslogIdentifier=meteor
  [Install]
  WantedBy=multi-user.target

您还必须授予流星服务的权限

$ sudo chmod 644 meteor.service

然后,我们需要在每次服务器重新启动时将服务添加到此启动

$  systemctl enable meteor.service

最后启动服务

$ service meteor start

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 2015-11-02
    • 2015-08-21
    • 2014-03-11
    • 1970-01-01
    相关资源
    最近更新 更多