假设你已经准备好所有资源:
域名:demo.example.ai
服务器 IP:12.23.34.45
服务器 OS:ubuntu server 22.04
这里以一个简单的 Gradio 的 Chatbot 为例,介绍如何快速在服务器上部署一个 AI Demo,并确保服务在后台持续运行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 import gradio as grimport openaiopenai.api_base = "https://api.openai.com/v1" openai.api_key = "sk-proj-1234567890" with gr.Blocks() as demo: chatbot = gr.Chatbot(type ="messages" ) msg = gr.Textbox() send_btn = gr.Button("发送" ) clear_btn = gr.ClearButton([msg, chatbot]) def respond (message, chat_history ): print (message, chat_history) chat_history.append({"role" : "user" , "content" : message}) messages = [{"role" : "system" , "content" : "You are a helpful assistant." }] messages.extend([{"role" : entry["role" ], "content" : entry["content" ]} for entry in chat_history]) print (messages) response = openai.chat.completions.create( model="gpt-4o-mini" , messages=messages ) bot_message = response.choices[0 ].message.content.strip() chat_history.append({"role" : "assistant" , "content" : bot_message}) return "" , chat_history msg.submit(respond, [msg, chatbot], [msg, chatbot]) send_btn.click(respond, [msg, chatbot], [msg, chatbot]) if __name__ == "__main__" : demo.launch()
1 在服务器上运行服务 为了确保你的 AI Demo 在服务器上持续运行,可以使用 tmux 来启动一个持续运行的服务。
步骤如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 apt install tmux -y tmux new -s demo cd /root/project/ai-demopython app.py tmux attach -t demo
2 域名解析到服务器 假设你已经购买了一个域名,并将其指向你服务器的 IP 地址。
在 DNS 解析中配置一条 A 记录即可,如:
Type:A record
Host: demo.example.ai
Value: 12.23.34.45
保存并等待一分钟。
完成后,你可以通过 ping 命令或者浏览器检查域名是否正确解析到服务器 IP。
1 2 3 4 5 ping demo.example.ai PING demo.example.ai (12.23.34.45): 56 data bytes 64 bytes from 12.23.34.45: icmp_seq=0 ttl=48 time=42.597 ms 64 bytes from 12.23.34.45: icmp_seq=1 ttl=48 time=37.991 ms ...
3 配置 HTTPS 证书 为确保你的服务安全,我们将使用 Certbot 自动获取并配置免费的 SSL 证书。
步骤如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 apt install certbot nginx -y service nginx stop certbot certonly --standalone -d "demo.example.ai" service nginx start
4 配置 Nginx 反向代理到服务 在这一步,我们将配置 Nginx 将外部的 HTTP/HTTPS 请求转发到你正在运行的服务。
步骤如下:
🔔 需要把 demo.example.ai 替换为你的域名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 vim /etc/nginx/sites-available/demo.example.ai server { listen 80; listen [::]:80; server_name demo.example.ai; rewrite ^(.*)$ https://demo.example.ai:443$1 permanent; } server { listen 443 ssl; listen [::]:443 ssl; ssl_certificate /etc/letsencrypt/live/demo.example.ai/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/demo.example.ai/privkey.pem; keepalive_timeout 70; server_name demo.example.ai; server_tokens off; fastcgi_param HTTPS on; fastcgi_param HTTP_SCHEME https; location / { proxy_pass http://127.0.0.1:7860; proxy_buffering off; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade ; proxy_set_header Connection "upgrade" ; proxy_set_header Host $host ; proxy_set_header X-Forwarded-Host $host ; proxy_set_header X-Forwarded-Proto $scheme ; } } cd /root/software/git clone https://github.com/perusio/nginx_ensite.git cd nginx_ensitesudo make install nginx_ensite demo.example.ai service nginx reload
这样,Nginx 就会把 HTTP 请求转发到你服务的端口,确保外部访问是通过域名而不是直接通过 IP。
完成!现在访问 demo.example.ai 即可看到你的 AI Demo。