A Complete Guide to Deploying a React, Node.js, and MySQL Application on a VPS
Contents
Article:
Deploying a full-stack application with React (frontend), Node.js (backend), and MySQL (database) on a VPS ensures a secure, scalable, and performant solution. This guide provides a comprehensive step-by-step process for deploying your application on a VPS.
Prerequisites
- A VPS server with root access (Ubuntu-based server recommended).
- A domain name (optional but preferred).
- Installed software on your local system:
- Fully developed React and Node.js application.
- MySQL database setup.
- Required server software:
- Node.js
- Nginx
- MySQL server
- PM2 (for Node.js process management)
Step 1: Set Up the VPS
- Log in to the VPS:
Use SSH to connect to your server.
bashssh root@your_server_ip
- Update System Packages:
bashsudo apt update && sudo apt upgrade -y
- Install Node.js:
bashcurl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt install -y nodejs
- Install Nginx:
bashsudo apt install nginx
- Install PM2:
bashsudo npm install -g pm2
- Install MySQL:
bashsudo apt install mysql-server
Step 2: Set Up the MySQL Database
- Secure MySQL Installation:
bashsudo mysql_secure_installation
Follow the prompts to set up a root password and secure MySQL. - Create a Database and User:
Log in to the MySQL server:
bashsudo mysql -u root -p
Create a database, user, and grant privileges:sqlCREATE DATABASE your_database_name; CREATE USER 'your_username'@'%' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'%'; FLUSH PRIVILEGES; EXIT;
- Allow Remote Access (Optional):
Edit the MySQL configuration to allow external connections:bashsudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Find the linebind-address = 127.0.0.1
and replace it with:iniCopy codebind-address = 0.0.0.0
Restart MySQL:bashsudo systemctl restart mysql
Step 3: Upload Your Application to the VPS
- Transfer Files:
Usescp
or FileZilla to upload your application files:
bashscp -r /local/path/to/project root@your_server_ip:/var/www/your-app
- Navigate to the Project Directory:
bashcd /var/www/your-app
Step 4: Configure the Backend (Node.js)
- Install Dependencies:
Navigate to the backend directory and install required packages:bashCopy codecd backend npm install
- Configure Environment Variables:
Create a.env
file for your environment variables:bashCopy codenano .env
Add the database credentials and other settings:
envDB_HOST=localhost DB_USER=your_username DB_PASS=your_password DB_NAME=your_database_name
- Start the Backend with PM2:
bashpm2 start index.js --name "backend" pm2 save pm2 startup
Step 5: Configure the Frontend (React)
- Build the React App:
Navigate to the React directory and build the app:
bashcd frontend npm run build
- Move Build Files:
Copy the React build files to the Nginx web directory:
bashsudo cp -r build/* /var/www/html
Step 6: Configure Nginx
- Edit Nginx Configuration:
Create a new Nginx configuration file:
bashsudo nano /etc/nginx/sites-available/your-app
Add the following configuration:nginxserver { listen 80; server_name your_domain_or_ip; root /var/www/html; index index.html index.htm; location /api/ { proxy_pass http://localhost:5000/; # Backend URL proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } location / { try_files $uri /index.html; } }
- Enable the Configuration:
Link and test the configuration:
bashsudo ln -s /etc/nginx/sites-available/your-app /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl restart nginx
Step 7: Secure Your App with HTTPS
- Install Certbot for SSL:bashCopy code
sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d your_domain
- Auto-Renew Certificates:
Add a cron job for certificate renewal:bashCopy codesudo crontab -e # Add the following line 0 0 * * * certbot renew --quiet
Step 8: Test Your Application
- Frontend: Visit your domain or server IP (
http://your_domain_or_ip
). - Backend: Test API routes via Postman or a browser (
http://your_domain_or_ip/api
). - Database: Ensure data flows between the application and the MySQL database.
Conclusion
By following these steps, you can successfully deploy a React, Node.js, and MySQL application on a VPS. This setup provides a robust, secure, and scalable solution for production environments. Regularly monitor the server’s performance, implement backups, and update software to maintain reliability.