Deploying LibreChat with Azure OpenAI GPT on Hetzner, DigitalOcean (or any other Cloud Provider)
Tom Oehlrich
Have you been searching for a self-hosted alternative to web-based chat platforms like ChatGPT or Claude? LibreChat offers a flexible, open-source solution, enabling integration of multiple large language models (LLMs) within a single client, including OpenAI’s GPT models and Azure OpenAI. This guide walks you through setting up LibreChat on a Hetzner Cloud and a DigitalOcean server, with each step beyond the cloud instance setup applicable to other cloud providers as well.
Some of the links (*) in this blog post are affiliate links. This means I may earn a commission, at no extra cost to you, if you make a purchase through these links. I only recommend products and services that I personally use and enjoy.
Step 1: Prepare Your Cloud Instance
Hetzner Cloud
Create a new instance and choose a basic Ubuntu server.
With Hetzner, you can always upgrade or downgrade vCPU and RAM, as well as increase disk size. Just keep in mind that you can’t switch to a plan with a smaller disk.
I chose the entry-level server with 2 vCPU, 4 GB RAM, 40 GB SSD, and 20 TB of traffic for under $4 USD per month.
DigitalOcean Cloud
Create a project with DigitalOcean*, and within it, set up a new Droplet with a basic Ubuntu server.
I chose the smallest shared CPU Droplet that meets LibreChat’s minimum requirements: 1 vCPU, 1 GB RAM, 10 GB SSD, and 1 TB transfer for $6 USD per month.
If you’re just experimenting, "password authentication" should be fine; otherwise, always opt for SSH key authentication.
SSH into your server
Once the server is created, SSH into it using:
ssh root@your-server-ip
Step 2: Install Docker, Docker Compose, and Git
Update your package manager: Before installing anything, make sure your system is up to date.
apt update && apt upgrade -y
Install Docker: Docker is crucial for containerizing LibreChat.
apt install docker.io -y
Install Docker Compose: Docker Compose simplifies running multi-container Docker applications.
apt install docker-compose -y
Install Git: This might already be installed, but it's essential for cloning the LibreChat repository.
apt install git -y
Step 3: Clone the LibreChat Repository
Clone the LibreChat repository from GitHub:
git clone https://github.com/danny-avila/LibreChat.git
cd LibreChat
Step 4: Set Up the Environment
Create the
.env
file: The.env
file contains environment variables required for LibreChat to function.cp .env.example .env nano .env
Add your OpenAI API key: In the
.env
file, add your OpenAI API key.OPENAI_API_KEY=your_openai_api_key
Step 5: Run LibreChat with Docker
Once the environment is configured, you can use Docker Compose to run LibreChat.
sudo docker-compose up -d
Step 6: Set Up SSL with Nginx and Certbot
1. Install Nginx and Certbot:
First, ensure your package manager is up to date:
sudo apt update && sudo apt upgrade -y
Then install Nginx and Certbot:
sudo apt install certbot python3-certbot-nginx -y
sudo apt install nginx -y
2. Create an Nginx Configuration:
You'll need to create an Nginx configuration file for LibreChat. Create a new file in /etc/nginx/sites-available/
:
sudo nano /etc/nginx/sites-available/librechat
Paste the following configuration into the file, replacing your_domain.com
with your actual domain:
server {
listen 80;
server_name your_domain.com www.your_domain.com;
location / {
proxy_pass http://localhost:3080;
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;
}
}
For a subdomain, only include the subdomain:
server_name subdomain.your_domain.com;
3. Enable the Nginx Configuration:
Link the configuration to the sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/librechat /etc/nginx/sites-enabled/
4. Test and Reload Nginx:
Before proceeding, ensure your Nginx configuration is valid:
sudo nginx -t
If everything is okay, reload Nginx:
sudo systemctl reload nginx
5. Update DNS Settings:
Set up an A-record for your domain or subdomain and enter your Hetzner / DigitalOcean* server's IPv4 address. After some propagation time, your domain should point to the LibreChat installation.
6. Obtain SSL Certificates:
Run Certbot to generate SSL certificates:
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
For a subdomain, use:
sudo certbot --nginx -d subdomain.your_domain.com
Enter your email address and accept the Let's Encrypt Terms of Service.
If you're having difficulties with IPv6, you might need to add an A record for IPv6 in your DNS settings.
Certbot should automatically handle renewal, but it's a good idea to test it:
sudo certbot renew --dry-run
Step 7: Disable User Registration (Optional)
In a company setup there is a good chance that you'd like to disable user registration in LibreChat. You can do so by updating the .env
file:
nano .env
ALLOW_REGISTRATION=false
While you are on it make some more adjustments like setting new JWT secrets.
JWT_SECRET=[NEW SECRET]
JWT_REFRESH_SECRET=[ANOTHER NEW SECRET]
Then restart the Docker containers:
sudo docker-compose down
sudo docker-compose up -d
Step 8: Create users manually
Use the command line to create new users:
sudo docker-compose exec api npm run create-user
Just enter a couple of information about the new user and you are done.
Step 9: Set up Azure OpenAI
The final optional step is to set up Azure OpenAI. By configuring Azure OpenAI, you can leverage your own custom deployments that offer flexibility to meet specific needs. Azure allows you to create regional deployments, which can provide better response times and data residency options compared to the global standard model.
In the .env
file, you’ll find a section for Azure OpenAI settings:
AZURE_OPENAI_DEFAULT_MODEL=gpt-3.5-turbo
AZURE_OPENAI_MODELS=gpt-3.5-turbo,gpt-4
AZURE_USE_MODEL_AS_DEPLOYMENT_NAME=TRUE
AZURE_API_KEY=
AZURE_OPENAI_API_INSTANCE_NAME=
AZURE_OPENAI_API_DEPLOYMENT_NAME=
AZURE_OPENAI_API_VERSION=
AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME=
These settings are marked as deprecated but will still work for now. However, the recommend way is to use the librechat.yaml
configuration file for azureOpenAI instead. A guide on using the librechat.yaml
file might be worth another blog post.
Below is an example configuration for an Azure endpoint, such as
https://my-openai-resource.openai.azure.com/openai/deployments/my-gpt-deployment-4o-2024-08-06-sweden/chat/completions?api-version=2023-03-15-preview
AZURE_OPENAI_DEFAULT_MODEL=gpt-4o-2024-08-06-sweden
AZURE_OPENAI_MODELS=gpt-4o-2024-08-06-sweden
AZURE_USE_MODEL_AS_DEPLOYMENT_NAME=TRUE
AZURE_API_KEY=MY_API_KEY
AZURE_OPENAI_API_INSTANCE_NAME=my-openai-resource
AZURE_OPENAI_API_DEPLOYMENT_NAME=
AZURE_OPENAI_API_VERSION=2023-03-15-preview
AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME=my-gpt-deployment-4o-2024-08-06-sweden
Setting up a server like this should also involve basic security measures, so don’t forget to secure your setup accordingly.
That’s it! Enjoy exploring your new LibreChat setup.