live2d-wiget 看板娘后台本地化部署 使用nginx 加 php 部署

大佬们的项目,本篇只做 live2d 本地化api 的 项目部署记录
live2d 看板娘 :https://github.com/Ylanw/live2d-widget
live2d 本地化api: https://github.com/fghrsh/live2d_api

以下采用ubuntu 系统 做 环境部署, 使用 nginx、 php8、 php-fpm 用来搭建环境

要在Linux系统上安装PHP 8并支持Nginx部署,你可以按照以下步骤进行操作。这里以Ubuntu 22.04为例进行说明。如果你使用的是其他版本的Ubuntu或Debian,大部分步骤应该是相似的。

更新系统

sudo apt update
sudo apt upgrade

安装依赖

sudo apt install -y build-essential zlib1g-dev libssl-dev libxml2-dev libbz2-dev libjpeg-dev libpng-dev libonig-dev libfreetype6-dev libwebp-dev libzip-dev

添加PPA仓库

sudo add-apt-repository ppa:ondrej/php
sudo apt update
add-apt-repository 如无报错请忽视

add-apt-repository 命令在 Ubuntu 和其衍生版本中用于添加新的 PPA(个人包文件档案)到系统。如果你在使用这个命令时遇到“找不到命令”的错误,那么可能是因为 software-properties-common 包没有安装,或者你正在使用的系统中这个命令不可用。

sudo apt update
sudo apt install software-properties-common

安装PHP 8

sudo apt install -y php8.1-cli php8.1-common

安装 PHP8 的扩展

sudo apt install -y php8.1-fpm php8.1-mysql php8.1-gd php8.1-mbstring php8.1-curl
配置Nginx
sudo apt install -y nginx
nginx配置文件

查看对应 nginx资源目录
ubuntu 大多在 /etc/nginx/conf.d/ 创建 xxxx.conf 将以下信息放入
nginx 整个配置文件 可以直接丢进到 nginx/conf.d 里面,上面 server块可以忽视以及站点配置文件可以忽视

server {
        listen 82;
        server_name localhost;
        # 采用 https 可以启用
        # rewrite ^(.*)$ https://${server_name}$1 permanent;
        
        location /{
					# 开启CORS支持
					add_header 'Access-Control-Allow-Origin' '*' always;
					add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
					add_header 'Access-Control-Max-Age' 1728000 always;
					add_header 'Access-Control-Allow-Credentials' 'true' always;
					add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;

					# 针对预检请求的特殊处理
					if ($request_method = 'OPTIONS') {
							add_header 'Content-Type' 'text/plain charset=UTF-8';
							add_header 'Content-Length' 0;
							 return 204;
					}
					# 指定静态资源的根目录,root后可设置相对路径,也可以设置绝对路径,根据需求设定
				root   /etc/nginx/html/live2d;

        
		}

		location ~* ^/(get|add|rand|rand_textures|switch|switch_textures|tools)(/?.*)$ {
			# 开启CORS支持
			add_header 'Access-Control-Allow-Origin' '*' always;
			add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
			add_header 'Access-Control-Max-Age' 1728000 always;
			add_header 'Access-Control-Allow-Credentials' 'true' always;
			add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;

			# 针对预检请求的特殊处理
			if ($request_method = 'OPTIONS') {
					add_header 'Content-Type' 'text/plain charset=UTF-8';
					add_header 'Content-Length' 0;
				 return 204;
			}
			# 跟上面 php-fpm 对应
			fastcgi_pass unix:/run/php/php-fpm.sock;
			fastcgi_index index.php;
			# 修改自己项目的对应目录, 项目存放在/etc/nginx/html/live2d/ 下面
			fastcgi_param SCRIPT_FILENAME /etc/nginx/html/live2d/$fastcgi_script_name;
			include fastcgi_params;			
		}
		
		

        error_page   500 502 503 504  /50x.html;  # 指定报错页面

        location = /50x.html {
            root   html;
        }
}
server {
       listen  446 ssl; #监听端口
       server_name  localhost; #请求域名
       ssl on; #开启ssl
       ssl_certificate      /etc/nginx/conf.d/cert/example.com.pem; #pem证书路径
       ssl_certificate_key  /etc/nginx/conf.d/cert/example.com.key; #pem证书key路径
       ssl_session_timeout     5m; #会话超时时间
       ssl_ciphers     ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #加密算法
       ssl_protocols   TLSv1 TLSv1.1 TLSv1.2; #SSL协议

     location /{
					# 开启CORS支持
					add_header 'Access-Control-Allow-Origin' '*' always;
					add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
					add_header 'Access-Control-Max-Age' 1728000 always;
					add_header 'Access-Control-Allow-Credentials' 'true' always;
					add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;

					# 针对预检请求的特殊处理
					if ($request_method = 'OPTIONS') {
							add_header 'Content-Type' 'text/plain charset=UTF-8';
							add_header 'Content-Length' 0;
							 return 204;
					}
					# 指定静态资源的根目录,root后可设置相对路径,也可以设置绝对路径,根据需求设定
				root   /etc/nginx/html/live2d;

        
		}

		location ~* ^/(get|add|rand|rand_textures|switch|switch_textures|tools)(/?.*)$ {
			# 开启CORS支持
			add_header 'Access-Control-Allow-Origin' '*' always;
			add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
			add_header 'Access-Control-Max-Age' 1728000 always;
			add_header 'Access-Control-Allow-Credentials' 'true' always;
			add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;

			# 针对预检请求的特殊处理
			if ($request_method = 'OPTIONS') {
					add_header 'Content-Type' 'text/plain charset=UTF-8';
					add_header 'Content-Length' 0;
				 return 204;
			}
			# 跟上面 php-fpm 对应
			fastcgi_pass unix:/run/php/php-fpm.sock;
			fastcgi_index index.php;
			# 修改自己项目的对应目录, 项目存放在/etc/nginx/html/live2d/ 下面
			fastcgi_param SCRIPT_FILENAME /etc/nginx/html/live2d/$fastcgi_script_name;
			include fastcgi_params;			
		}

       error_page   500 502 503 504  /50x.html;  # 指定报错页面

        location = /50x.html {
            root   html;
        }


}

