简体   繁体   English

如何在一个EC2实例上运行多个应用程序

[英]How to run more than one app on one instance of EC2

I have a couple of small production sites and a bunch of fun hobbyist/experimental apps and such. 我有几个小型生产站点和一堆有趣的业余爱好者/实验性应用程序等。 I'd like to run all of them on one EC2 instance. 我想在一个EC2实例上运行所有它们。

Can I install node.js, npm, express and couchdb once, and then run each app on a different port, and adjust the dns settings at my domain registry to point to the appropriate place? 我可以安装一次node.js,npm,express和benchdb,然后在不同的端口上运行每个应用程序,并在域注册表中调整dns设置以指向适当的位置吗?

Update: Thanks Mike! 更新:谢谢迈克! For anyone else who's looking for multiple IP addresses on EC2: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html 对于正在EC2上寻找多个IP地址的其他任何人: http : //docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html

There are multiple ways to go about it. 有多种解决方法。

Different Ports 不同的端口

You could run each Node.js process on a different port and simply open the ports to the world. 您可以在不同的端口上运行每个Node.js进程,只需打开端口即可。 However, your URLs would need a port on the end of the hostname for each project. 但是,对于每个项目,您的URL都需要在主机名末尾有一个端口。 yoururl.com:8080/ would technically work, but probably not what you're looking for. yoururl.com:8080/技术yoururl.com:8080/可以正常工作,但可能不是您想要的。

Multiple IP Addresses 多个IP地址

You could use multiple IP addresses on one EC2 instance, however, they come with an additional cost of about $3.65 a month each. 您可以在一个EC2实例上使用多个IP地址,但是,每个IP地址每月要额外支付3.65美元。 So if you have 10 different domains you want to host on once instance then that's over $30 a month extra in hosting fees. 因此,如果您要在一个实例上托管10个不同的域,则每月需多付30美元的托管费。

On the flip side, any domain using SSL needs it's own IP address. 另一方面,任何使用SSL的域都需要它自己的IP地址。

Also, there are limits to the number of IP addresses you can assign to an instance and the smaller the instance, the less IP addresses you get. 此外,可以分配给实例的IP地址数量也有限制,实例越小,获得的IP地址就越少。

The number of IP addresses that you can assign varies by instance type. 您可以分配的IP地址数量因实例类型而异。 Small instances can accommodate up to 8 IP addresses (across 2 elastic network interfaces) whereas High-Memory Quadruple Extra Large and Cluster Computer Eight Extra Large instances can be assigned up to 240 IP addresses (across 8 elastic network interfaces). 小型实例最多可以容纳8个IP地址(跨2个弹性网络接口),而高内存四倍体超大型和群集计算机可以为8个特大实例分配多达240个IP地址(跨8个弹性网络接口)。 For more information about IP address and elastic network interface limits, go to Instance Families and Types in the Amazon EC2 User Guide. 有关IP地址和弹性网络接口限制的更多信息,请转到Amazon EC2用户指南中的实例系列和类型。

Express Vhosts 快速虚拟主机

Express comes with virtual host functionality. Express具有虚拟主机功能。 You can run multiple domains under one Node.js/Express server and setup routes based on domain name. 您可以在一台Node.js / Express服务器下运行多个域,并根据域名设置路由。 vhost under Express enables this. Express下的vhost启用了此功能。

Reverse Proxy 反向代理

You can setup Nginx in front of multiple application servers. 您可以在多个应用程序服务器之前设置Nginx。 This has the most flexibility. 这具有最大的灵活性。 You can have one Node.js process per domain which allows you to do updates and restarts on one domain at a time. 每个域可以有一个Node.js进程,它允许您一次在一个域上进行更新和重新启动。 It also allows you to host applications servers such as Apache/PHP under the same EC2 instance along side your Node.js process. 它还允许您在Node.js进程的同一EC2实例下托管应用程序服务器(例如Apache / PHP)。

With Nginx acting as a reverse proxy you could also host different application servers under the same domain, but serving different paths. 使用Nginx充当反向代理,您还可以在同一域中托管不同的应用程序服务器,但是服务于不同的路径。

For instance, Node.js could serve the main root path of a domain but you could setup the /blog/ path to go to a Apache/PHP/Wordpress setup on the same EC2 instance. 例如,Node.js可以提供域的主根路径,但是您可以设置/blog/路径以转到同一EC2实例上的Apache / PHP / Wordpress设置。

Already answered @ https://stackoverflow.com/a/50528580/3477353 but mentioning it again. 已回答@ https://stackoverflow.com/a/50528580/3477353,但再次提及。

Adding to the @Daniel answer, I would like to mention the configuration of my nginx for those who are looking for the exact syntax in implementing it. 除了@Daniel答案外,我想为那些正在寻找实现它的确切语法的人提到我的nginx的配置。

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. 注意每个对象中的proxy_pass。

Thank you. 谢谢。

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

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