07.06.2023

Nginx plus React app on Ubuntu 20.04

Intro

This article will describe you how to prepare the server, install and depoy ReactOS application onto Nginx web-server.

In the Serverspace you can create a server with already installed app "Nginx".

Server preparing

To install React app on your own server you should have:

Installation process

sudo -s
apt install nodejs npm -y
npm i -g npx -y
node -v
npm -v
npx -v

You should see something like that:

npx create-react-app <YOUR_APP_NAME>

Compilation takes much time, so be patient. When the process finished, you will see this:

Web-server install

To work with your app you need any webserver, e.g. Nginx. To install it run:

Then create basic config-file:

cat <<EOF > /etc/nginx/sites-enabled/<DOMAIN_NAME>
server {
listen 80;
root /var/www/<DOMAIN_NAME>;
index index.php index.html;
server_name <DOMAIN_NAME> www.<DOMAIN_NAME>;
location / {
}
}
EOF

Application launch

cd /home/<USERNAME>/<APPNAME>
npm start

You should see something similar to picture below:

Open the URL http://<YOUR_SERVER_IP>:3000 in the browser. In case you see an answer like below - terminate the app via CTRL+C and go to the next step:

cd /home/<USERNAME>/<MYAPP>
npm run build

Wait a little. Expected result looks like:

mkdir /var/www/<DOMAIN_NAME> && cp /home/<USERNAME>/<MYAPP>/build/* /var/www/<DOMAIN_NAME>/
systemctl enable nginx && service nginx restart

Final check

To check the result just open your website in browser:

Conclusion

In this article we discribed how to install NodeJS, create your first app and deploy it into your website with Nginx webserver.