Run these commands (as root) to update all dependencies and reboot:
apt update && apt upgrade -y
apt autoremove -y
apt autoclean -y
reboot
Install required packages:
apt install -y curl wget apt-transport-https git
Create a user for your project:
adduser APP_USER
To install Node.js using NVM, run:
cd ~
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
# Use the desired Node.js version
nvm use 22
Tip: If you need another version, replace
22
with the required version number.
apt install -y nginx
Add the MySQL repository and install MySQL:
wget http://repo.mysql.com/mysql-apt-config_0.8.14-1_all.deb
apt update
apt install -y mysql-server
Run the MySQL secure installation wizard:
mysql_secure_installation
Create a database and a user:
mysql
Run the following commands inside MySQL:
CREATE DATABASE YOUR_DB_NAME COLLATE utf8_general_ci;
CREATE USER 'YOUR_DB_USERNAME'@'localhost' IDENTIFIED BY 'YOUR_DB_PASSWORD';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, SHOW VIEW ON YOUR_DB_NAME.* TO 'YOUR_DB_USERNAME'@'localhost';
FLUSH PRIVILEGES;
Install PostgreSQL:
apt install -y postgresql postgresql-contrib
Ensure the service is running:
systemctl start postgresql
Create a PostgreSQL user and database:
sudo -u postgres createuser --interactive
sudo -u postgres createdb DB_NAME
apt install -y redis-server
Set a Redis password:
nano /etc/redis/redis.conf
Uncomment requirepass
and set your password:
requirepass YOUR_PASSWORD
Restart Redis:
systemctl restart redis
npm install -g pm2
npm install -g yarn
- Create a MySQL or PostgreSQL database for your project (with superuser permissions).
- If using Nginx for proxy forwarding, update the default configuration file:
nano /etc/nginx/sites-available/default
Add the following configuration:
server {
listen 80;
server_name myapp.com; # Replace with your domain
location / {
proxy_pass http://localhost:3000; # Change to your app's port
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}
- Install
nginx-extras
:apt install -y nginx-extras
- Edit Nginx configuration:
Add the following:
nano /etc/nginx/nginx.conf
http { more_set_headers "Server: Your_New_Server_Name"; server_tokens off; }
- Restart Nginx:
systemctl restart nginx
Log in with the project user and pull the latest files from the repository:
git pull YOUR_REPOSITORY_ADDRESS
Install dependencies using your package manager:
npm install
# or
yarn install
Set environment variables (typically in a .env
file):
nano .env
Run the application with PM2:
pm install -g pm2
pm2 start app.js --name your_app_name
Or using Docker Compose:
docker compose --profile profile_name up -d --build
- Default application port:
3000
- Default domain:
myapp.com
This guide ensures a robust, production-ready setup for your Node.js application on a Debian server. 🚀