Bitnami Node.js Virtual Machine

Description

Node.js is a runtime environment built on V8 JavaScript engine. Its event-driven, non-blocking I/O model enables the development of fast, scalable, and data-intensive server applications.

First steps with the Bitnami Node.js Stack

Welcome to your new Bitnami application! This guide includes some basic information you will need to get started with your application.

How to import a Bitnami Virtual Machine?

Check the following instructions to import a Bitnami Virtual Machine:

Importing a Bitnami Virtual Machine in VirtualBox
  • Select the "File -> Import Appliance" menu option and select the .ova file downloaded from the Bitnami website. Then click "Continue".
  • Once it is imported, click the "Start" button in the VirtualBox toolbar.

For a detailed walkthrough, check our Virtualbox tutorial.

Importing a Bitnami Virtual Machine in VMware
  • Select the "File -> Import" menu option and select the .ova file downloaded from the Bitnami website. Then click "Continue".
  • Once the import is complete, click "Finish" to start the virtual machine.

For a detailed walkthrough, check our VMware tutorial.

What credentials do I need?

  • The server credentials, consisting of an SSH username and password. These credentials allow you to log in to your Virtual Machines server using an SSH client and execute commands on the server using the command line.

What SSH username should I use for secure shell access to my application?

SSH username: bitnami

How do I get my SSH key or password?

You can obtain the SSH password from the virtual machine console when it starts up. Click here for more information.

How to access your application?

Once you have imported your Bitnami Virtual Machine, the IP address for your application is displayed on the virtual machine's login screen. Access the application via your browser by entering this IP address.

Check these instructions about how to remotely access the Bitnami application.

How to start or stop the services?

Each Bitnami stack includes a control script that lets you easily stop, start and restart services. The script is located at /opt/bitnami/ctlscript.sh. Call it without any service name arguments to start all services:

$ sudo /opt/bitnami/ctlscript.sh start

Or use it to restart a single service, such as Apache only, by passing the service name as argument:

$ sudo /opt/bitnami/ctlscript.sh restart apache

Use this script to stop all services:

$ sudo /opt/bitnami/ctlscript.sh stop

Restart the services by running the script without any arguments:

$ sudo /opt/bitnami/ctlscript.sh restart

Obtain a list of available services and operations by running the script without any arguments:

$ sudo /opt/bitnami/ctlscript.sh

What are the default ports?

A port is an endpoint of communication in an operating system that identifies a specific process or a type of service. Bitnami stacks include several services or servers that require a port.

Remember that if you need to open some ports you can follow the instructions given in the FAQ to learn how to open the server ports for remote access.

Port 22 is the default port for SSH connections.

Bitnami opens some ports for the main servers. These are the ports opened by default: 80, 443.

How can I get started with Node.js?

To get started, we suggest the following steps:

Step 1. Create a new Express project.

Install Express by executing the commands below:

$ sudo npm -g install express
$ sudo npm -g install express-generator

Then, create a skeleton project and install dependencies:

$ mkdir -p projects
$ cd projects
$ express sample-application

For more information, refer to these instructions.

Step 2. Deploy your Node.js application with Express.

To start your application server, you just have to execute the following commands, inside your application directory:

$ npm install
$ npm start

If you are using a recent Express version (greater than 4.0), you should use the following command instead:

$ ./bin/www

For more information, refer to these instructions.

Step 3. Install modules with npm.

To install modules locally, you just have to execute this command:

$ npm install PACKAGE

To install modules globally, use:

$ npm -g install PACKAGE

We recommend installing packages in both local and global modes, so that you can share installed commands and still protect the dependencies of your project.

For more information, refer to these instructions.

How to connect to Node.js from a different machine?

For security reasons, the Node.js ports in this solution cannot be accessed over a public IP address. To connect to Node.js from a different machine, you must open port 80, 443 for remote access. Refer to the FAQ for more information on this.

IMPORTANT: Making this application's network ports public is a significant security risk. You are strongly advised to only allow access to those ports from trusted networks. If, for development purposes, you need to access from outside of a trusted network, please do not allow access to those ports via a public IP address. Instead, use a secure channel such as a VPN or an SSH tunnel. Follow these instructions to remotely connect safely and reliably.

