Run these commands (as root) to update all dependencies and reboot:
apt update && apt upgrade -y
apt autoremove -y
apt autoclean -y
rebootInstall required packages:
apt install -y curl wget apt-transport-https gitCreate a user for your project:
adduser APP_USERTo 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 22Tip: If you need another version, replace
22with the required version number.
apt install -y nginxAdd 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-serverRun the MySQL secure installation wizard:
mysql_secure_installationCreate a database and a user:
mysqlRun 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-contribEnsure the service is running:
systemctl start postgresqlCreate a PostgreSQL user and database:
sudo -u postgres createuser --interactive
sudo -u postgres createdb DB_NAMEapt install -y redis-serverSet a Redis password:
nano /etc/redis/redis.confUncomment requirepass and set your password:
requirepass YOUR_PASSWORD
Restart Redis:
systemctl restart redisnpm install -g pm2npm 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/defaultAdd 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_ADDRESSInstall dependencies using your package manager:
npm install
# or
yarn installSet environment variables (typically in a .env file):
nano .envRun the application with PM2:
pm install -g pm2
pm2 start app.js --name your_app_nameOr 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. 🚀