In this article, I am going to walk you through setting up a PHP backend server for a React Application.
To download or browse the code visit the repository via this Github link.
With this setup you will be able to run and develop your React app on your normal Apache localhost rather than node js localhost:3000 which is crazy when you wan’t to force authentication redirects with PHP or using secure cookie as you develop your web app or render some things with PHP before your app starts. I will also add SCSS support and nice advanced stuff.
Alright, now let's get to it!
Requirements: You need Node.js and npm installed on your local machine. As well as a XAMPP, WAMP or any other web server solution.
Setting up the folder structure
Here is the folder structure we will be left with after the dust settles:
First, we need to create the
php-react-app root directory for now. This will hold all of our files. Next we have the
app folder that will contain our entire React application. I added an extra folder
_devapp which hosts my React app code. I’d recommend you do this too if your server backend is not running on Node js. If interested with knowing why i do that please leave a response and i will give you my
WHY.I also have an
assets folder which will contain our compiled react and css. The
node_modules folder is self explanatory, but this will contain all of the NPM packages for our
app.js file. It will be created automatically when we install our NPM.
Getting Started with the Backend
create a new file called