Search code examples
wordpressapachereactjswordpress-rest-api

Possible to run React JS and Wordpress on the same web server?


I am using the Wordpress REST API to provide a back end to my React JS front end. Right now both are running on separate Apache servers on AWS and all works well.

Is it possible to run both on the same server? So that my React front end is making requests to Wordpress on the same server?


Solution

  • Sure, if you want to use Apache to handle distributing your frontend and handling the WP backend, read up on Virtual Hosts: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts

    The gist of it is that you'll have a VH for your frontend

    <VirtualHost *:80>
        ServerAdmin admin@example.com
        ServerName example.com
        ServerAlias www.example.com <-- see here
        DocumentRoot /var/www/example.com/client
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    and a VH for your backend

    <VirtualHost *:80>
        ServerAdmin admin@example.com
        ServerName example.com
        ServerAlias api.example.com <-- and here
        DocumentRoot /var/www/example.com/server
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>