Set Up a Node js App for a Website With Apache on Ubuntu

Set Up a Node js App for a Website With Apache on Ubuntu

Node.js is a JavaScript runtime environment which lets you easily build server-side applications. This post will explain how to set up a Cloud Server running Ubuntu 16.04 so that Node.js scripts run as a service, and configure the Apache server to make the script accessible from the web.

Install Node.js

Update your server’s packages and install curl with the following commands:

sudo apt-get update
sudo apt-get install curl

Download the Node.js PPA:
curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh

Run the nodesource_setup.sh command to add the PPA to your server’s package cache:

sudo bash nodesource_setup.sh

Note: This script will update the server automatically. There is no need to run apt-get update a second time.

Install Node.js:
sudo apt-get install nodejs

This will automatically install npm as well.

Finally, install the build-essential package for npm:
sudo apt-get install build-essential

After install configure Apache

To access the Node.js script from the web, install the Apache modules proxy and proxy_http with the commands:
sudo a2enmod proxy
sudo a2enmod proxy_http
Once the installation is complete, restart Apache for the changes to take effect:
sudo service apache2 restart
Edit this file with your editor of choice, for example with the command:
sudo nano /etc/apache2/sites-available/example.com.conf

<VirtualHost *:80>
ServerName example.com

   ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full
   <Proxy *>
      Require all granted
   </Proxy>

   <Location /nodejs>
      ProxyPass http://127.0.0.1:8080
      ProxyPassReverse http://127.0.0.1:8080
   </Location>

    <Directory "/var/www/example.com/html">
    AllowOverride All
    </Directory>
</VirtualHost>


sudo services apache2 restart

Lets Get Started your project with professional way

Get in touch and let us know
how we can Help


Contact Us