Very new to server stuff, sorry if this may be a noob question.
Problem
I am trying to run React and Node/express on a server. React does work and when I start my node app via npm start
the server actually works too, meaning: Opening the website on IP w.x.y.z shows my website and using a form submit will reach the node backend. When I try to use PM2 to run my node app it does not work. PM2 will start my application but I can't reach it, neither through the website nor via curl localhost/api/...
.
Working example with npm start
Output of starting my node app with npm start:
Express server listening on port 3001
Then using curl I receive an expected message from the backend:
curl localhost/api/users
-> {message: "blabla"}
Failing example with PM2 start and status
With npm start
currently not running I start the application with the following command:
sudo pm2 start app.js -- start
which then shows me it's online:
ââââââ¬âââââââââââââââââââââ¬âââââââââââ¬âââââââ¬ââââââââââââ¬âââââââââââ¬âââââââââââ
â id â name â mode â ⺠â status â cpu â memory â
ââââââ¼âââââââââââââââââââââ¼âââââââââââ¼âââââââ¼ââââââââââââ¼âââââââââââ¼âââââââââââ¤
â 0 â app â fork â 0 â online â 0% â 10.5mb â
ââââââ´âââââââââââââââââââââ´âââââââââââ´âââââââ´ââââââââââââ´âââââââââââ´âââââââââââ
But now using curl I receive a 503 server error:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>503 Service Unavailable</title>
</head><body>
<h1>Service Unavailable</h1>
<p>The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.</p>
<hr>
<address>Apache/2.4.48 (Ubuntu) Server at localhost Port 80</address>
</body></html>
Server at localhost Port 80
already made me wonder whether this indicates a problem but let's move on.
File structure
Static react files -> /var/www/html/client/build/
Node stuff -> /var/www/html/server/
Apache config
The config was changed to point to the build folder of the react app, also allowing the react router to work properly.
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/client/build/
# following is used for React Router to work properly
<Directory "/var/www/html/client/build/">
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html [L]
</Directory>
# following is what I thought is the right way to add the possibility to talk to the nodejs backend
# ServerName localhost
ProxyPreserveHost on
ProxyPass /api http://localhost:3001/api
ProxyPassReverse /api http://localhost:3001/api
...
</VirtualHost>
Netstat output
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::80 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
UFW status output
To Action From
-- ------ ----
22 ALLOW Anywhere
80 ALLOW Anywhere
443 ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
8080 ALLOW Anywhere
3001 ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
80 (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
8080 (v6) ALLOW Anywhere (v6)
3001 (v6) ALLOW Anywhere (v6)
What have I tried so far
I looked at several setup examples for PM2 which are often just the basic starting of an app and don't go in too deep or don't have the combination with React.
I also tried to change the apache ProxyPass setting, adding the 3001 port to the allowed ports in UFW (does this make any sense when using the proxypass?).
Not sure whether there is a concept I haven't understood at this point or if I am still missing something. Or maybe just have a wrong configuration at this point.
Any help is greatly appreciated!