I have created a dashboard by using “React js” for Frontend and “Node js” for Backend. Now the project is completed, I need to deploy and host this project on Apache server.
I have tried by running the “npm run build” then copying all the files of build folder to the server but still its not running.
Error: The requested URL /Dashboard was not found on this server
So can anyone help me out with this ? Because I am new to these whole web development stuff.
If anyone tell me what are the configurations needed to make this happen, it will be helpful a lot.
package.json
{
"name": "eclaims",
"version": "0.1.0",
"private": true,
"dependencies": {
"alert-node": "^1.2.4",
"await": "^0.2.6",
"axios": "^0.18.0",
"browser-history": "^1.0.1",
"canvasjs": "^1.8.1",
"chart.js": "^2.8.0",
"cors": "^2.8.5",
"express-redirect": "^1.2.2",
"history": "^4.9.0",
"http-serve": "^1.0.1",
"mdbreact": "^4.14.0",
"oracledb": "^3.1.2",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"react": "^16.8.6",
"react-background-image-loader": "0.0.5",
"react-bootstrap": "^0.32.4",
"react-canvas-js": "^1.0.1",
"react-chartjs-2": "^2.7.6",
"react-dom": "^16.8.6",
"react-dropdown": "^1.6.4",
"react-dropdown-button": "^1.0.11",
"react-native": "^0.59.8",
"react-router": "^5.0.0",
"react-router-bootstrap": "^0.25.0",
"react-router-dom": "^4.3.1",
"react-router-redirect": "^1.0.1",
"react-scripts": "2.1.8",
"react-select": "^2.4.3",
"reactstrap": "^8.0.0",
"redirect": "^0.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
I also tried by creating the “.htaccess” file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
But still no luck.
3
Answers
I’ve followed certain steps from this post from Reddit and I can attest this works for making SPAs run in Apache web server with proper routing.
To quote:
Assuming you’ve run
npm run build
and then copied all the contents inside thebuild
folder to your public directory, this should work.When I first started web dev, I wasn’t really sure I made the VirtualHost config right, so what I usually did was first to make a dummy index.html with, say, Hello World example. When I’ve confirmed that I’m able to see Hello World in the browser (which means I got the VirtualHost config right), I dump the contents of the
build
folder to where the dummy index.html was.My Solution:
Apache:
I am running an Apache Server with multiple React-Apps, that are accessible by Aliases, as e.g.
/abc/app/
orxyz/app/
. For that paths, i adjusted the<Directory>...</Directory>
area inside my virtual hosts config:Additionally, I added the following into
/var/www/abc/.htaccess
according to the React Docs:React Router > 4:
But most importantly I adjusted my
package.json
and.env
files. If you use environment files, you can add the following lines (obv adjusted with your path) to your production or release environment files:Don’t rename PUBLIC_URL. You can change everything after REACT_APP_
Delete the
homepage
line from your package.jsonAlternatively, if you don’t work with
.env
files, you might adjust yourhomepage
line in the package.json like so:In your
index.js
, or where ever you use BrowserRouter or Router, you alter your code like so:React is for front-end. I guess you want to say, apache and nodeJs