DEV Community

Pierangelo
Pierangelo

Posted on

Using NodeJS App with Apache

How setting Apache2 using proxy for expose a NodeJS app.

First install apache2:

apt update

apt install apache2

install nodejs e npm

apt install nodejs

apt install npm

copy your app in /var/www/demo

here a repo to my demo app if you want use it:

https://github.com/pierangelo1982/nodejs-experiment/tree/master/06%20-%20show_hostname

install pm2

npm install -g pm2

we use pm2 for start a daemon of our app, and for enable an autostart in case of reboot of our machine.

from your terminal go into the root folder of the app

cd /var/www/demo

and launch the command:

pm2 start app.js

after that we must config an automatic start in case of reboot:

so launch this command:

pm2 startup systemd

and copy the env that will be print and paste it in the terminal, in my case is this:

sudo env PATH=$PATH:/usr/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u pierangelo — hp /home/pierangelo

now we go to settings apache2
as first things we must enable two modules:

proxy

proxy_http

so from terminal launch this:

a2enmod proxy

a2enmod proxy_http

after that go in /etc/sites/available and create a file named myapp.conf

myapp.conf

<VirtualHost *:80>
 ServerName node.mysite.it 
 ProxyPreserveHost on
 ProxyPass / http://localhost:3000/
 ProxyPassReverse / http://localhost:3000/
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

check carefully, in ProxyPass and ProxyPassReverse you mast show the localhost with the port that you use in your nodejs app.

after that enable your virtualhost:

a2ensite myapp.conf

and restart apache2

service apache2 restart

now you should see your app running on your domain address.

Vedi Tutorial Here

Top comments (0)