This guide provides step-by-step instructions to deploy and run a This React application on an Ubuntu VM using Nginx, making it accessible from a public IP.
Launch an EC2 instance on Amazon. Select the ubuntu VM and in Security Groups make sure to allow HTTP and SSH. Create a key pair using pem and save it.
Navigate back to your instances and select the EC2 instance and hit connect. Navigate to SSH and follow the instructions on your command shell
Since a React application requires both Node.js and npm, install them first:
sudo apt update
sudo apt install -y nodejs npmTo verify the installation:
node -v
npm -vUpdate package lists and install Nginx:
sudo apt install -y nginxStart and enable Nginx:
sudo systemctl start nginx
sudo systemctl enable nginxTo stop an Nginx server use:
sudo systemctl stop nginxCheck Nginx status:
systemctl status nginxNavigate to a temporary directory and clone the repository and change to that directory:
git clone https://github.com/Yaswanth20/my-react-app.git
cd my-react-appOpen the App.js file
Navigate to your React app’s source folder:
cd my-react-app/srcOpen the App.js file in a text editor:
nano App.js(or use vi/vim if you prefer)
Modify the content
<h2>Deployed by: <strong>Your Full Name</strong></h2>
<p>Date: <strong>DD/MM/YYYY</strong></p>Update the page as you like
Install required npm dependencies:
npm installBuild the React application:
npm run buildThis will generate a build/ folder with production-ready static files.
Remove any existing files in the Nginx web directory:
sudo rm -rf /var/www/html/*Copy the React build files to /var/www/html/:
sudo cp -r build/* /var/www/html/Start and enable your server:
sudo systemctl start nginx
sudo systemctl enable nginxCheck to see what port your server is running on:
Navigate back to your main directory
cd ~check your nginx.conf file using cat
cat /etc/nginx/nginx.confand look in the html section for the server and specifically the listen to identify the port. It should be defaultly on 80
server {
listen 80;
listen 443 ssl;
server_name example.com;
# ... other configurations
}If its not there, you can also use network utilities with the following command
sudo ss -nltp | grep nginxTry stopping the nginx server and see what happens.