Hello Friends,

Welcome To Infinitbility!

In this Article, we deploy nodejs application subdirectory level in apache ubuntu server. here you get complete tutorial to deploy, setup, and run node js project in apache server.

let’s start with subdirectory.

what is Subdirectory url level?

Some people also say sub url or custom url. subdirectory come after core domain.

Take below example.

suburl, subdir, subdirectory

here https://infinitbility.com is core domain & category is a subdirectory url.

Install Node

here we are installing node version 10. you can also on below curl command.

  • go to root dir on your ubuntu server.
cd ~
  • download node nodesource setup using curl command.
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
  • run downloaded nodesource setup of node.
sudo bash nodesource_setup.sh
  • install nodejs on your server.
sudo apt-get install -y nodejs
  • check node version.
node -v
  • check npm version.
npm -v

Install pm2

Now we will install PM2, which is a process manager for Node.js applications. PM2 provides an easy way to manage and daemonize applications (run them as a service).

We will use Node Packaged Modules (NPM), which is basically a package manager for Node modules that installs with Node.js, to install PM2 on our app server. Use this command to install PM2:

sudo npm install -g pm2

Install Apache2

  • install apache2 on your ubuntu machine.
sudo apt-get install apache2

Next thing we need to proxy all requests incoming on port 80 through the URL of a node.js application to the running local node.js process. For this, we need to install/enable mod_proxy and mod_proxy_http modules on the Apache server :

sudo a2enmod proxy
sudo a2enmod proxy_http

Setup node project

put your node project code on html directory.

  • go to html directory.
cd /var/www/html/

Assuming you guys know to put code on the ubuntu server. ( if not comment on the post we will definitely create a tutorial. )

setup virtual host

  • go to apache conf dir.
cd /etc/apache2/sites-available/
  • you can also create a separate conf file for your project but here explaining with the default conf file.
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example.com
    <Directory />
        Options -Indexes +FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

    # subdirectory is your custom url
    ProxyRequests On
    ProxyPass /subdirectory http://example.com:8080


    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  • enable sites ( when disable )
sudo a2ensite 000-default.conf
  • restart your apache server
sudo service apache2 restart

Run node project and check url

  • go to application folder and run below command
pm2 start server.js

Similarly, we can configure multiple apps using separate ports. The idea is quite simple, we use different ports to configure multiple apps without having to change the original URL.

Looking to setup react aaplication to setup on apache serevr?.

check below link.

https://infinitbility.com/deploy-react-application-on-ubuntu-apache

Thanks for reading…