Skip to content

Instantly share code, notes, and snippets.

@kanchokanchev
Last active July 5, 2025 14:08
Show Gist options
  • Select an option

  • Save kanchokanchev/3ede290fef019d594517261158cde202 to your computer and use it in GitHub Desktop.

Select an option

Save kanchokanchev/3ede290fef019d594517261158cde202 to your computer and use it in GitHub Desktop.
Nginx - Backup Nginx Configuration And Certificates #Nginx_ADMIN

Instructions for Backing Up Nginx Configuration and Certificates

Follow these steps to back up your Nginx configuration and certificates from an Ubuntu machine.

Step 1: Create a Backup Directory

  1. Create a Temporary Directory:

    • Create a directory to store all configurations and certificates temporarily:
    mkdir ~/nginx_backup

Step 2: Copy Nginx Configuration Files

  1. Copy Nginx Configuration:

    • Copy the main Nginx configuration files to the backup directory:
    sudo cp -r /etc/nginx ~/nginx_backup/
  2. Copy SSL Certificates:

    • If your SSL certificates are stored in the default location, copy them as follows:
    sudo cp -r /etc/ssl ~/nginx_backup/
    • If your certificates are stored in a custom directory, adjust the path accordingly.

Step 3: Compress the Backup Directory

  1. Create a Compressed Zip File:

    • Use the zip command to compress the backup directory:
    cd ~
    sudo zip -r nginx_backup.zip nginx_backup/

    This will create a nginx_backup.zip file in your home directory.

Step 4: Transfer the Backup File

  1. Use a Transfer Method:
    • Transfer the nginx_backup.zip file to your new machine. You can use methods like:
      • scp command (if available)
      • Cloud storage (e.g., Dropbox, Google Drive)
      • USB drive

Step 5: Restore Configuration and Certificates on the New Machine

  1. Extract the Backup File:

    • Place the nginx_backup.zip file on the new machine and extract it:
    sudo unzip nginx_backup.zip -d ~/
  2. Move Configurations to the Appropriate Directories:

    • Move the extracted configurations to their respective directories:
    sudo mv ~/nginx_backup/nginx /etc/
    sudo mv ~/nginx_backup/ssl /etc/
  3. Verify File Permissions:

    • Ensure that file permissions and ownership are correct:
    sudo chown -R root:root /etc/nginx/
    sudo chown -R root:root /etc/ssl/

Step 6: Install and Configure Nginx on the New Machine

  1. Install Nginx:

    • If not already installed, install Nginx on the new machine:
    sudo apt update
    sudo apt install nginx -y
  2. Test Nginx Configuration:

    • Test the configuration to ensure there are no errors:
    sudo nginx -t
  3. Start Nginx Service:

    • Start the Nginx service:
    sudo systemctl start nginx
  4. Enable Nginx to Start on Boot:

    • Ensure Nginx starts automatically after a reboot:
    sudo systemctl enable nginx

Step 7: Verify Migration

  1. Check Sites:

    • Open your web browser and verify that all sites are working as expected.
  2. Check SSL Certificates:

    • Verify that the SSL certificates are correctly applied to the appropriate sites.
  3. Verify Logs (Optional):

    • Check the Nginx logs to ensure there are no errors:
    sudo tail -f /var/log/nginx/error.log

Step 8: Cleanup (Optional)

  1. Remove Temporary Backup Files:

    • Optionally, remove the backup directory and zip file:
    rm -rf ~/nginx_backup
    rm ~/nginx_backup.zip

This process should help you seamlessly back up and restore your Nginx configuration and certificates to a new server. Let me know if you need any further assistance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment