Simple Web Server (NodeJs + Express) + Apache2
How to Setting Apache2 using proxy for expose a Nodejs + Express Framework app.
Configuring Apache2 and NodeJS
First, Install apache2 on local (use Linux Ubuntu) or Ubuntu on WSL (Windows):
sudo apt-get updatesudo apt-get install apache2
You can check whether apache2 is active or not by using the command:
sudo systemctl status apache2
And now u can check on ur browser with http://127.0.0.1
Install NodeJs and npm on local:
sudo apt install nodejssudo apt intall npm
Now u can copy your app in directory /var/www/
cd /var/www
Here a repository to my web server app if you want use it:
U can git clone the repository in /var/www
git clone https://github.com/dandiirwanto20/web-server-dengan-configurasi-apache2
you can see the new folder with the name web-server-dengan-configurasi-apache2. Then go into the folder with the command:
cd /web-server-dengan-configurasi-apache2
Now you can run NodeJs app by using command:
npm intallnpm run start
In the command line will display the server is running on the server 0.0.0.0:8000
You can check it on a web browser and it will display the command: “Selamat Datang di Web Server Sederhana”
Configuring Apache for Node.js
We’ll reconfigure the Apache server to listen on port 80
and redirect all requests to the Node application running on port 8000
.
For ProxyPass
to work, we must enable the proxy
and proxy_http
modules that act as gateways to allow for the passing of the request.
First u can Enabling the proxy
and proxy_http
modules:
sudo a2enmod proxysudo a2enmod proxy_http
and now u can configure 000-default.conf file in sites-available folder by using command:
cd .. #to back in root directorycd /etc/apache2/000-default.conf
access files to edit by using commands:
sudo nano 000-default.conf
or
sudo vim 000-default.conf
And now you can add some commands in the file:
ProxyPreserveHost on
ProxyPass / http://localhost:8000/
ProxyPassReverse / http://localhost:8000/
For applying the configuration you can use the command:
sudo systemctl restart apache2
Testing the configuration
To test the configuration is successful or not you can enter the application directory that you save:
cd ..cd /var/www/web-server-dengan-configurasi-apache2
And run the command:
npm installnpm run start
Then typing http://127.0.0.1 in web browser and NodeJs app with Apache2 configured successfully.
Note: If u use Windows, I recommend using VirtualBox or VMware for easier & useful configuration.
Configuration with different ports
We can set up apache with a different port. we can do this by configuring the ports.conf file in the apache2 folder. in this case, we will change port 80 to 3000.
First go to the apache2 folder: (make sure it’s in the root folder)
cd /etc/apache2ls
Then open the file with the command:
sudo nano ports.conf
then edit listen 80 to listen 3000
Don’t forget to edit the 000-default.conf file in the <VirtualHost *:80> section to <VirtualHost *:3000>
lastly type the command “sudo systemctl restart apache2” to activate the new configuration.
now you should see your app running on your domain address.
http://127.0.0.1:3000
Thank You!