How to reset the MariaDB root password?

Please note that depending on the version you have installed, you may find the MariaDB files at /opt/bitnami/mysql

If you don't remember your MariaDB root password, you can follow the steps below to reset it to a new value:

  • Create a file in /home/bitnami/mysql-init with the content shown below (replace NEW_PASSWORD with the password you wish to use):

     UPDATE mysql.user SET Password=PASSWORD('NEW_PASSWORD') WHERE User='root';
     FLUSH PRIVILEGES;
    
  • Stop the MariaDB server:

     $ sudo /opt/bitnami/ctlscript.sh stop mariadb
    
  • Start MariaDB with the following command:

     $ sudo /opt/bitnami/mariadb/bin/mysqld_safe --defaults-file=/opt/bitnami/mariadb/my.cnf --pid-file=/opt/bitnami/mariadb/data/mysqld.pid --init-file=/home/bitnami/mysql-init 2> /dev/null &
    
  • Restart the MariaDB server:

     $ sudo /opt/bitnami/ctlscript.sh restart mariadb
    
  • Remove the init script

     $ rm /home/bitnami/mysql-init
    

How to change the MariaDB root password?

You can modify the MariaDB password using the following command at the shell prompt:

$ /opt/bitnami/mariadb/bin/mysqladmin -p -u root password NEW_PASSWORD

How to create an SSL certificate?

OpenSSL is required to create an SSL certificate. A certificate request can then be sent to a certificate authority (CA) to get it signed into a certificate, or if you have your own certificate authority, you may sign it yourself, or you can use a self-signed certificate (because you just want a test certificate or because you are setting up your own CA).

Follow the steps below:

  • Generate a new private key:

     $ sudo openssl genrsa -out /opt/bitnami/apache2/conf/server.key 2048
    
  • Create a certificate:

     $ sudo openssl req -new -key /opt/bitnami/apache2/conf/server.key -out /opt/bitnami/apache2/conf/cert.csr
    
    IMPORTANT: Enter the server domain name when the above command asks for the "Common Name".
  • Send cert.csr to the certificate authority. When the certificate authority completes their checks (and probably received payment from you), they will hand over your new certificate to you.

  • Until the certificate is received, create a temporary self-signed certificate:

     $ sudo openssl x509 -in /opt/bitnami/apache2/conf/cert.csr -out /opt/bitnami/apache2/conf/server.crt -req -signkey /opt/bitnami/apache2/conf/server.key -days 365
    
  • Back up your private key in a safe location after generating a password-protected version as follows:

     $ sudo openssl rsa -des3 -in /opt/bitnami/apache2/conf/server.key -out privkey.pem
    

    Note that if you use this encrypted key in the Apache configuration file, it will be necessary to enter the password manually every time Apache starts. Regenerate the key without password protection from this file as follows:

     $ sudo openssl rsa -in privkey.pem -out /opt/bitnami/apache2/conf/server.key
    

Find more information about certificates at http://www.openssl.org.

How to enable HTTPS support with SSL certificates?

NOTE: The steps below assume that you are using a custom domain name and that you have already configured the custom domain name to point to your cloud server.

Bitnami images come with SSL support already pre-configured and with a dummy certificate in place. Although this dummy certificate is fine for testing and development purposes, you will usually want to use a valid SSL certificate for production use. You can either generate this on your own (explained here) or you can purchase one from a commercial certificate authority.

