Linux - Ollama Installation
A comprehensive step-by-step guide for installing Ollama on Debian 12 with Apache2 reverse proxy, SSL encryption and HTTP Basic Authentication for secure public API usage.
Important Notes
Please note these important points before setup
Root Access Required
Root access or sudo permissions are required for all installation steps
Domain Required
Valid domain with DNS configuration is necessary for SSL certificates
Firewall Configuration
Open ports 80 and 443 in your firewall for HTTP/HTTPS access
Secure Password
Use a strong password for API authentication
Create Backup
Backup all configuration files before making important changes
Hardware Requirements
At least 8GB RAM is recommended for running Ollama models
Setup Steps
Update package lists again
apt update -y && apt upgrade -yInstall basic packages
apt install sudo curlDownload and install Ollama
curl -fsSL https://ollama.com/install.sh | shDownload and test small test model
ollama run gemma3:4bOpen Ollama service configuration
systemctl edit ollama.serviceAdd the following environment variables
Reload systemd manager
sudo systemctl daemon-reexecReload Ollama service
sudo systemctl daemon-reloadRestart Ollama service
sudo systemctl restart ollamaInstall Apache2 and SSL modules
sudo apt update && sudo apt install apache2 apache2-utils -yEnable required Apache modules
sudo a2enmod proxy proxy_http ssl headers rewriteRestart Apache2
sudo systemctl restart apache2Install Certbot for Let's Encrypt
sudo apt install certbot python3-certbot-apache -yRequest SSL certificate for domain
sudo certbot --apache -d server.chad.lu --register-unsafely-without-emailCreate directory for auth files
sudo mkdir -p /etc/apache2/htpasswdCreate user with password for HTTP Basic Auth
sudo htpasswd -c /etc/apache2/htpasswd/ollama-api.htpasswd apiuserEnter a secure password when prompted
Create the Apache Virtual Host configuration file
Enable new site configuration
sudo a2ensite server.chad.lu.confDisable default Apache site
sudo a2dissite 000-default.confDisable default SSL site
sudo a2dissite 000-default-le-ssl.confReload Apache configuration
sudo systemctl reload apache2Test API endpoint with curl
curl -u apiuser https://server.chad.lu/ollama/api/generate -H "Content-Type: application/json" -d '{"model": "gemma3:4b", "prompt": "Erzähl mir eine Geschichte.", "stream": true}'You will be prompted for the 'apiuser' password. On successful test, you will receive JSON responses with generated text fragments.
Troubleshooting
Common issues and their solutions
Installation Complete!
Your Ollama server is now fully configured and secured with HTTPS and Basic Authentication. You can now safely access the API.