Run multiple apps in one server

-출처: https://sungwoncho.io/run-multiple-apps-in-one-droplet/

It is possible to run more than one apps in a single virtual private server.

Having used Platform-as-a-Service products like Heroku most of the time, this basic idea was so foreign to me. Now that I am running several apps in my server, I would like to share how I am doing so.

I am using Nginx as a HTTP server. I am also using Digital Ocean, but this post can be generalized to any infrastructure providers.

Our goal

We want to run two apps in our server: a NodeJS and a Meteor app. We want to make it so that users can go to node_app.com to use NodeJS app, and meteor_app.com to use Meteor app.

Reverse proxy

What we want to do in this situation is to set up a reverse proxy using Nginx. A reverse proxy is like a traffic light that lets users connect to multiple applications or servers.

In our case, we can have Nginx listen on port 80 using node_app.com and meteor_app.com as server_name.

This way, when a request comes in for node_app.com, Nginx routes it to the NodeJS app. A request for meteor_app.com will be routed to Meteor app.

Here are the steps to set it up:

1. Run apps on different ports

Using forever, we run the NodeJS app by executing the following in the app root directory.

NODE_ENV=production forever start [app_file_name]

We could do the same with the Meteor app.

forever start [app_file_name]

Let us say that the node app was running on the port 2368, and Meteor app on the port 3000.

By the way, forever is a handy tool to keep the script running, well… forever.

2. Make Nginx configuration files

In /etc/nginx/sites-available, we make two configuration files.

node_app.conf

server {  
  listen 80;
  server_name node_app.com

  location / {
    proxy_pass         http://127.0.0.1:2368;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection "upgrade";
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   Host      $http_host;
  }
}

meteor_app.conf

server {  
  listen 80;
  server_name meteor_app.com

  location / {
    proxy_pass         http://127.0.0.1:3000;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection "upgrade";
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   Host      $http_host;
  }
}

All we are doing is making Nginx listen on the port 80 using different server_name, and routing the requests to either port 2368 or 3000 depending on the server_namethat request is hitting.

server_name is an Nginx directive. The official doc says:

Server names are defined using the server_name directive and determine which server block is used for a given request.

3. Restart Nginx

To apply the changes, you need to restart Nginx by running:

sudo service nginx restart

Before you do that, you can test if all the configurations are valid by running:

nginx -t

If Nginx fails to restart for some reason, you can always check the log at /var/log/nginx/error.log. It is the default path for error logs unless you specify one in the config.

Conclusion

You can set up a reverse proxy using Nginx to listen on port 80 and route requests to other ports on which different apps are running. All this can be done in one server.

How To Install Nginx on Ubuntu 14.04 LTS

– 출처: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-14-04-lts

Introduction

Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or a reverse proxy.

In this guide, we’ll discuss how to get Nginx installed on your Ubuntu 14.04 server.

Prerequisites

Before you begin this guide, you should have a regular, non-root user with sudo privileges configured on your server. You can learn how to configure a regular user account by following steps 1-4 in our initial server setup guide for Ubuntu 14.04.

When you have an account available, log in as your non-root user to begin.

Step One — Install Nginx

We can install Nginx easily because the Ubuntu team provides an Nginx package in its default repositories.

Since this is our first interaction with the apt packaging system in this session, we should update our local package index before we begin so that we are using the most up-to-date information. Afterwards, we will install nginx:

sudo apt-get update
sudo apt-get install nginx

You will probably be prompted for your user’s password. Enter it to confirm that you wish to complete the installation. The appropriate software will be downloaded to your server and then automatically installed.

Step Two — Check your Web Server

In Ubuntu 14.04, by default, Nginx automatically starts when it is installed.

You can access the default Nginx landing page to confirm that the software is running properly by visiting your server’s domain name or public IP address in your web browser.

If you do not have a domain name set up for your server, you can learn how to set up a domain with DigitalOcean here.

If you do not have a spare domain name, or have no need for one, you can use your server’s public IP address. If you do not know your server’s IP address, you can get it a few different ways from the command line.

Try typing this at your server’s command prompt:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

You will get back one or two lines. You can try each in your web browser to see if they work.

An alternative is typing this, which should give you your public IP address as seen from another location on the internet:

curl http://icanhazip.com

When you have your servers IP address or domain, enter it into your browser’s address bar:

http://server_domain_name_or_IP

You should see the default Nginx landing page, which should look something like this:

Nginx default page

This is the default page included with Nginx to show you that the server is installed correctly.

Step Three — Manage the Nginx Process

Now that you have your web server up and running, we can go over some basic management commands.

To stop your web server, you can type:

sudo service nginx stop

To start the web server when it is stopped, type:

sudo service nginx start

To stop and then start the service again, type:

sudo service nginx restart

We can make sure that our web server will restart automatically when the server is rebooted by typing:

sudo update-rc.d nginx defaults

This should already be enabled by default, so you may see a message like this:

System start/stop links for /etc/init.d/nginx already exist.

This just means that it was already configured correctly and that no action was necessary. Either way, your Nginx service is now configured to start up at boot time.

Conclusion

Now that you have your web server installed, you have many options for the type of content to serve and the technologies you want to use to create a richer experience.

Learn how to use Nginx server blocks here. If you’d like to build out a more complete application stack, check out this article on how to configure a LEMP stack on Ubuntu 14.04.

By Justin Ellingwood

Simple Meteor Deployment for CentOS 6+

– 출처: https://github.com/creatorkuang/meteor-please

meteor-please for centos 6

npm version

Simple Meteor Deployment for CentOS 6+

Installation

npm install -g mplzc6

Usage

1. Initialise

Simply run in your Meteor project’s directory:

mplzc6

You’ll get a prompt to automatically configure a mplz.json for your project.

2. Setup Your Environment

Once you’ve got a configuration file, you can spin up your server, then use this command inside your project directory to install the production environment (nodejs, mongodb, nginx):

mplzc6 setup

Now go grab a coffee, because it will probably take some time for all the things to install.

3. Deploy Your App

After the server setup is done, you can run this command to deploy your app:

mplzc6 deploy

Easy!

Commands

mplzc6 init Reconfigures your app’s mplz.json settings file.

mplzc6 setup Sets up your server according to your mplz.json settings.

mplzc6 deploy Deploys your app according to your mplz.json settings.

mplzc6 reconfig Apply any configuration changes if your mplz.json has been modified since last setup.

mplzc6 start Starts your app. (systemd)

mplzc6 stop Stops your app. (systemd)

mplzc6 restart Restarts your app. (systemd)

mplzc6 delete Deletes your app from the deployment directory.

TODOs

  • SSL
  • Multiple instances/load balancing/oplog tailing
  • Prompt cleanup/validation
  • Support for node apps
  • Exclude folders

Installing Node.js via package manager

– 출처: https://nodejs.org/en/download/package-manager/

Enterprise Linux and Fedora

Including Red Hat® Enterprise Linux® / RHEL, CentOS and Fedora.

Node.js is available from the NodeSource Enterprise Linux and Fedora binary distributions repository. Support for this repository, along with its scripts, can be found on GitHub at nodesource/distributions.

Note that the Node.js packages for EL 5 (RHEL5 and CentOS 5) depend on the EPEL repository being available. The setup script will check and provide instructions if it is not installed.

Run as root on RHEL, CentOS or Fedora, for Node.js v4 LTS Argon:

curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -

Alternatively for Node.js v5:

curl --silent --location https://rpm.nodesource.com/setup_5.x | bash -

Alternatively for Node.js 0.10:

curl --silent --location https://rpm.nodesource.com/setup | bash -

Then install, as root:

yum -y install nodejs