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.

Top 50+ Linux Commands

Linux is a powerful operating system with a rich set of commands that allow you to control nearly every aspect of the system. Here is a list of over 50 Linux commands that you should know, including some of the most important ones. beginning of what …

read more

how to run start up

Starting a startup can be an exciting yet challenging endeavor. Here's a general outline of steps you can take to get started: running a startup requires dedication, perseverance, and flexibility. Be prepared to overcome challenges and learn from fai …

read more