搭建部署live2d api
封面《春音アリス*グラム》

关于moc3模型的支持请参考这篇文章live2d-widget-moc3

引言

想要根据live2d api搭建自己的api。我想要使用docker方便部署,但是不会php和nginx环境搭建,还对docker比较陌生。因此写下这篇文章。因为这三件都不是很熟悉,轻喷。

docker配置

首先将live2d api仓库克隆下来

1
2
git clone https://github.com/fghrsh/live2d_api.git
cd live2d_api

docke-compose编写

创建docker-compose.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
version: "3.7"

services:
php:
image: php:8-fpm
volumes:
- ./:/var/www/html
nginx:
image: nginx:latest
restart: always
ports:
- 80:80
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./:/usr/share/nginx/html

nginx配置

创建nginx.conf文件,注意中间的跨域请求配置,同时server name这些请根据自己的需求配置

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#user nobody;
worker_processes auto;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;

#跨域
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'Content-Type';

location / {
index index.php;
autoindex off;
}

#转发
location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
root /var/www/html;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

部署

使用docker部署比较简单

1
2
3
docker compose up -d
#或者可以用下面的
docker-compose up -d

测试

部署后使用浏览器打开http://localhost/get/?id=1-23结果如下
get api
测试使用stevenjoezhang的live2d-widget,将其中的apiPath替换为http://localhost/,打开demo.html,结果如下,可以看到请求的url为localhost,api测试成功。
api test

总结

本次完成了对live2d api的docker化。但是只在本地服务器上进行了测试,后续将对远程服务器进行测试。