Skip to content

Instantly share code, notes, and snippets.

@moeiscool
Last active March 12, 2023 04:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moeiscool/876e64a9725fa07ac9e6852e1001f1d5 to your computer and use it in GitHub Desktop.
Save moeiscool/876e64a9725fa07ac9e6852e1001f1d5 to your computer and use it in GitHub Desktop.
Run PHP Site in Node.js with Express Web Server and node-php

Installing MariaDB and Creating a user

You must be root to follow the steps in this guide.

  1. Download and Install MariaDB. MariaDB is used in place of MySQL.
sudo apt-get install mariadb-server mariadb-client
  1. Set it up to run on boot.
sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service
  1. Run configuration.
sudo mysql_secure_installation

The choices you should choose are as follows:

- Enter current password for root (enter for none): Just press the Enter
- Set root password? [Y/n]: Y
- New password: Enter password
- Re-enter new password: Repeat password
- Remove anonymous users? [Y/n]: Y
- Disallow root login remotely? [Y/n]: Y
- Remove test database and access to it? [Y/n]:  Y
- Reload privilege tables now? [Y/n]:  Y
  1. Restart MariaDB.
sudo systemctl restart mariadb.service
  1. Enter the MariaDB client.
CREATE USER 'mysqlConnectionUserName'@'localhost' IDENTIFIED BY 'new_password_here';
GRANT ALL ON * TO 'mysqlConnectionUserName'@'localhost' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;
FLUSH PRIVILEGES;
  1. At this point you may want to import your SQL database for any websites you want to use. While still in the MariaDB client you can run the following to import .sql files.
source ./example.sql;
  1. Once done you can leave the client.
EXIT;

Installing a PHP Website using Node.js Express Web Server

You must be root to follow the steps in this guide.

  1. Download and install Node.js and PHP.
curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
sudo apt-get install -y nodejs php
sudo apt install php-fpm php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-intl php-mysql php-cli php-mcrypt php-ldap php-zip php-curl php-cgi
  1. Install Process Manager for Node.js Scripts.
npm install pm2 -g
  1. Create a directory for the website and enter it.
mkdir /home/wordpress
cd /home/wordpress
  1. Download runPhpSite.js into the current directory. You should be in /home/wordpress.
wget https://gist.githubusercontent.com/moeiscool/876e64a9725fa07ac9e6852e1001f1d5/raw/e4a8c72721e48c13a22a6894a56e766481fa5eed/runPhpSite.js -O runPhpSite.js
  1. Install node-php
npm install node-php
  1. Put the PHP website inside a folder called public. The full path should be /home/wordpress/public. Your folder structure should be similar to the following.
- home
| - wordpress
| |- runPhpSite.js
| |- public
  1. Run the website and it should be on port 80.
pm2 start runPhpSite.js
  1. Checking logs for the wbsite can be done by running.
pm2 logs --lines 100
  1. Restarting the webserver can be done by running.
pm2 restart runPhpSite
// Place this file next to a folder named "public"
// "public" should contain your php website. Like Opencart or Wordpress.
var express = require('express');
var php = require("node-php");
var path = require("path");
var app = express();
app.use("/", php.cgi("public"));
app.listen(80);
console.log("Server listening!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment