By PrimeXchange
I have installed node.js and few projects of it. But the problem is they all are situated in root/node_module and for internet access it in var/www/html, how to make coordination between these as there will problem as they are in different folder .
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
/var/www/html typically holds client-side files like HTML and JS and they’re served literally like files. /root/node_module is where npm puts your server-side code (logic) and modules that are loaded and processed by nodejs. The two folders don’t need coordination. The coordination is done by JS on the client side in the form of calls to the server (nodejs).
To coordinate between your Node.js projects (currently installed in /root/node_module) and the web server’s document root (/var/www/html), you can manage the projects in a way that allows both Node.js and the web server (like Apache or Nginx) to serve them effectively.
/var/www/htmlThe simplest solution is to move your Node.js projects to the /var/www/html directory, where your web server expects to find them.
First, create a directory for each Node.js project under /var/www/html, and then move your Node.js projects there:
sudo mkdir /var/www/html/my-node-app
sudo mv /root/node_module/my-node-app/* /var/www/html/my-node-app
Repeat this for all your projects.
After moving the projects, you need to adjust the permissions so that the web server and Node.js can access them properly.
www-data for Apache/Nginx):sudo chown -R www-data:www-data /var/www/html/my-node-app
sudo chmod -R 755 /var/www/html/my-node-app
If you’re using PM2 to manage your Node.js applications, ensure that PM2 starts the applications from their new location under /var/www/html/.
Start your project with PM2:
pm2 start /var/www/html/my-node-app/app.js
To coordinate between your Node.js projects (currently installed in /root/node_module) and the web server’s document root (/var/www/html), you can manage the projects in a way that allows both Node.js and the web server (like Apache or Nginx) to serve them effectively.
/var/www/htmlThe simplest solution is to move your Node.js projects to the /var/www/html directory, where your web server expects to find them.
First, create a directory for each Node.js project under /var/www/html, and then move your Node.js projects there:
bash Copy code
sudo mkdir /var/www/html/my-node-app sudo mv /root/node_module/my-node-app/* /var/www/html/my-node-app
Repeat this for all your projects.
After moving the projects, you need to adjust the permissions so that the web server and Node.js can access them properly.
www-data for Apache/Nginx):bash Copy code
sudo chown -R www-data:www-data /var/www/html/my-node-app
bash Copy code
sudo chmod -R 755 /var/www/html/my-node-app
If you’re using PM2 to manage your Node.js applications, ensure that PM2 starts the applications from their new location under /var/www/html/.
Start your project with PM2:
bash Copy code
pm2 start /var/www/html/my-node-app/app.js
This command will start your Node.js project from its new location, ensuring it can be accessed from the correct directory.
If you want to expose your Node.js applications via a web server like Apache or Nginx, you can configure a reverse proxy to route traffic between your web server and the Node.js application.
Edit the Apache configuration file (usually located at /etc/apache2/sites-available/000-default.conf):
<VirtualHost *:80>
ServerName my-node-app.com
# Reverse Proxy to Node.js
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
# PHP settings for other projects
DocumentRoot /var/www/html/
<Directory /var/www/html/>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Enable the necessary Apache modules and restart Apache:
sudo a2enmod proxy proxy_http
sudo systemctl restart apache2
Edit your Nginx configuration file (usually at /etc/nginx/sites-available/default):
server {
listen 80;
server_name my-node-app.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# Serve PHP or static files from /var/www/html
location /php/ {
root /var/www/html;
}
}
Restart Nginx:
sudo systemctl restart nginx
You can run your Node.js applications on different ports (e.g., 3001, 3002, etc.) and configure your reverse proxy to route traffic accordingly. This helps if you’re running multiple Node.js apps on the same server.
/root/node_module to /var/www/html.Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.