Introduction
ReactJS is a popular JavaScript library for building user interfaces, especially for single-page applications. In this guide, you will learn how to install ReactJS and serve your application using Nginx on Ubuntu 22.04. This setup can be effectively hosted on a Windows VPS UK for optimal performance and scalability.
Prerequisites
- An Ubuntu 22.04 server with root access
- Node.js and npm (Node Package Manager) installed
- Basic knowledge of Linux commands
Step 1: Update Your System
Start by updating your package index and upgrading existing packages:
sudo apt update && sudo apt upgrade -y
Step 2: Install Node.js and npm
Install Node.js and npm using the following commands:
sudo apt install nodejs npm -y
Verify the installation:
node -v
npm -v
Step 3: Create a React Application
Use the npx
command to create a new React application. Replace my-app
with your desired application name:
npx create-react-app my-app
Change into your application directory:
cd my-app
Step 4: Build the React Application
Build your React application for production:
npm run build
This command creates a build
directory containing static files for deployment.
Step 5: Install Nginx
Install Nginx to serve your React application:
sudo apt install nginx -y
Step 6: Configure Nginx
Edit the Nginx configuration file to serve your React app:
sudo nano /etc/nginx/sites-available/default
Replace the existing content with the following:
server {
listen 80;
server_name your_domain.com; # Replace with your domain or IP
location / {
root /path/to/my-app/build; # Adjust the path to your build directory
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
Make sure to replace /path/to/my-app/build
with the actual path to your React application's build directory and your_domain.com
with your domain or server IP address.
Step 7: Test Nginx Configuration
Test the Nginx configuration for syntax errors:
sudo nginx -t
Step 8: Restart Nginx
Restart Nginx to apply the changes:
sudo systemctl restart nginx
Step 9: Access Your React Application
Open your web browser and navigate to http://your_domain.com
(or your server's IP address). You should see your React application running.
Step 10: Conclusion
You have successfully installed ReactJS and configured Nginx to serve your application on Ubuntu 22.04. This setup provides a robust platform for web applications and can greatly benefit from being hosted on a Windows VPS. For additional options, explore various VPS UK Windows solutions, including Windows Virtual Private Server Hosting and Windows VPS Hosting UK for optimal performance.