Multipli siti web in diversi container su un server Proxmox 9 con un solo IP Pubblico

tratto da: https://community.ovhcloud.com/community/en/new-os-available-proxmox-virtual-environment-ve-9?id=community_question&sys_id=362f0572b1e7ae14f07803b560bfcdaf

Prerequisiti

  • OVH/Kimsufi dedicated server
  • Proxmox 9 installed by OVH
  • SSH access to the server
  • Basic understanding of Linux networking

Parte 1: Installazione iniziale

1.1 Setup utente amministratore

# SSH into server as root
ssh root@YOUR_SERVER_IP

# Create admin user
adduser utente
usermod -aG sudo utente

# Set up SSH key access
mkdir -p /home/utente/.ssh
cp /root/.ssh/authorized_keys /home/utente/.ssh/
chown -R utente: /home/utente/.ssh
chmod 700 /home/utente/.ssh
chmod 600 /home/utente/.ssh/authorized_keys

# Configure passwordless sudo
echo "utente ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/utente
chmod 440 /etc/sudoers.d/utente

1.2 Accesso Interfaccia Web

Imposto la password di root per poter accedere all’interfaccia web

passwd root

Posso ora accedere alla pagina web di Proxmox: https://YOUR_SERVER_IP:8006

Parte 2: Configurazione di rete dei container

2.1 il problema

La configurazione di default della rete dei container su OVH fallisce a causa di:

  • I container si connettono direttamente alla rete esterna vmbr0
  • Non è prevista la configurazione NAT per i container
  • Mancano le regole di FORWARD tra le reti esterne ed interne

2.2 Creo una rete Bridge interna

Modifica /etc/network/interfaces e aggiungi:

auto vmbr6
iface vmbr6 inet static
address 10.0.0.254/24
bridge-ports none
bridge-stp off
bridge-fd 0
post-up echo 1 > /proc/sys/net/ipv4/ip_forward
post-up iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o vmbr0 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -s 10.0.0.0/24 -o vmbr0 -j MASQUERADE

2.3 Attivo il bridge interno

# Bring up the new bridge
ifup vmbr6

# Verify bridge creation
ip addr show vmbr6

2.4 Configuro l’inoltro dei pacchetti:

# Enable IP forwarding permanently
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p

# Add FORWARD rules for container-to-internet traffic
iptables -I FORWARD -i vmbr6 -o vmbr0 -j ACCEPT
iptables -I FORWARD -i vmbr0 -o vmbr6 -m state --state RELATED,ESTABLISHED -j ACCEPT

# Importo il forward dei pacchetti web
iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to-destination 10.0.0.100:15080
iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 443 -j DNAT --to-destination 10.0.0.100:15443
iptables -A FORWARD -p tcp -d 10.0.0.100 --dport 15080 -j ACCEPT
iptables -A FORWARD -p tcp -d 10.0.0.100 --dport 15443 -j ACCEPT

# Save iptables rules
mkdir -p /etc/iptables
iptables-save > /etc/iptables/rules.v4

Parte 3: Creazione del container di routing

3.1 Download del Template per il Container

Dall’interfaccia web di Proxmox:

  • Vai sul nodo –> local storage
  • Clicca sul tab: “CT Templates”
  • Clicca sul tasto “Templates”
  • Scarica Debian-13-standard

3.2 Crea il container dall’interfaccia web

Dal nodo proxmox premo il tasto destro e scelgo “Crea CT”

  • General tab:
    • CT ID: 100
    • Hostname: your-container-name
    • Set password
    • Add SSH public key (opzionale)
  • Template tab:
    • Select debian-13-standard
  • Root Disk tab:
    • Size: 8GB (adjust as needed)
  • CPU/Memory tabs:
    • Set as needed (1 core, 1GB RAM for basic web server)
  • Network tab:
    • CRITICAL: Change bridge from vmbr0 to vmbr6
    • Set static IP: 10.0.0.100/24 (increment for additional containers)
    • Gateway: 10.0.0.254
  • DNS tab:
    • Use host settings

3.3 Verifica e configurazione Container di routing

# Start container
pct start 100

# Enter container
pct enter 100

# Test connectivity
ping -c 3 10.0.0.254 # Gateway
ping -c 3 8.8.8.8 # Internet

# Applico gli aggiornamenti come di prassi
apt update && apt upgrade -y

# Scarico i pacchetti nginx e Let's Encrypt
apt install python3-acme python3-certbot python3-mock python3-openssl python3-pkg-resources python3-pyparsing python3-zope.interface python3-certbot-nginx nginx

# Rimuovo la configurazione per il sito di default
rm /etc/nginx/sites-enabled/default

# Creo una configurazione di routing per un primo sito web: example.com sul server locale 10.0.0.101
echo 'server {
listen 15080;
listen [::]:15080; # Also listen on IPv6
server_name example.com;

