DEV Community

Cover image for Combine Node.js and WordPress Under One Domain
Kolawole Yusuf
Kolawole Yusuf

Posted on

Combine Node.js and WordPress Under One Domain

I have been tinkering with a website that merges a custom Node.js application with a WordPress blog, and I'm excited to share my journey.

After experimenting with various hosting configurations, I discovered a straightforward approach to creating a seamless online presence using Nginx on AlmaLinux.

Important Note: Throughout this guide, replace example.com with your actual domain name. For instance, if your domain is mydomain.com, you'll substitute all instances of example.com with mydomain.com.

My goal was simple, build a setup where my main website runs on Node.js while maintaining a WordPress blog, all without sacrificing performance. For example, my primary domain example.com is powered by Node.js, while my blog lives at example.com/blog. Whether you are a developer looking to craft a unique web presence or someone eager to blend different technologies, this method has been a game-changer for me, offering consistent URL structure that helps search engines understand site hierarchy and provides significant SEO advantages.

The real beauty of this setup is its adaptability. While I am using AlmaLinux, the core principles can be applied to virtually any Linux distribution you are comfortable with. It's all about finding that perfect balance between your custom application and content management needs.

Before getting started, ensure you have the following:

  1. AlmaLinux 8 or 9
  2. A server running Nginx
  3. Basic Node.js application setup
  4. WordPress setup
  5. Familiarity with Nginx configurations

Step 1: Prepare your server
You can install Nginx on AlmaLinux by first updating your system and installing the necessary dependencies using the following commands:

sudo dnf update -y
sudo dnf install -y epel-release
sudo dnf install -y nodejs npm nginx
Enter fullscreen mode Exit fullscreen mode

Step 2: You can create a simple app using Express

mkdir my-node-app
cd my-node-app
npm init -y
npm install express //Install Express (or your preferred Node.js framework)
Enter fullscreen mode Exit fullscreen mode

Create basic Node.js application

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Welcome to my website!');
});

app.listen(port, () => {
  console.log(`Node.js app running on port ${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Use PM2 for process management

sudo npm install -g pm2
pm2 start app.js
pm2 startup systemd
Enter fullscreen mode Exit fullscreen mode

Step 3: WordPress installation
Download and extract WordPress

cd /var/www/example.com
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
mv wordpress blog
Enter fullscreen mode Exit fullscreen mode

Step 4: Nginx configuration
Create /etc/nginx/conf.d/example.com.conf

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com;

    # Node.js application (root domain)
    location / {
        proxy_pass http://localhost: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;
    }

    # WordPress blog
    location /blog {
        alias /var/www/example.com/blog;
        try_files $uri $uri/ /blog/index.php?$args;

        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm/www.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Security and permissions

sudo chown -R nginx:nginx /var/www/example.com
sudo chmod -R 755 /var/www/example.com
Enter fullscreen mode Exit fullscreen mode

Step 6: Start services

sudo systemctl enable nginx
sudo systemctl enable pm2-root
sudo systemctl start nginx
pm2 startup
pm2 save
Enter fullscreen mode Exit fullscreen mode

Conclusion
You now have a Node.js website operating as the primary site and a WordPress blog located at /blog within the same domain using Nginx. This setup provides a solution where each application can function separately while utilizing the same domain name, for streamlined content and web application management and deployment.

Additional recommendations

  • Implement SSL with Let's Encrypt
  • Set up regular backups

Top comments (0)