Report this

What is the reason for this report?

How should I set up a web app which uses node.js and apachi (PHP)

Posted on December 4, 2018

Hello,

I am very new to deployment. I have been a learning developer for the last 3 months and I am ready to deploy my first project.

It is a simple web app which uses node.js and php. However I am not sure how I should set it app up. I have a few concerns:

  1. Which Linux distro should I use - for now I am going for Ubuntu since it seems the most popular ?

  2. When I get a droplet in Digital Ocean should it be partitioned somehow or should I install node.js with pm2 and apache all on the same machine ?

  3. When sending the files to the served should the PHP part be kept separate from the JS part of the application ? Or should I deploy the whole project in one directory ?



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.

Hi @kkrumov

  1. I prefer to use Ubuntu server for my projects so you can use it too, it is popular and has enough documentation and guides to work with it.

  2. You can install pm2 and apache on the same droplets, that is okay.

  3. NodeJS and PHP are both for backend you are using them both? How is your project structed in Development if they are together then you can keep them together on the server.

Hope this helps

1. Which Linux Distro to Use

Ubuntu is a great choice, especially for beginners. It’s widely used, has excellent documentation, and most cloud providers (like DigitalOcean) have plenty of tutorials and support for Ubuntu. I recommend Ubuntu 22.04 LTS, as it’s stable and has long-term support.

2. Should the Droplet Be Partitioned?

For a simple web app, partitioning isn’t necessary, especially when you’re starting out. You can install both Node.js (with PM2) and Apache on the same machine. This setup will work fine for smaller projects.

However, if your project grows in complexity later, you might consider separating different services (Node.js, PHP, databases) across multiple droplets or using containers like Docker. For now, keeping everything on one server is perfectly fine.

3. How to Organize PHP and Node.js Files

There are two ways you can organize the PHP and Node.js parts of your application:

Option 1: Separate Directories for PHP and Node.js

You can structure your project so that PHP and Node.js live in separate directories:

  • PHP part (served by Apache): Place the PHP files in the Apache web root (e.g., /var/www/html/php-app).
  • Node.js part (served by Node/PM2): Place the Node.js app in a separate directory (e.g., /var/www/node-app).

You would then configure Apache as a reverse proxy to serve both PHP and Node.js. Requests to /php would be handled by Apache, and requests to /node or the API would be forwarded to the Node.js server.

Option 2: Single Directory

Alternatively, you could deploy both parts of the project in the same directory if they’re tightly integrated. For example, you might have the following structure:

/var/www/my-project
    ├── public/        # Public files (JS, CSS, HTML)
    ├── php/           # PHP scripts
    └── node/          # Node.js app

In this setup, you would still use Apache to serve the PHP files, and Node.js would handle its own routes. Apache could also act as a reverse proxy to forward requests to the Node.js part if needed.

Deployment Steps

Here’s a simplified deployment approach for your use case:

  1. Create a DigitalOcean Droplet with Ubuntu 24.04 LTS.

  2. Update your droplet, install Apache, Nodejs and PM2:

sudo apt update && sudo apt upgrade -y
sudo apt install apache2 -y
sudo apt install php libapache2-mod-php -y
sudo apt install php libapache2-mod-php -y
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
sudo npm install pm2@latest -g
  1. Set up Reverse Proxy (if you’re separating PHP and Node.js): In the Apache config file (/etc/apache2/sites-available/000-default.conf), add the following lines to proxy requests to Node.js:
<VirtualHost *:80>
    ServerName yourdomain.com

    # Proxy requests to Node.js
    ProxyPass /node http://127.0.0.1:3000/
    ProxyPassReverse /node http://127.0.0.1:3000/

    # PHP settings
    DocumentRoot /var/www/html/php-app
    <Directory /var/www/html/php-app>
        AllowOverride All
    </Directory>
</VirtualHost>

Enable the necessary modules:

sudo a2enmod proxy proxy_http
sudo systemctl restart apache2
  1. Deploy the App:
  • PHP part: Place your PHP files in /var/www/html/php-app/.
  • Node.js part: Start your Node.js app with PM2:
pm2 start /var/www/node-app/app.js
  1. Enable SSL with Let’s Encrypt: Use Certbot to enable SSL for both PHP and Node.js:
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache

Once done, you’ll have a setup where PHP and Node.js run together on the same server and domain. Requests can be routed appropriately between Apache and Node.js.

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.