Report this

What is the reason for this report?

Internet different and Node different

Posted on September 29, 2018

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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

/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).

Did you solve it ? I’ve the same problem.

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.

1. Move Node.js Projects to /var/www/html

The 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.

2. Set Correct Permissions

After moving the projects, you need to adjust the permissions so that the web server and Node.js can access them properly.

  • Set the appropriate ownership for the web server (usually 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

3. Use PM2 to Manage Node.js Projects

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.

1. Move Node.js Projects to /var/www/html

The 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.

2. Set Correct Permissions

After moving the projects, you need to adjust the permissions so that the web server and Node.js can access them properly.

  • Set the appropriate ownership for the web server (usually www-data for Apache/Nginx):

bash Copy code

sudo chown -R www-data:www-data /var/www/html/my-node-app

  • Set the correct permissions:

bash Copy code

sudo chmod -R 755 /var/www/html/my-node-app

3. Use PM2 to Manage Node.js Projects

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.

4. Set Up Apache/Nginx Reverse Proxy (If Needed)

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.

For Apache:

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

For Nginx:

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

5. Use Separate Ports (Optional)

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.

Conclusion:

  • Move your Node.js projects from /root/node_module to /var/www/html.
  • Ensure correct permissions and ownership.
  • Use PM2 to manage your Node.js applications and configure the web server (Apache or Nginx) as a reverse proxy to serve your Node.js apps alongside PHP.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.