I am using react-router so I want to host in AWS Ec2. How to deploy the app and run permanently in the background or let me know if any other way
By using it you can deploy multiple react applications on the same machine.
What do you need to deploy the application?
1. Nodejs
2. PM2js
3. Koa.js
4. Koa-static.js
You just need to flow this procedure.
npm run build
.Filename: buildStart.js
const httpPort = 80;
const httpsPort = 443;
const koa = require( 'koa' );
const serve = require( 'koa-static' );
const http = require( 'http' );
const https = require( 'https' );
const fs = require( 'fs' );
const app = new koa();
const cert = fs.readFileSync( '/ssl/cert.crt' );
const key = fs.readFileSync( '/ssl/private.key' );
app.use( serve( __dirname + '/build', {
maxage: 365 * 24 * 60 * 60
} ) );
http.createServer( app.callback() ).listen( httpPort, () => console.log( `sever is listening on ${httpPort}` ) );
https.createServer( { cert, key }, app.callback() ).listen( httpsPort, () => console.log( `sever is listening on ${httpsPort}` ) );
The above code will start your react application on both HTTP and HTTPS. if you don't have certificates for https then you can only start it for HTTP.
const httpPort = 80;
const httpsPort = 443;
const koa = require( 'koa' );
const serve = require( 'koa-static' );
const http = require( 'http' );
const https = require( 'https' );
const fs = require( 'fs' );
const app = new koa();
app.use( serve( __dirname + '/build', {
maxage: 365 * 24 * 60 * 60
} ) );
http.createServer( app.callback() ).listen( httpPort, () => console.log( `sever is listening on ${httpPort}` ) );
I'm a little bit new to React, but I wanna add my experience here. I deployed my CRA app on AWS EC2 using Apache.
Now done. Finally, make sure to use HashRouter rather than BrowserRouter. Because EC2 didn't allow the .htaccess file to redirect all requests to index.html, but replacing BrowserRouter with HashRouter helped me solve that issue. Hope this helps others save the time. Thanks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With