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:- Ubuntu server with root or sudo privileges.
- Node.js and npm installed on the server.
-
Install Nginx:
Update the package index and install Nginx:
sudo apt update sudo apt install nginx
-
Build your React Application:
Navigate to your React application directory and build it:
npm run build
-
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.
-
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/
-
Test Nginx Configuration:
Test if the Nginx configuration is valid:
sudo nginx -t
-
Restart Nginx:
Restart Nginx to apply the changes:
sudo systemctl restart nginx
-
Configure Firewall (if necessary):
If you have a firewall enabled, allow traffic on port 80 (HTTP):
sudo ufw allow 'Nginx Full'
-
Point Domain to Server IP (if applicable):
If you have a domain, point it to your server's IP address using DNS settings.
-
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.