Migrating Python and Node.js applications has its own specifics: besides files and the database, you need to transfer the environment, install dependencies, and set up systemd services for autostart.
Migrating a Python/Django application
1. Pin down dependencies
># On the old server
source /path/to/venv/bin/activate
pip freeze > requirements.txt
# Transfer to the new server
git clone https://github.com/your/repo.git /var/www/myapp
# or rsync
rsync -avz /var/www/myapp/ root@new-server:/var/www/myapp/
2. Deploy on the new server
># Create the virtual environment
python3 -m venv /var/www/myapp/venv
source /var/www/myapp/venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Environment variables — transfer the .env file
scp old-server:/var/www/myapp/.env /var/www/myapp/.env
# Apply database migrations
python manage.py migrate
# Collect static files
python manage.py collectstatic --noinput
3. Setting up systemd for Django (Gunicorn)
># /etc/systemd/system/myapp.service
[Unit]
Description=Django App via Gunicorn
After=network.target
[Service]
User=www-data
WorkingDirectory=/var/www/myapp
EnvironmentFile=/var/www/myapp/.env
ExecStart=/var/www/myapp/venv/bin/gunicorn --workers 3 --bind unix:/run/myapp.sock myapp.wsgi:application
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now myapp
Migrating a Node.js application
1. Pin down the Node.js version
># Check the version on the old server
node --version
npm --version
# Install nvm on the new server
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 20 # Install the required version
2. Transfer and restore
># Transfer only the code (without node_modules!)
rsync -avz --exclude=node_modules /var/www/nodeapp/ root@new-server:/var/www/nodeapp/
# Install dependencies on the new server
cd /var/www/nodeapp
npm ci # clean install from package-lock.json
# Transfer .env
scp old-server:/var/www/nodeapp/.env /var/www/nodeapp/
3. PM2 for autostart
># Install PM2
npm install -g pm2
# Start the application
pm2 start app.js --name myapp
# Save the PM2 configuration (export from the old server)
# On the old server
pm2 save
# Transfer the config
scp old-server:~/.pm2/dump.pm2 ~/.pm2/
pm2 resurrect
# Set up autostart on reboot
pm2 startup
pm2 save
Nginx reverse proxy for Python/Node.js
># /etc/nginx/sites-available/myapp
server {
listen 80;
server_name myapp.example.com;
location / {
proxy_pass http://unix:/run/myapp.sock; # Python/Gunicorn
# or
# proxy_pass http://localhost:3000; # Node.js
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Environment variables: Never commit
.env files to git. When migrating, transfer them via SCP or a secure secrets manager (Vault, Doppler). Check .gitignore before cloning.