Once you obtain the certificate and certificate key files, you will need to update your server to use them. Follow these steps to activate SSL support:

  • Use the table below to identify the correct locations for your certificate and configuration files.

    Variable Value
    Current application URL https://[custom-domain]/
      Example: https://my-domain.com/ or https://my-domain.com/appname
    Apache configuration file /opt/bitnami/apache2/conf/bitnami/bitnami.conf
    Certificate file /opt/bitnami/apache2/conf/server.crt
    Certificate key file /opt/bitnami/apache2/conf/server.key
    CA certificate bundle file (if present) /opt/bitnami/apache2/conf/server-ca.crt
  • Copy your SSL certificate and certificate key file to the specified locations.

    NOTE: If you use different names for your certificate and key files, you should reconfigure the SSLCertificateFile and SSLCertificateKeyFile directives in the corresponding Apache configuration file to reflect the correct file names.
  • If your certificate authority has also provided you with a PEM-encoded Certificate Authority (CA) bundle, you must copy it to the correct location in the previous table. Then, modify the Apache configuration file to include the following line below the SSLCertificateKeyFile directive. Choose the correct directive based on your scenario and Apache version:

    Variable Value
    Apache configuration file /opt/bitnami/apache2/conf/bitnami/bitnami.conf
    Directive to include (Apache v2.4.8+) SSLCACertificateFile "/opt/bitnami/apache2/conf/server-ca.crt"
    Directive to include (Apache < v2.4.8) SSLCertificateChainFile "/opt/bitnami/apache2/conf/server-ca.crt"
    NOTE: If you use a different name for your CA certificate bundle, you should reconfigure the SSLCertificateChainFile or SSLCACertificateFile directives in the corresponding Apache configuration file to reflect the correct file name.
  • Once you have copied all the server certificate files, you may make them readable by the root user only with the following commands:

     $ sudo chown root:root /opt/bitnami/apache2/conf/server*
    
     $ sudo chmod 600 /opt/bitnami/apache2/conf/server*
    
  • Open port 443 in the server firewall. Refer to the FAQ for more information.

  • Restart the Apache server.

You should now be able to access your application using an HTTPS URL.

How can I run a command in the Bitnami Node.js Stack?

Log in to the server console as the bitnami user and run the command as usual. The required environment is automatically loaded for the bitnami user.

How to create a custom Node.js application?

Many users run a Bitnami stack as a development environment for their own projects (as opposed to running third-party applications). To create a custom Node.js application from scratch, follow the steps below. These steps assume that your application will live in the /opt/bitnami/apps/myapp/ directory:

  • Run the following commands to create the directories:

    $ sudo mkdir -p /opt/bitnami/apps/myapp
    $ sudo mkdir /opt/bitnami/apps/myapp/conf
    $ sudo mkdir /opt/bitnami/apps/myapp/htdocs
    
  • Create and edit the /opt/bitnami/apps/myapp/conf/httpd-prefix.conf file and add the line below to it:

    Include "/opt/bitnami/apps/myapp/conf/httpd-app.conf"
    
  • Create and edit the /opt/bitnami/apps/myapp/conf/httpd-app.conf file and add the content below to it. This is the main configuration file for your application, so modify it further depending on your application's requirements.

    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
    
    NOTE: 3000 is the default port for the Express server. If you have customized your application to use a different port, change it here as well.
  • Once you have created the files and directories above, add the following line to the end of the main Apache configuration file at /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf, as shown below:

    Include "/opt/bitnami/apps/myapp/conf/httpd-prefix.conf"
    
  • Restart the Apache server:

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    
  • Start the Express server:

    $ cd /opt/bitnami/apps/myapp/htdocs
    $ ./bin/www
    

    Alternatively, use the following command to start the server and keep it running even after your server session ends. Replace FILE with the correct filename for your application.

    $ forever start FILE.js
    

Although your application is now available, you may not be able to access it immediately. This is because the Express server runs on port 3000 by default, and Bitnami stacks on some platforms have this port closed for security reasons. To access the application, you will need to create an SSH tunnel to the port.

How to create a project using Express?

Express is a high-performance Web development framework for Node.js inspired by Sinatra.

  • Installing it is as simple as executing the commands below:

     $ sudo npm -g install express
     $ sudo npm -g install express-generator
    
  • Create a skeleton project by executing these commands. This will produce a new application in the projects/sample-application directory:

     $ mkdir -p projects
     $ cd projects
     $ express sample-application
    
  • Install dependencies using npm:

     $ cd sample-application
     $ npm install
    

This will download and install the required dependencies into a node_modules directory under your project folder.

How to start the Express web application?

To start your application server, you just have to execute the following command, inside your application directory:

$ npm start

Alternatively, use the following command to start the server and keep it running even after your server session ends. Replace FILE with the correct filename for your application.

