Running NodeJS in Apache is currently not possible since extension for Nodejs in Apache doesn’t exist yet unlike mod-perl or mod-php. However, It is possible to run NodeJS as a separate process and set your apache server as a proxy so that your apache server will serve whatever your nodejs application respond.
Why would you like to run Apache and NodeJS together?
One of the reason why you want to do this is because you might have different application that is running on Apache and PHP or Apache and PERL, etc. on the same server as your NodeJS application and you cannot run both Apache and NodeJS on the same port. So the work around would be to run Apache on default http / https port and run your NodeJS application on a custom port.
Assuming you already have installed your Apache Server running on port 80/443 and your NodeJS application is running on a custom port (8080).
First, you need to enable Apache proxy and proxy_http module. To enable it, you have to run this on your Ubuntu terminal:
sudo a2enmod proxy proxy_http
To check if proxy and proxy_http is already enabled, run this command on your terminal:
sudo apachectl -M | grep proxy
Output should be similar to this:
proxy_module (shared) proxy_http_module (shared)
Now that you have enable the necessary modules, all we need to do is set your apache as proxy for your nodejs application.
Configure a virtualhost config file inside /etc/apache2/sites-available directory
<VirtualHost *:80> ServerAdmin firstname.lastname@example.org ServerName yoursite.com ServerAlias www.yoursite.com ProxyRequests off <Proxy *> Order deny,allow Allow from all </Proxy> <Location /> ProxyPass http://localhost:8080/ ProxyPassReverse http://localhost:8080/ </Location> </VirtualHost>
After that, restart your apache using the command below
sudo service apache2 restart
And everything should be working as expected.
If you encounter errors during your configuration, please comment below and we will try to help you fix it as soon as we can 😀