5

It's probably related to this question: How to run more than one app on one instance of EC2

But that question only seemed to be talking about multiple node.js apps.

I am trying learn several different things, so I'm building different websites to learn Ruby on Rails, LAMP, and node.js. Along with my personal website and blog.

Is there any way to run all these on the same EC2 instance?

3
  • You can follow this answer stackoverflow.com/a/16549172/93988. It's general enough to work with Ruby and Apache. Apr 1, 2014 at 1:45
  • Yeah, that's the question I linked to. Installing several different web servers and running them on one instance, the answers referenced still applies? Apr 1, 2014 at 1:47
  • 1
    Yes. As long as all of them are using different ports there will be no conflict. Apr 1, 2014 at 1:49

2 Answers 2

8

First, there's nothing EC2-specific about setting up multiple web apps on one box. You'll want to use nginx (or Apache) in "reverse proxy" mode. This way, the web server listens on port 80 (and 443), and your apps run on various other ports. Each incoming request reads the "Host" header to map the request to a backend. So different DNS names/domains show different content.

Here is how to setup nginx in reverse proxy mode: http://www.cyberciti.biz/tips/using-nginx-as-reverse-proxy.html

For each "back-end" app, you'll want to:

1) Allocate a port (3000 in this example)

2) write an upstream stanza that tells it where your app is

3) write a (virtual) server stanza that maps from the server name to the upstream location

For example:

upstream app1  {
      server 127.0.0.1:3000; #App1's port
}

server {
    listen       *:80;
    server_name  app1.example.com;

    # You can put access_log / error_log sections here to break them out of the common log.

    ## send request to backend
    location / {
     proxy_pass              http://app1;
     proxy_set_header        Host            $host;
     proxy_set_header        X-Real-IP       $remote_addr;
     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}

I prefer to have Nginx in front of Apache for two reasons: 1) nginx can serve static files with much less memory, and 2) nginx buffers data to/from the client, so people on slow internet connections don't clog your back-ends.

When testing your config, use nginx -s reload to reload the config, and curl -v -H "Host: app1.example.com" http://localhost/ to test a specific domain from your config

0
5

Adding to the @Brave answer, I would like to mention the configuration of my nginx for those who are looking for the exact syntax in implementing it.

server {
  listen 80;
  server_name mysite.com;
  location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Host       $http_host;
    proxy_pass        http://127.0.0.1:3000;
  }
}
server {
  listen 80;
  server_name api.mysite.com;
  location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Host       $http_host;
    proxy_pass        http://127.0.0.1:4500;
  }
}

Just create two server objects with unique server name and the port address.

Mind proxy_pass in each object.

Thank you.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.