location / {
proxy_pass http://10.0.0.101:80; # Indirizzo del server di destinazione
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;
}
}' > /etc/nginx/conf.d/router.conf

# Riavvio il server NGINX
service nginx restart

3.4 Test raggiungibilità sito web

I record DNS del vostro sito “example.com” deve essere configurato con un record di tipo A che punta all’indirizzo IP del vostro server Proxmox

Puntate il vostro browser verso l’indirizzo http://example.com

Se tutto e’ andato a buon fine dovreste vedere la pagina di Welcome di NGINX

3.5 Richiesta del certificato Let’s Encrypt

certbot --nginx -d example.com -m mymail@mydomain.com --agree-tos --no-eff-email --http-01-port 15080

Certbot vi chiederà se redirigere il traffico http a https. Per ora rispondete no.

3.6 Modificate la porta per https:

sed -i 's/listen 443 ssl/listen 15443 ssl/g' /etc/nginx/conf.d/router.conf

# Riavvio NGINX
service nginx restart

Parte 4: Server Web

Potete ora creare un nuovo container per ospitare il sito example.com (potete ripetere questi passaggi per altri nuovi container):

  • General tab:
    • CT ID: 101
    • Hostname: example.com
    • Set password
    • Add SSH public key (opzionale)
  • Template tab:
    • Select debian-13-standard
  • Root Disk tab:
    • Size: 8GB (adjust as needed)
  • CPU/Memory tabs:
    • Set as needed (1 core, 1GB RAM for basic web server)
  • Network tab:
    • CRITICAL: Change bridge from vmbr0 to vmbr6
    • Set static IP: 10.0.0.101/24 (increment for additional containers)
    • Gateway: 10.0.0.254
  • DNS tab:
    • Use host settings

Nota che l’indirizzo IP 10.0.0.101 è quello indicato nel file router.conf visto sopra.

Dalla shell di proxmox:

# Start container
pct start 101

# Enter container
pct enter 101

# Test connectivity
ping -c 3 10.0.0.254 # Gateway
ping -c 3 8.8.8.8 # Internet

# Applico gli aggiornamenti come di prassi
apt update && apt upgrade -y

# Scarico, installo e configuro Caddy
wget -O /tmp/caddy.tar.gz 'https://github.com/caddyserver/caddy/releases/download/v2.7.6/caddy_2.7.6_linux_amd64.tar.gz'
tar -xzf /tmp/caddy.tar.gz -C /usr/local/bin/ caddy
chmod +x /usr/local/bin/caddy

# Verify installation
/usr/local/bin/caddy version

# Create directory structure
mkdir -p /etc/caddy /var/www/html

# Create test page
echo '<h1>Working Caddy Web Server</h1><p>Container networking successful!</p>' > /var/www/html/index.html

# Create basic Caddyfile
echo ':80 {
root * /var/www/html
file_server
}' > /etc/caddy/Caddyfile

# Start Caddy
nohup /usr/local/bin/caddy run --config /etc/caddy/Caddyfile > /var/log/caddy.log 2>&1 &

Potete ora navigare su https://example.com

Avete finito !!

Nel file /etc/nginx/conf.d/router.conf potete aggiungere la configurazione anche per altri siti o server locali modificando il nome del sito o l’indirizzo del server (10.0.0.102, 10.0.0.103 …)

Configurare Open VPN su Mikrotik RouterOS

La seguente guida (spudoratamente ispirata da qui ) spiega come installare una Open VPN sul sistema RouterOS di Mikrotik (testato sulla versione 6.41.3)

Partiamo da una configurazione Base del Mikrotik

/system reset-configuration

Assegnare l’identificativo al Mikrotik da

/system identity set name="MioMikrotik"

Impostare l’ora esatta con NTP:

/system ntp client set mode=unicast enabled=yes servers=([:resolve 0.it.pool.ntp.org],[:resolve 1.it.pool.ntp.org])

Impostare il fuso orario:

/system clock set time-zone-name=Europe/Rome

Copiare questo script personalizzando le variabili global alle prime linee

# Setup OpenVPN Server and generate certs
#
# Change variables below and paste the script
# into MikroTik terminal window.
#

:global CN [/system identity get name]
:global COUNTRY "IT"
:global STATE "MYSTATE"
:global LOC "MYCITY"
:global ORG "MyOrg"
:global OU ""
:global KEYSIZE "2048"

## functions
:global waitSec do={:return ($KEYSIZE * 10 / 1024)}

## generate a CA certificate
/certificate

add name=ca-template country="$COUNTRY" state="$STATE" locality="$LOC" \
  organization="$ORG" unit="$OU" common-name="$CN" key-size="$KEYSIZE" \
  days-valid=3650 key-usage=crl-sign,key-cert-sign
sign ca-template ca-crl-host=127.0.0.1 name="$CN"
:delay [$waitSec]

