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.

SSH Essentials: Working with SSH Servers, Clients, and Keys

SSH (Secure Shell) is a cryptographic network protocol that allows secure communication between two computers over an insecure network. It is commonly used for remote login and command execution but can also be used for secure file transfer and other …

read more

How To Create SSH Keys with OpenSSH on macOS, Linux, or Windows Subsystem f …

Creating SSH keys with OpenSSH on macOS, Linux, or Windows Subsystem for Linux (WSL) involves a few simple steps. Here's a guide: Open Terminal (macOS/Linux) or WSL (Windows), Generate SSH Key Pair, Generate SSH Key Pair, Verify SSH Key Generation,Vi …

read more