Report this

What is the reason for this report?

How to host a php and node application

Posted on September 24, 2020

Hey guys, I have never hosted a node application before but i have this website written in PHP and node. I used node and vue to create the admin panel page and the booking process. Is it possible to host this website with PHP and node



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 there @pujeremy27,

What you could do is use Nginx as a reverse proxy for your Node application and also use PHP FPM with Nginx for your PHP application.

What you could do is follow the steps here on how to setup Nginx with PHP-FPM:

https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-ubuntu-18-04

And also you can follow this article on how to prepare your Node JS for production with Nginx:

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

Hope that this helps! Regards, Bobby

Yes, you can host a website that uses both PHP and Node.js on the same server. This setup is common when parts of a website (like an admin panel or specific services like a booking process) are built using Node.js, while the main website runs on a traditional LAMP stack (Linux, Apache, MySQL, PHP).

Here’s a step-by-step guide on how you can set up a server to host both PHP and Node.js applications:

Step 1: Choose Your Server

You can use any VPS providers like DigitalOcean, AWS, Linode, or others. These platforms support both Node.js and PHP.

Step 2: Set Up the Server

You’ll likely be using a Linux server. Here’s what you need to install:

For PHP:

  1. Install Apache or Nginx as your web server. Apache is commonly used with PHP but Nginx can also serve PHP via PHP-FPM.
sudo apt update
sudo apt install apache2 libapache2-mod-php
# or for Nginx
sudo apt install nginx php-fpm
  1. Install PHP and any necessary PHP extensions:
sudo apt install php php-cli php-common php-mysql php-gd php-mbstring

For Node.js:

  1. Install Node.js: You can install Node.js from the official repositories, or use a version manager like nvm for more flexibility.
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install -y nodejs
  1. (Replace 14.x with the version of Node.js you need.)

  2. Set up your Node application: Place your Node.js application in a suitable directory, like /var/www/nodeapp.

Step 3: Configure the Web Server

You’ll need to configure your web server to handle both PHP and Node.js.

For Apache:

  1. Proxy requests to Node.js: Use mod_proxy and mod_proxy_http to forward requests to your Node.js application.
<VirtualHost *:80>
    ServerName yourdomain.com

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full
    <Proxy *>
        Require all granted
    </Proxy>

    <Location /nodeapp>
        ProxyPass http://127.0.0.1:3000
        ProxyPassReverse http://127.0.0.1:3000
    </Location>
</VirtualHost>

Ensure your Node app listens on the specified port (3000 in this case).

For Nginx:

  1. Configure a reverse proxy for Node.js alongside PHP:
server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://localhost:3000; # Node.js app
        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;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Ensure the PHP version is correct
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Step 4: Deploy and Run Your Applications

  • Run your Node.js app: Navigate to your Node app directory, install dependencies with npm install, and start your app (e.g., using node app.js, npm start, or a process manager like pm2).
  • Deploy your PHP site in the web server’s root directory (e.g., /var/www/html).

Step 5: Ensure Everything Starts on Boot

Use process managers like systemd or pm2 for Node.js to ensure your apps restart automatically if the server reboots.

Step 6: Test Your Setup

Visit your domain and check both the PHP site and Node.js applications to ensure they are working correctly.

By following these steps, you should be able to host both PHP and Node.js applications on a single server efficiently.

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.