## generate a server certificate
/certificate
add name=server-template country="$COUNTRY" state="$STATE" locality="$LOC" \
  organization="$ORG" unit="$OU" common-name="server@$CN" key-size="$KEYSIZE" \
  days-valid=3650 key-usage=digital-signature,key-encipherment,tls-server
sign server-template ca="$CN" name="server@$CN"
:delay [$waitSec]

## create a client template
/certificate
add name=client-template country="$COUNTRY" state="$STATE" locality="$LOC" \
  organization="$ORG" unit="$OU" common-name="client" \
  key-size="$KEYSIZE" days-valid=3650 key-usage=tls-client

## create IP pool
/ip pool
add name=VPN-POOL ranges=192.168.252.128-192.168.252.224

## add VPN profile
/ppp profile
add dns-server=192.168.252.1 local-address=192.168.252.1 name=VPN-PROFILE \
  remote-address=VPN-POOL use-encryption=yes

## setup OpenVPN server
/interface ovpn-server server
set auth=sha1 certificate="server@$CN" cipher=aes128,aes192,aes256 \
  default-profile=VPN-PROFILE enabled=yes require-client-certificate=yes

## add a firewall rule
/ip firewall filter
add chain=input dst-port=1194 protocol=tcp comment="Allow OpenVPN"

## Sposto la regola del firewall prima della generica deny
/ip firewall filter move [find \
comment="Allow OpenVPN"] \
[find where chain=input in-interface-list="!LAN"]

ed incollarlo nel terminale del Mikrotik (io trovo comodo copiarlo dal terminale dopo essermi collegato via ssh al Mikrotik)

Stessa cosa col seguente script per la configurazione dell’utente client (questo andrà ripetuto per ogni nuovo utente da abilitare):

# Add a new user and generate/export certs
#
# Change variables below and paste the script
# into MikroTik terminal window.
#

:global CN [/system identity get name]
:global USERNAME "user"
:global PASSWORD "password"

## add a user
/ppp secret
add name=$USERNAME password=$PASSWORD profile=VPN-PROFILE service=ovpn

## generate a client certificate
/certificate
add name=client-template-to-issue copy-from="client-template" \
  common-name="$USERNAME@$CN"
sign client-template-to-issue ca="$CN" name="$USERNAME@$CN"
:delay 20

## export the CA, client certificate, and private key
/certificate
export-certificate "$CN" export-passphrase=""
export-certificate "$USERNAME@$CN" export-passphrase="$PASSWORD"

A questo punto dal pc possiamo scaricare i certificati, tramite interfaccia web (impostare correttamente l’indirizzo ip del vostro Mikrotik es: 192.168.88.1):

http://MikroTik_IP/webfig/#Files

o da terminale con sftp:

sftp admin@MikroTik_IP:cert_export_\*

Creare nella stessa cartella dei certificati un file user.auth con indicati nome utente e password:

user
password

Sempre nella stessa cartella creare il file di configurazione lato client modificando i parametri in grassetto:

client
dev tun
proto tcp-client
remote MikroTik_IP 1194
nobind
persist-key
persist-tun
cipher AES-256-CBC
auth SHA1
pull
verb 2
mute 3

# Create a file 'user.auth' with a username and a password
#
# cat << EOF > user.auth
# user
# password
# EOF
auth-user-pass user.auth

# Copy the certificates from MikroTik and change
# the filenames below if needed
ca cert_export_MioMikroTik.crt
cert cert_export_user@MioMikroTik.crt
key cert_export_user@MioMikroTik.key

# Add routes to networks behind MikroTik
#route 192.168.10.0 255.255.255.0

A questo punto la configurazione è pronta per essere data in pasto al vostro client OVPN

Se preferite che non venga chiesta la password all’avvio della VPN è necessario trasformare la chiave privata in questo modo:

openssl rsa -passin pass:password -in cert_export_user@MioMikroTik.key -out cert_export_user_np@MioMikroTik.key

ed utilizzare questa nuova chiave privata.

Per rimuovere un utente e revocare il relativo certificato:

# Delete a user and revoke his certificate
#
# Change variables below and paste the script
# into MikroTik terminal window.
#

:global CN [/system identity get name]
:global USERNAME "user"

## delete a user
/ppp secret
remove [find name=$USERNAME profile=VPN-PROFILE]

## revoke a client certificate
/certificate
issued-revoke [find name="$USERNAME@$CN"]

Per rimuovere la configurazione OpenVPN dal Mikrotik:

# Revert OpenVPN configuration
#

/ip pool
remove [find name=VPN-POOL]

/ppp profile
remove [find name=VPN-PROFILE]

/ip firewall filter
remove [find comment="Allow OpenVPN"]

/ppp secrets
remove [find profile=VPN-PROFILE]

/certificate
## delete the certificates manually

I certificati vanno rimossi manualmente.

Buon Lavoro 😉