0

How to run nginx and Node.js at server startup?

In order to start Amazon EC2 with AWS Auto Scaling, I must directly connect to EC2 to run nginx and Node.js.

Can this be done for Auto Scaling?


In Amazon EC2, I want to run nginx with Node.js during EC2 startup by Auto Scaling.

EC2 is set up as an Auto Scaling group using images. I want to run EC2 Node.js applications and nginx, which are started by Auto Scaling, together with the EC2 server startup.

For nginx, I can run the executable with chkconfig, but the Node.js application will run as pm2, using the code written in package.json.

How can I run nginx and Node.js with EC2 startup and let the new EC2 -- started with Auto Scaling -- respond properly?


comment reply :

I don't want to run node.js using "node app.js" command.

I want to run node.js by package.json ( script )

ex.

  "start": "NODE_ENV=production PORT=3000 pm2 start server.js -i -1"

How can I do this?

Your suggestions are using linux server init script file.

But, I want to set NODE_ENV, PORT and use pm2 command.


solution

I solved the problem.

When Linux booted, I tried to use the script file to automatically run node.js.

I created the script file and made the shell script run automatically after linux booted, but it did not seem to be a good idea.

Alternatively, pm2 startup and ecosystem.config.js can be used to solve problems flexibly.

Thank you for your reply.

hyundeock
  • 445
  • 1
  • 6
  • 15
  • 1
    Possible duplicate of [How can I automatically start a node.js application in Amazon Linux AMI on aws?](https://stackoverflow.com/questions/11275870/how-can-i-automatically-start-a-node-js-application-in-amazon-linux-ami-on-aws) – Joey Ciechanowicz Mar 25 '19 at 06:55
  • Thank you! But is there any way to run it without writing a script file on the server? If you are using a passenger, you know that it is possible to run a node from the nginx server block. Is not this possible? – hyundeock Mar 25 '19 at 07:01

1 Answers1

2

This has nothing to do with autoscaling. It most often has to do with the EC2 AMI (Amazon Machine Image) that the autoscaler is launching your EC2 instances with, and possibly also with the "user metadata" that you are passing to the instance when it launches. These are the only two things that impact what an EC2 instance does when and after it starts up, up until it starts communicating with the outside world.

So what you need to do is create an AMI that is set up so that the right things launch when an EC2 instance is launched from that AMI. What you'd do is take the AMI you want to use as a starting point, launch that AMI into an instance, make the necessary changes and installations you want, and then save off a new AMI. Then change your autoscaling group to launch new instances with that new AMI.

You could alternately send a script in your "user metadata" that launches things, but this is rarely what you want to do". Most of the time, you want to have your AMI do the right thing.

It's also possible that you are using some sort of post-boot provisioner, like Chef, Ansible or Chef Habitat. If you are, that's where you'd set all of this stuff up. You'd want that system to do the work you're describing. But if you're doing that, what I have said earlier still applies. For this to work, you'd often have also built a custom AMI that has parts of the provisioning system already built into it, so that that system can connect into it and provision it. It's possible for these systems to start from a generic AMI as well. It depends on the system.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Thank you for answer! As you have suggested, I am using EC2 registered with auto scailing using AMI. However, my question is related to registering the initial settings when creating an AMI. As EC2 runs, node.js Can I get it to run automatically? – hyundeock Mar 26 '19 at 02:31
  • What I think you want is to work on a EC2 instance until it does everything you want, including starting everything up right. When you can restart your EC2 instance and have everything running right, that's when you've got what you want. Then you create an AMI from that, and every time the scaler launches an instance, it will do that same thing. So ideally, there's no magic. It will just reproduce whatever happens when you start that one instance you're using as a prototype. Make sense? – CryptoFool Mar 26 '19 at 04:56
  • The only way it should get complicated is if you want to influence the behavior so that different instances launched from your AMI do different things. That's where the user metadata would come into it. You can in one way or another cause the metadata to be different for different launches. What's in your AMI would somehow look at that data coming in and decide how to act. These two ideas together are really all there is, from my own fairly extensive experience. Of course, from these two things, you can go on and do just about anything. – CryptoFool Mar 26 '19 at 04:58
  • Once the instance starts, and you've set it up the way you want, anything is possible. It can talk to external components to get configuration. It can listen, and external components can connect to it. This could even be done without a custom AMI if the outside world launched a standard instance, then ssh'd into it and started setting it up. That's what our system actually does. Up to you. Main thing though is the idea that until the instance is running, you just have the AMI setup with the user instance metadata attached to it. Most of the time, you don't use the metadata. – CryptoFool Mar 26 '19 at 04:59
  • I can help you with specifics, but I don't know Node very well. But just start an EC2 instance and configure it to start Node.js, so that it works right when you restart it. Then save off an AMI, and every time you launch an instance from that AMI, node.js will start up, just like with the original prototype. – CryptoFool Mar 26 '19 at 05:02
  • btw, best to power down the instance before imaging it. There's a checkbox when you make an AMI so you can optionally have it stop the instance to image it it. – CryptoFool Mar 26 '19 at 05:03
  • Detailed explanation Thank you very much. I know that it is right to do what you say. As in the above answer, I wonder how to do Node.js to run automatically. For example, you could use nginx's server block or use init.d's script file. – hyundeock Mar 26 '19 at 07:33