How To Deploy a React Application with Nginx on Ubuntu

Deploying a React application with Nginx on Ubuntu involves several steps including installing Nginx, building your React application, configuring Nginx to serve your application, and setting up a domain if necessary. Below are the general steps to accomplish this:

Prerequisites:
  1. Ubuntu server with root or sudo privileges.
  2. Node.js and npm installed on the server.
Steps:
  1. Install Nginx:

    Update the package index and install Nginx:

                    
                        sudo apt update
                        sudo apt install nginx                    
                    
                

  2. Build your React Application:

    Navigate to your React application directory and build it:

                    
                        npm run build
                    
                

  3. Configure Nginx:

    Navigate to the Nginx configuration directory:

                    
                        cd /etc/nginx/sites-available
                    
                

    Create a new configuration file for your React application:

                    
                        sudo nano your-app.conf
                    
                

    Replace your-app with your application name and add the following configuration:

                    
                        server {
                            listen 80;
                            server_name yourdomain.com; # Replace with your domain name
                        
                            root /path/to/your/react/app/build;
                            index index.html;
                        
                            location / {
                                try_files $uri $uri/ /index.html;
                            }
                        }                    
                    
                

    Save the file and exit the text editor.

  4. Enable the Site:

    Create a symbolic link to enable the site:

                    
                        sudo ln -s /etc/nginx/sites-available/your-app.conf /etc/nginx/sites-enabled/
                    
                

  5. Test Nginx Configuration:

    Test if the Nginx configuration is valid:

                    
                        sudo nginx -t
                    
                

  6. Restart Nginx:

    Restart Nginx to apply the changes:

                    
                        sudo systemctl restart nginx
                    
                

  7. Configure Firewall (if necessary):

    If you have a firewall enabled, allow traffic on port 80 (HTTP):

                    
                        sudo ufw allow 'Nginx Full'
                    
                

  8. Point Domain to Server IP (if applicable):

    If you have a domain, point it to your server's IP address using DNS settings.

  9. Access Your React App:

    Open a web browser and enter your domain name or server's IP address. You should see your React application served by Nginx.

That's it! Your React application is now deployed with Nginx on Ubuntu. Make sure to secure your server and configure SSL if needed for HTTPS support.

How To Open a Port on Linux

Opening a port on Linux involves configuring the firewall to allow traffic through the specified port. Here's a step-by-step guide to achieve this, assuming you are using ufw (Uncomplicated Firewall) or iptables for managing your firewall settings. u …

read more

Troubleshooting Latency Issues on App Platform

Troubleshooting latency issues on an app platform can be complex, involving multiple potential causes across the network, server, application code, and database. Here’s a structured approach to identifying and resolving latency issues. Identify …

read more