Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying a create-react-app to Plesk Onyx

This question is not going to be particularly loaded with detail, because the problem is very basic, and pertains to the limitations in how Plesk Onyx implements Node support.

I'm trying to deploy a react app created with create-react-app to a domain hosted on a server with Plesk Onyx installed. I obviously cannot mess around with the core server because I might break other domains. So I need to install this app in a manner that is handled properly by Plesk. The problem I'm having is that I'm not finding a guide for how to do this anywhere.

Plesk requires a project structure that is really quite inflexible, and it's nothing like the structure imposed by create-react-app. For instance, Plesk requires that the document root be a child of the application root, which is the complete inverse of the way create-react-app sets up a project.

I have not shown code here because it's a deployment issue, not a code issue, and involves the structure of projects and how to shove a round peg into a square hole.

like image 560
Thomas W Tupper Avatar asked Jul 08 '18 03:07

Thomas W Tupper


People also ask

How do I use React app in Plesk?

Create a . htaccess file to your public folder in your create-react-app folder. Then build your app using npm run build.


2 Answers

I've had a similar problem but I think I have some kind of a workaround. I haven't actually tried if this works or in Plesk Onyx or not.

Basically I wanted to put a create-react-app to a plesk onyx based hosting and run npm start which eventually leads to react-scripts start, and I realized that Plesk doesn't have the capability to do that. Plesk isn't a VPS...

Instead, Plesk runs a js script file to execute the server, like App.js or server.js and it can be set through the Plesk NodeJS control panel.

To do that, I installed express by running: npm install express --save and made a server.js file which contains this code:

const express = require('express');
const bodyParser = require('body-parser')
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));

app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

app.listen(process.env.PORT || 8080);

I got the code from here: https://dev.to/loujaybee/using-create-react-app-with-express

Then I run npm run build to create a production build, and finally node server.js. My app (a simple tic-tac-toe game from the react tutorial) is available in localhost port 8080.

The root page

I'll give you an update if I did succeed.

like image 94
Valian Masdani Avatar answered Oct 07 '22 00:10

Valian Masdani


I found a really easy was to do this without adding express or using node. This will also make react-router work.

  1. Add the following .htaccess file to your public folder in your create-react-app folder
    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [QSA,L]
  1. Run npm run-script build
  2. FRP the entire contents of the build directory to your Linux server on Plesk
like image 26
Tyler Findlay Avatar answered Oct 07 '22 02:10

Tyler Findlay