Dark Light

If you’re looking for a way to automate workflows, you might be familiar with n8n, an open-source workflow automation tool. It’s great for connecting various services, triggering actions, and performing tasks across multiple platforms. Hosting n8n on your own server gives you full control over your workflows and data. In this guide, we’ll walk you through the steps to self-host n8n using aaPanel, Docker, and Nginx. We’ll take advantage of aaPanel’s integrated Docker management and provide instructions for setting up a reverse proxy with Nginx and SSL for secure access.

Prerequisites

Before we begin, ensure you have:

• A Linux server (e.g., Ubuntu or CentOS).

aaPanel installed on the server.

• A domain name pointing to your server’s IP (e.g., n8n.yourdomain.com).

SSL certificates for your domain (via Let’s Encrypt).

Docker, and nginx installed and enabled in aaPanel.

Step 1: Create a Folder for n8n and Set Up Docker Compose

1. In aaPanel, go to the File menu and create a folder named n8n in your desired location (e.g., /www/n8n).

2. Inside the n8n folder, create a docker-compose.yml file with the following content:

version: "3.7"

services:
  n8n:
    image: n8nio/n8n
    container_name: n8n
    restart: always
    environment:
      - N8N_HOST=<your-domain-or-ip>
      - N8N_PORT=8443
      - N8N_PROTOCOL=https
      - GENERIC_TIMEZONE=Europe/Berlin
      - N8N_BASIC_AUTH_USER=<your-username>
      - N8N_BASIC_AUTH_PASSWORD=<your-password>
    ports:
      - 8443:5678 # Expose the internal port 5678 through external port 443
    volumes:
      - /www/wwwroot/n8n:/home/node/.n8n

Replace the following placeholders:

• <your-domain-or-ip>: Replace with your actual domain or server IP address. However, it’s highly recommended to set up your instance with a domain and SSL from the beginning. Using an IP address can lead to some limitations, such as issues with SSL certificates, domain-based services (like webhooks), and scalability. A domain name ensures secure, reliable access to your n8n instance, and using SSL further secures your communications, which is particularly important for integrations with third-party services.

• Actually when you use the – 8443:5678 mapping in the docker-compose.yml file, the port mapping will automatically direct traffic from port 8443 on the host to port 5678 inside the container. Therefore, you do not need to explicitly define N8N_PORT=8443 in the environment variables.

• <your-username> and <your-password>: Set your desired basic authentication username and password.

Step 2: Start the n8n Container

1. Once you’ve saved the docker-compose.yml file, navigate to the n8n folder in aaPanel.

2. From the SSH terminal or aaPanel’s Docker Management interface, navigate to the folder where the docker-compose.yml file is located and run:

docker-compose up -d

This command will download the official n8n Docker image and start the container in the background.

Step 3: Add Your Domain to aaPanel

Before configuring Nginx, you need to add your domain to aaPanel to properly manage your website settings. Here’s how:

1. Navigate to the Website section.

2. Click on Add Site and fill in the details:

Domain Name: Enter your domain (e.g., n8n.yourdomain.com).

Web Directory: Set the root directory for your website, which can be /www/n8n (or wherever your docker-compose.yml is located).

PHP Version: If you don’t need PHP, you can leave this as it is (it’s not necessary for n8n).

After filling in the details, click Add.

3. Once the site is added, enable SSL if you haven’t already, using the Let’s Encrypt option available in aaPanel. This will automatically install SSL certificates for your domain.

Step 4: Set Up SSL with Let’s Encrypt

To secure your n8n instance with SSL using Let’s Encrypt, follow these steps:

1. Install Certbot:

sudo apt install certbot python3-certbot-nginx

2. Request the SSL certificate for your domain:

sudo certbot --nginx -d n8n.yourdomain.com

3. Follow the instructions to complete the certificate installation. Certbot will automatically configure SSL for Nginx.

Step 5: Set Up Nginx as a Reverse Proxy

1. Create a new Nginx configuration file for your n8n instance:

server {
    listen 443 ssl;
    server_name n8n.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/n8n.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/n8n.yourdomain.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8443;
        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;
    }
}

Explanation:

• Replace n8n.yourdomain.com with your actual domain.

• The SSL certificates are expected to be in /etc/letsencrypt/live/. Adjust the paths if they are different.

• The proxy pass is set to route traffic to port 8443 where the n8n container is running.

2. Enable the Nginx configuration by creating a symbolic link:

sudo ln -s /etc/nginx/sites-available/n8n /etc/nginx/sites-enabled/

3. Test the Nginx configuration:

sudo nginx -t

4. Reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 6: Access n8n Securely Register for n8n

Once SSL is configured, you can access your n8n instance securely at: https://n8n.yourdomain.com. You can now start using n8n for your automation workflows. Upon the first launch, you need to register an admin user through the web interface:

1. Open a browser and navigate to https://n8n.yourdomain.com

2. You’ll be prompted to register using an email, first name, last name and password. This step is necessary as n8n requires an initial setup for the admin user.

3. After completing the registration, you will have access to the n8n dashboard.

Since the first run requires you to register, the N8N_BASIC_AUTH_USER and N8N_BASIC_AUTH_PASSWORD variables won’t be used at this stage. You can configure them for later use in securing n8n or for API access.

Step 7: Configure Webhooks (Optional)

If you plan to use webhooks in your n8n automations (for example, with nodes like the Telegram Trigger or other webhook-based triggers), it’s important to configure the WEBHOOK_URL environment variable. This ensures that n8n can properly route incoming webhook requests to your domain.

To enable webhook functionality in your setup:

1. Open your docker-compose.yml file located in the n8n folder.

2. Add the WEBHOOK_URL variable under the environment section.

WEBHOOK_URL=https://n8n.yourdomain.com/

After updating your docker-compose.yml file with the WEBHOOK_URL variable, you will need to restart the n8n container for the changes to take effect.

1. Navigate to your n8n folder (where the docker-compose.yml is located).

2. Run the following command to restart the container:

docker-compose down && docker-compose up -d


This will stop and restart the n8n container with the new configuration.

Conclusion

By following these steps, you’ve successfully set up n8n with Docker, aaPanel, and Nginx. Now, you can automate your workflows securely with a fully functional self-hosted n8n instance accessible over HTTPS. Enjoy building your automations!

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts