Deploying OpenVidu Server and your app on AWS


Deploying OpenVidu Server on AWS with Cloud Formation

The deployment of OpenVidu can be a piece of cake if you have an AWS account. Just follow these steps:

1. Access to the console of AWS Cloud Formation

Go to CloudFormation

2. Click on Create Stack

3. Option Specify an Amazon S3 template URL with the following URL

https://s3-eu-west-1.amazonaws.com/aws.openvidu.io/CF-OpenVidu-latest.json

4. Complete the configuration fields

We provide 3 different scenarios: you can use the default SELF-SIGNED CERTIFICATE stored in the application (users will need to accept the security alert) or if you have a custom domain, either allow LET'S ENCRYPT to automatically generate a valid and free certificate for your domain or use your own CUSTOM CERTIFICATE if you already have one.

Self-Signed certificate Let's Encrypt certificate Custom certificate
Stack name Your choice Your choice Your choice
Type of SSL Certificate selfsigned letsencrypt owncert
Email Your choice
Fully qualified domain name Your custom domain
(for example: openvidu.io)
Your custom domain
(for example: openvidu.io)
Elastic IP One AWS Elastic IP you generated
(check AWS Docs to generate a new one)
One AWS Elastic IP you generated
(check AWS Docs to generate a new one)
CRT File Content of your .crt file
-----BEGIN CERTIFICATE-----
fooFOOfooFOOfooFOOfoo...
-----END CERTIFICATE-----
KEY File Content of your .key file
-----BEGIN RSA PRIVATE KEY-----
fooFOOfooFOOfooFOOfoo...
-----END RSA PRIVATE KEY-----
OpenVidu Secret Your choice Your choice Your choice
Instance Type Your choice
(at least t2.medium recommended)
Your choice
(at least t2.medium recommended)
Your choice
(at least t2.medium recommended)
KeyName Your choice
(check AWS Docs to create a new one)
Your choice
(check AWS Docs to create a new one)
Your choice
(check AWS Docs to create a new one)

5. Create your Stack

No extra options are necessary. Click on NextNextCreate

CREATE_IN_PROGRESS status will show up. You will now have to wait for a few minutes until it shows CREATE_COMPLETE.

6. Access and test your OpenVidu Server through your new IP

After status changes to CREATE_COMPLETE, go to Outputs tab to get your brand new IP and click on it (or if you have deployed under your own custom domain, then you should access through it).

You will connect to your OpenVidu dashboard. To access to it use:

  • OPENVIDUAPP as username
  • The secret you chose on Step 4 as password (field OpenVidu Secret in the table).

At OpenVidu dashboard you can test the video transmission. You can now add your own application to your instance. To learn how check the next section.

Adding your own app to CloudFormation OpenVidu Server

Once you have deployed your CloudFormation OpenVidu Server and checked that it is up and running, you can easily add your own application to the instance. Follow these steps:

1. Upload your app to your EC2 instance

In Linux/Mac you can use scp command. In Windows (or If you prefer a more friendly GUI) you can use FileZilla. Furthermore, if your app is stored in a GitHub repo, you can directly clone it once you connect to your instance.

Remember that the key will be the same you indicated when configuring the CloudFormation fields.

2. Connect to your EC2 instance through SSH

Remember that the key will be the same you indicated when configuring the CloudFormation fields. In case of doubt, check AWS docs.

3. Configure your app

Depending on the framework of your app:


Plain HTML/CSS/JS
  • If your app doesn't have a server-side and it is plain HTML, CSS and JavaScript, you just have to add your web files into /var/www/html/. You will have instant access to it through https://AMAZON_URL_OR_YOUR_CUSTOM_DOMAIN/

    If you want to test the deployment of a plain HTML/CSS/JS app, you can use openvidu-insecure-js


Java
  • Copy your JAR or WAR into /opt/

  • Write a script to launch your app with all the parameters it needs, and store it under /opt/. For example, a file /opt/YOUR_LAUNCHER.sh containing:

    cd /opt
    java -jar -Dserver.port=4040 myapp.jar
    

    IMPORTANT 1: It is crucial to navigate to /opt before the launching command. Otherwise, the system will surely have some problems for finding your files

    IMPORTANT 2: Obviously your app will need Java to run. You must install the correct version of Java in your machine (check version: java -version)

    IMPORTANT 3: Make sure the script you made is executable.(For Linux you could do like this chmod +x YOUR_LAUNCHER.sh)

  • Configure Nginx: add a new location directive to the file /etc/nginx/sites-enabled/default, inside the server { } group:

      location / {
        rewrite /(.*) /$1 break;
        proxy_pass https://localhost:PORT;
      }
    

    For example

      location / {
        rewrite /(.*) /$1 break;
        proxy_pass https://localhost:4040;
      }
    
  • Configure Supervisor: add the script you wrote in the second step to the file /etc/supervisor/conf.d/openvidu.conf like this:

      [program:YOUR_APP]
      command=/bin/bash /opt/YOUR_LAUNCHER.sh YOUR_APP_PARAM_1 YOUR_APP_PARAM_2 ...
      redirect_stderr=true
    

    Now your /etc/supervisor/conf.d/openvidu.conf must looks like this:

      [supervisord]
      nodaemon=true
      logfile=/var/log/supervisor/supervisord.log
      pidfile=/var/run/supervisord.pid
      loglevel=debug
    
      [program:openvidu-server]
      command=java -jar -Dopenvidu.secret="MY_SECRET" -Dserver.ssl.enabled=false -Dopenvidu.pu$
    
      [program:YOUR_APP]
      command=/bin/bash /opt/YOUR_LAUNCHER.sh YOUR_APP_PARAM_1 YOUR_APP_PARAM_2 ...
      redirect_stderr=true
    

    To connect your Java app to OpenVidu Server in order to get your sessionIds and tokens (check Securization section), you will need to use the URL https://localhost:8443. localhost because both your app and OpenVidu Server run in the same machine. 8443 because there's where OpenVidu Server listens for petitions. For example, our tutorial openvidu-js-java sets this parameter as an environment variable right here.

  • Restart Nginx: systemctl restart nginx

  • Restart Supervisor: systemctl restart supervisor

  • You will have access to your app through https://AMAZON_URL_OR_YOUR_CUSTOM_DOMAIN/

  • Troubleshooting: If your app is not working as expected, there are a few files you should check for debugging: /var/log/nginx/ (info about the proxy), /var/log/supervisor/ (output of your app). You can also try to connect to your app directly through the port like https://AMAZON_URL_OR_YOUR_CUSTOM_DOMAIN:YOUR_APP_PORT

    If you want to test the deployment of a Java app, you can use openvidu-js-java or openvidu-mvc-java (release here)


Node
  • Add your web files into /opt/

  • Write a script to launch your app with all the parameters it needs, and store it under /opt/. For example, a file /opt/YOUR_LAUNCHER.sh containing:

    cd /opt
    node myserver.js 4040
    

    IMPORTANT 1: It is crucial to navigate to /opt before the launching command. Otherwise, the system will surely have some problems for finding your files

    IMPORTANT 2: Obviously your app will need Node to run. You must install the correct version of Node in your machine (check version: nodejs -v)

    IMPORTANT 3: Make sure the script you made is executable.(For Linux you could do like this chmod +x YOUR_LAUNCHER.sh)

  • Configure Nginx: add a new location directive to the file /etc/nginx/sites-enabled/default, inside the server { } group::

      location / {
        rewrite /(.*) /$1 break;
        proxy_pass https://localhost:PORT;
      }
    

    For example

      location / {
        rewrite /(.*) /$1 break;
        proxy_pass https://localhost:4040;
      }
    
  • Restart Nginx: systemctl restart nginx

  • Configure Supervisor: add the script you wrote in the second step to /etc/supervisor/conf.d/openvidu.conf like this:

      [program:YOUR_APP]
      command=/bin/bash /opt/YOUR_LAUNCHER.sh YOUR_APP_PARAM_1 YOUR_APP_PARAM_2 ...
      redirect_stderr=true
    

    Now your /etc/supervisor/conf.d/openvidu.conf must looks like this:

      [supervisord]
      nodaemon=true
      logfile=/var/log/supervisor/supervisord.log
      pidfile=/var/run/supervisord.pid
      loglevel=debug
    
      [program:openvidu-server]
      command=java -jar -Dopenvidu.secret="MY_SECRET" -Dserver.ssl.enabled=false -Dopenvidu.pu$
    
      [program:YOUR_APP]
      command=/bin/bash /opt/YOUR_LAUNCHER.sh YOUR_APP_PARAM_1 YOUR_APP_PARAM_2 ...
      redirect_stderr=true
    

    To connect your Node app to OpenVidu Server in order to get your sessionIds and tokens (check Securization section), you will need to use the URL https://localhost:8443. localhost because both your app and OpenVidu Server run in the same machine. 8443 because there's where OpenVidu Server listens for petitions. For example, our tutorial openvidu-js-node expects this parameter as the first argument on launch command right here.

  • Restart Supervisor: systemctl restart supervisor

  • You will have access to your app through https://AMAZON_URL_OR_YOUR_CUSTOM_DOMAIN/

  • Troubleshooting: If your app is not working as expected, there are a few files you should check for debugging: /var/log/nginx/ (info about the proxy), /var/log/supervisor/ (output of your app). You can also try to connect to your app directly through the port like https://AMAZON_URL_OR_YOUR_CUSTOM_DOMAIN:YOUR_APP_PORT

    If you want to test the deployment of a Node app, you can use openvidu-js-node


PHP, Ruby, Python, .NET...

We are not going to spell out in detail what is to be done with the rest of backend technologies, but you can get the idea from the Java and Node steps above. It is always the same process: take your app to your instance, place it under /opt/, configure Nginx and Supervisor (this last with the necessary launching script depending on your technology) and relaunch both services. Be sure that your machine has all the necessary dependencies and technologies to execute your application, depending on the framework (Java for a Java app, Node for a Node app, Ruby for a Ruby app...)

Voilà! It is really this simple.

PS: Multiple apps in the same instance

To launch multiple apps in the same instance, just store each one of them inside its own folder under /opt/. For example:

  • /opt/YOUR_APP_1 : will be available at https://AMAZON_URL_OR_YOUR_CUSTOM_DOMAIN/YOUR_APP_1/
  • /opt/YOUR_APP_2 : will be available at https://AMAZON_URL_OR_YOUR_CUSTOM_DOMAIN/YOUR_APP_2/

The other steps explained above also need some adjustments related to paths:

  • Configure Nginx: now it would be...

    location /YOUR_APP_1 {
      rewrite /YOUR_APP_1(.*) /$1 break;
      proxy_pass https://localhost:PORT1;
    }
    
    location /YOUR_APP_2 {
      rewrite /YOUR_APP_2(.*) /$1 break;
      proxy_pass https://localhost:PORT2;
    }
    
  • Configure Supervisor: now...

      [program:YOUR_APP_1]
      command=/bin/bash /opt/YOUR_APP_1/YOUR_LAUNCHER_1.sh YOUR_APP1_PARAM_1 YOUR_APP1_PARAM_2 ...
      redirect_stderr=true
    
      [program:YOUR_APP_2]
      command=/bin/bash /opt/YOUR_APP_2/YOUR_LAUNCHER_2.sh YOUR_APP2_PARAM_1 YOUR_APP2_PARAM_2 ...
      redirect_stderr=true