Blog, News & Events

How to Deploy AI Models on a VPS (TensorFlow & PyTorch Setup)

Hosting AI models on a VPS is one of the smartest ways to make your machine learning projects accessible, scalable, and cost-effective. Whether you’re working with TensorFlow or PyTorch, a VPS allows you to deploy your models on your own virtual server—without paying for expensive dedicated cloud services.

In this guide, we’ll show you how to set up TensorFlow and PyTorch on a VPS (like Sternhost VPS), optimize the environment, and deploy your AI model for real-time inference.

⚙️ Step 1: Choose a Suitable VPS

Before deployment, select a VPS with sufficient resources:

  • CPU: At least 4 cores (8+ for heavy inference)

  • RAM: Minimum 8 GB

  • Storage: SSD recommended for speed

  • GPU Support (optional): For training or GPU-based inference

💡 Sternhost VPS offers SSD storage and root access, perfect for TensorFlow and PyTorch hosting.

🧩 Step 2: Connect to Your VPS

Use SSH to connect to your VPS:

ssh root@your-server-ip

Update and upgrade your system:

sudo apt update && sudo apt upgrade -y

🧰 Step 3: Install Python and Virtual Environment

You’ll need Python 3.9+ and venv for environment management:

sudo apt install python3 python3-venv python3-pip -y
python3 -m venv ai_env
source ai_env/bin/activate

🧠 Step 4: Install TensorFlow or PyTorch

Depending on your framework:

For TensorFlow:

pip install tensorflow

For PyTorch:

pip install torch torchvision torchaudio

Check installation:

python -c "import tensorflow as tf; print(tf.__version__)"
# or
python -c "import torch; print(torch.__version__)"

🚀 Step 5: Upload Your Model

Use scp or an SFTP client to upload your model files (.h5, .pt, .pkl, etc.) to your VPS:

scp model.h5 root@your-server-ip:/root/

🧪 Step 6: Run a Simple Flask API

Create an API to serve predictions:

pip install flask

Example app.py:

from flask import Flask, request, jsonify
import tensorflow as tf

app = Flask(__name__)
model = tf.keras.models.load_model(“model.h5”)

@app.route(“/predict”, methods=[“POST”])
def predict():
data = request.json
prediction = model.predict([data[“input”]])
return jsonify({“prediction”: prediction.tolist()})

if __name__ == “__main__”:
app.run(host=“0.0.0.0”, port=5000)

Run it:

python app.py

Your model is now live at http://your-server-ip:5000/predict.

🔐 Step 7: Secure and Optimize

  • Use Nginx as a reverse proxy

  • Enable SSL with Let’s Encrypt

  • Use Gunicorn or Uvicorn for production-level serving

 

🧭 Step 8: Monitor and Scale

Monitor server performance using:

htop
nvidia-smi # for GPU

As traffic grows, you can easily scale your resources on Sternhost VPS.

Deploying AI models on a VPS gives you full control, better performance, and cost efficiency compared to cloud platforms. With Sternhost’s VPS hosting, you can seamlessly install TensorFlow or PyTorch, run APIs, and deploy your models securely with SSL and server monitoring.

Leave a Reply

Your email address will not be published.