DEV Community

Cover image for How to deploy Node server on Apache2
Tikam Singh Alma
Tikam Singh Alma

Posted on • Updated on

How to deploy Node server on Apache2

I'm assuming that you have built an website on React/Next/Angular or Vue.js and looking for deployment process on aws or some cloud.Let's go let's deploy your code on apache2 using PM2.

Tools used here:
Node
Frontend Frameworks
PM2
Apache2

Code and build a website in

  • React
  • Next.js
  • Angular
  • Vue,js

There are two types of website

  • Static
  • Dynamic

Assuming that you know how to create a website, and build and run on local, let's move it to the internet.If don't have any idea about building website comment it I'll guide you to the clouds.

Methods to deploy

  • Netlify
  • Github
  • Gitlab
  • AWS EC2
  • Heroku

Today we will deploy Node server on apache2

Deploy on Apache2

Install PM2

sudo npm install -g pm2
Enter fullscreen mode Exit fullscreen mode

Build your server files:

Assuming that you have code of some website on react.js/next.js/angular.js or Vue.js as far it builds and runs on npm it will fly.

npm run build
Enter fullscreen mode Exit fullscreen mode

Start or Run node server with pm2


pm2 start npm --name "app-name" -- start
Enter fullscreen mode Exit fullscreen mode

Install Apache2 server

sudo apt install apache2
Enter fullscreen mode Exit fullscreen mode

The Apache web server will be operating as a reverse proxy. Requests to it will be proxied to the backend NodeJS applications, managed by PM2. In order for Apache to proxy requests, the following modules must be installed and enabled.

sudo en2mod proxy
sudo a2enmod proxy_http
Enter fullscreen mode Exit fullscreen mode

Restart the apache server

sudo service apache2 restart
Enter fullscreen mode Exit fullscreen mode

Add running node server configuration on apache2

cd into /etc/apache2/site-available

and make file server.conf

cd /etc/apache/site-available

sudo nano domain.conf

OR 

sudo nano /etc/apache2/sites-available/domain.conf
Enter fullscreen mode Exit fullscreen mode

Port 80

<VirtualHost *:80>
    ServerName domain.com
    ServerAlias www.domain.com

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full

    <Proxy *>
        Require all granted
    </Proxy>

    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Port 443 - Secure

<VirtualHost *:443>
    ServerName website.com
    ServerAlias *.website.com
    ProxyPreserveHost on
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
    LogLevel warn
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" **%T/%D**" combined
    ErrorLog /home/ubuntu/website/error.log
    CustomLog /home/ubuntu/website/access.log combined
    SSLEngine on
    SSLCertificateFile /etc/ssl/website.tech.crt
    SSLCertificateKeyFile  /etc/ssl/website.tech.key
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Here :

SSLEngine on

Add your own certificate key-pair

and stream your log file on custom location /home/ubuntu/website/error.log and /home/ubuntu/website/access.log

Enable the newly created configuration

sudo a2ensite domain.conf
Enter fullscreen mode Exit fullscreen mode

Restart the server

sudo service apache2 restart
Enter fullscreen mode Exit fullscreen mode

Go to website.com your custom domain.

Your website is deployed !!!

Top comments (1)

Collapse
 
howtomakeaturn profile image
尤川豪

this article is very helpful!
thanks a lot!