$ forever start FILE.js

If you are using a recent Express version (greater than 4.0), you should use the following command instead:

$ ./bin/www

Port 3000 is the default port used by Express when creating the template. This means that you may receive an error similar to the below when starting the server:

Error: listen EADDRINUSE
 at errnoException (net.js:670:11)
 at Array.0 (net.js:771:26)
 at EventEmitter._tickCallback (node.js:190:38)

This means that the port is already in use. Edit the app.js script and change the line containing app.listen() to use a different port number, as below. Then, start the application again.

app.listen(4000, function(){

How to install Node.js applications?

NOTE: To avoid a conflict between two or more Node.js applications attempting to use the same port, modify the application settings as needed to use a different port for each application.

To install a Node.js application, first ensure that all dependences are satisfied. Many applications will require Node.js and MongoDB, but others may require Redis or MySQL. To check available services, run this command:

$ sudo /opt/bitnami/ctlscript.sh status

To install an application in the example/ directory, follow these steps:

  • Create the directory:

     $ sudo mkdir /opt/bitnami/apps/example
    
  • Move the application files to that directory and follow the official installation steps.

How to enable CORS?

Edit the Apache configuration file at /opt/bitnami/apache2/conf/bitnami/bitnami.conf and add the following directive to the end of the file. Replace the DOMAIN placeholder with the domain name that you wish to permit requests from.

<IfModule headers_module>
      Header set Access-Control-Allow-Origin "DOMAIN"
</IfModule>

Then, restart your Apache server by executing the command below:

$ sudo /opt/bitnami/ctlscript.sh restart apache

How to connect your Node.js application to an Oracle Database?

In order to connect your Node.js application to an Oracle Database, you need to install the following packages.

  • libaio
  • Oracle Instant Client (and SDK)
  • oracledb Node.js module

Step 1: Install libaio

  • Execute the following command:

    $ sudo apt-get install libaio1
    

Step 2: Install Oracle Instant Client and Oracle Instant Client SDK

  • Download both instantclient-basic-linux.x64.zip and instantclient-sdk-linux.x64.zip. The version will depend on the Oracle Database you have running. For instance, for Oracle Database 12c you would download instantclient-basic-linux.x64-12.1.0.2.0.zip and instantclient-sdk-linux.x64-12.1.0.2.0.zip (we will use them in the rest of the examples).

  • Uncompress instantclient-basic-linux.x64.zip by executing the following command:

     $ unzip instantclient-basic-linux.x64-12.1.0.2.0.zip
    
  • Copy the extracted contents to /opt/bitnami/common/lib:

     $ sudo cp -r instantclient_12/* /opt/bitnami/common/lib
    
  • Execute the following command:

     $ sudo ln -s /opt/bitnami/common/lib/libclntsh.so.12.1 /opt/bitnami/common/lib/libclntsh.so
    
  • Extract instantclient-sdk-linux.x64-12.1.0.2.0.zip:

     $ unzip instantclient-sdk-linux.x64-12.1.0.2.0.zip
    
  • Copy the contents of the sdk subfolder to /opt/bitnami/common/include:

     $ sudo cp -r instantclient_12/sdk/include/* /opt/bitnami/common/include
    

Step 3: Install OracleDB Node.js module

  • Before installing, set the following environment variables:

     $   export OCI_LIB_DIR=/opt/bitnami/common/lib
     $   export OCI_INC_DIR=/opt/bitnami/common/include
    
  • In your Node.js application folder, execute the following command:

     $   npm install oracledb
    

Step 4: Test the connection

In order to test that the plugin was installed correctly, execute one of the examples detailed in the OracleDB Node.js module documentation.

  • Download this example from the OracleDB Node.js module website.

  • In the same folder where connect.js was downloaded, create the file dbconfig.js with the following contents (remembering to replace the DATABASE_USER, DATABASE_PASSWORD and DATABASE_CONNECTION_STRING placeholders with the correct values):

     module.exports = {
          user          : "DATABASE_USER",
          password      : "DATABASE_PASSWORD",
          connectString : "DATABASE_CONNECTION_STRING"
     };
    

    For example:

     module.exports = {
        user          : "SYSTEM",
        password      : "mypassword",
        connectString : "127.0.0.1:1521/PDB1.oracleuser.oraclecloud.internal" 
     };
    
  • Run the testing program:

    $ node example.js
    

If everything was correct, you should see the message "Connection was successful!"

How to install modules?

Node.js comes with the npm package manager. Using it, you can install, uninstall and search for Node.js modules.

npm installs modules in two different scopes: local and global.

Local Installation

The local installation is recommended, as the size of the packages is usually small. It installs the packages in a node_modules subdirectory of the current working directory, usually a project folder. This ensures all the required dependencies are installed and won't break when installing other packages in a different project or locally.

To install modules locally, you just have to execute this command:

$ npm install PACKAGE

For example, to install MySQL, you would execute:

$ npm install mysql

Global Installation

In other cases (typically when the module installs a command line tool), it is more convenient to install the packages globally. In these cases, the command varies slightly:

$ sudo npm -g install PACKAGE

For example, to install Express, you would execute:

$ sudo npm -g install express

This will make the Express command-line available in all projects.

We recommend installing packages in both local and global modes, so that you can share installed commands and still protect the dependencies of your project.

How to list the installed modules?

To list the modules installed locally in a project, enter the project directory and execute the npm list command, as shown in the example below.

$ cd sample-application
$ npm list
  application-name@0.0.1 /home/bitnami/projects/sample-application
+-- express@2.5.8
| +-- connect@1.8.7
| | +-- formidable@1.0.9
| +-- mime@1.2.4
| +-- mkdirp@0.3.0
| +-- qs@0.4.2
+-- jade@0.26.1
  +-- commander@0.5.2
  +-- mkdirp@0.3.0

You can also list the globally installed packages by entering the project directory and executing the npm -g list command, as shown below:

$ cd sample-application
$ npm -g list
  /opt/bitnami/nodejs/lib
+-- coffee-script@1.3.3
+-- colors@0.6.0-1
...
+-- express@2.5.9
| +-- connect@1.8.7
| +-- qs@0.4.2
| +-- cliff@0.1.7
...
| | +-- colors@0.6.0-1
+-- jade@0.26.0
| +-- commander@0.5.2
| +-- mkdirp@0.3.0
+-- node-dev@0.2.3
+-- npm@1.1.21
| +-- abbrev@1.0.3
| +-- node-gyp@0.4.1
| | +-- ansi@0.0.4
| +-- uid-number@0.0.3
| +-- which@1.0.5
+-- uglify-js@1.2.6

How to upgrade npm?

You can upgrade npm to the latest version with the following command:

$ npm -g install npm

How to create a full backup of Node.js?

Backup

The Bitnami Node.js Stack is self-contained and the simplest option for performing a backup is to copy or compress the Bitnami stack installation directory. To do so in a safe manner, you will need to stop all servers, so this method may not be appropriate if you have people accessing the application continuously.

Follow these steps:

  • Change to the directory in which you wish to save your backup:

      $ cd /your/directory
    
  • Stop all servers:

      $ sudo /opt/bitnami/ctlscript.sh stop
    
  • Create a compressed file with the stack contents:

      $ sudo tar -pczvf application-backup.tar.gz /opt/bitnami
    
  • Restart all servers:

      $ sudo /opt/bitnami/ctlscript.sh start
    

You should now download or transfer the application-backup.tar.gz file to a safe location.

Restore

Follow these steps:

  • Change to the directory containing your backup:

      $ cd /your/directory
    
  • Stop all servers:

      $ sudo /opt/bitnami/ctlscript.sh stop
    
  • Move the current stack to a different location:

      $ sudo mv /opt/bitnami /tmp/bitnami-backup
    
  • Uncompress the backup file to the original directoryv

      $ sudo tar -pxzvf application-backup.tar.gz -C /
    
  • Start all servers:

      $ sudo /opt/bitnami/ctlscript.sh start
    

If you want to create only a database backup, refer to these instructions for MySQL and PostgreSQL.

virtualMachine

Bitnami Documentation