配置PHP-FPM

配置对应文件
编辑PHP-FPM配置文件:

sudo vim /etc/php/8.1/fpm/pool.d/www.conf

编辑PHP-FPM的主配置文件 www.conf

listen = /run/php/php-fpm.sock
// 放开权限, 常用文件 报安全验证 Access denied 在这里面添加对应文件的后缀
security.limit_extensions = .php .php3 .php4 .php5 .php7 .json .moc .min .png .mtn

启动并启用Nginx和PHP-FPM,并加入系统自启动

sudo systemctl start nginx
sudo systemctl enable nginx

sudo systemctl start php8.1-fpm
sudo systemctl enable php8.1-fpm

验证安装

php -v

你应该看到类似以下输出:

PHP 8.1.12 (cli) (built: Jul  1 2023 16:47:22) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.12, Copyright (c) Zend Technologies

此时就可以将此地址做为 live2d 后台api 提供给前端使用, 修改前端对应的autoload.js

// 加载 waifu.css live2d.min.js waifu-tips.js
if (screen.width >= 768) {
	Promise.all([
		loadExternalResource(live2d_path + "waifu.css", "css"),
		loadExternalResource(live2d_path + "live2d.min.js", "js"),
		loadExternalResource(live2d_path + "waifu-tips.js", "js")
	]).then(() => {
		// 配置选项的具体用法见 README.md
		// D
		initWidget({
			waifuPath: live2d_path + "waifu-tips.json",
			// localhost 改成自己服务器的ip地址
			apiPath: "http://localhost:82/",
			// 官方提供 cdn 直接使用
			// cdnPath: "https://fastly.jsdelivr.net/gh/fghrsh/live2d_api/",
			tools: ["hitokoto", "asteroids", "switch-model", "switch-texture", "photo", "info", "quit"]
		});
	});
}

保存并关闭文件。
注意事项

  • 安全性:确保你的服务器防火墙设置允许HTTP和HTTPS流量。
  • 配置文件:根据你的具体需求,可能需要调整Nginx的配置文件。
  • 版本选择:如果你需要使用PHP 8.2或更高版本,只需将php8.1替换为对应的版本号即可。
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值