Get started with Node.js
Step 1: Create a new Node.js project
-
First, create a new folder to store your Express projects, such as the /opt/bitnami/projects directory, and give write permissions for the current system user:
$ sudo mkdir /opt/bitnami/projects $ sudo chown $USER /opt/bitnami/projects
-
Then, to create a new Express application, initialize a new project as below:
$ cd /opt/bitnami/projects $ express --view pug sample $ cd sample $ npm install
-
The Express application can be started by using this command, and it will run on port 3000:
$ DEBUG=sample:* ./bin/www
To access the application, browse to http://SERVER-IP:3000/. To end the application, terminate the running Express process.
NOTE: Access to the server on port 3000 may be blocked for security reasons. In this case, you must first create an SSH tunnel between your local system and the server before you can access the application. Follow these instructions.
Step 2: Daemonize your application with Forever to keep it running
The next step is to daemonize your application, so that it keeps running in the background.
This example shows how to use Forever, since it is bundled in the Bitnami installation. You can use other service managers such as PM2 or nodemon.
$ forever start /opt/bitnami/projects/sample/bin/www
Step 3: Serve your application through the Apache web server
You can serve your application through the Apache web server by enabling a virtual host that connects to your application. To do that, follow the steps below.
Enable predefined virtual hosts for an Express application
The Bitnami installation comes with predefined HTTP and HTTPS virtual hosts for connecting to a Node.js application running at port 3000. To enable them, follow the steps below:
-
Copy the file to remove the .disabled suffix:
$ sudo cp /opt/bitnami/apache/conf/vhosts/sample-vhost.conf.disabled /opt/bitnami/apache/conf/vhosts/sample-vhost.conf $ sudo cp /opt/bitnami/apache/conf/vhosts/sample-https-vhost.conf.disabled /opt/bitnami/apache/conf/vhosts/sample-https-vhost.conf
-
Restart Apache for the changes to be taken into effect:
$ sudo /opt/bitnami/ctlscript.sh restart apache
Create a custom virtual host
If the predefined virtual hosts are not available to you, or you prefer to apply a custom configuration, follow the steps below:
-
Create and edit the /opt/bitnami/apache/conf/vhosts/myapp-http-vhost.conf file and add the following lines:
<VirtualHost _default_:80> ServerAlias * DocumentRoot "/opt/bitnami/projects/myapp/public" <Directory "/opt/bitnami/projects/myapp/public"> Require all granted </Directory> ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </VirtualHost>
-
Create and edit the /opt/bitnami/apache/conf/vhosts/myapp-https-vhost.conf file and add the following lines:
<VirtualHost _default_:443> ServerAlias * SSLEngine on SSLCertificateFile "/opt/bitnami/apache/conf/bitnami/certs/server.crt" SSLCertificateKeyFile "/opt/bitnami/apache/conf/bitnami/certs/server.key" DocumentRoot "/opt/bitnami/projects/myapp" <Directory "/opt/bitnami/projects/myapp"> Require all granted </Directory> ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </VirtualHost>
-
Restart the Apache server:
$ sudo /opt/bitnami/ctlscript.sh restart apache
Step 4: Create an HTTPS certificate for Apache
To learn how to create an HTTPS certificate for Apache, refer to the Auto-configure a Let’s Encrypt certificate section.