用.netcore WebApi开发的微信小程序后台,部署到CentOS,步骤如下:
一、安装.netcore环境:
1、sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
2、sudo yum install dotnet-sdk-6.0
3、sudo yum install dotnet-runtime-6.0
4、dotnet --info命令查看安装是否成功,以及.netcore版本
二、部署:
1、vs中发布到文件夹
2、上传到centos指定目录中
3、修改mysql数据库连接串(正式库连接替换测试库连接)
4、进入目录,dotnet xxxx.dll --urls http://*:5000(或https),可启动服务
三、配置自启动服务:
(参考http://www.manongjc.com/detail/29-jkxitysrwytvgzc.html)
1.创建自启动文件(在 /etc/systemd/system/目录里)
vim /etc/systemd/system/testAPI.service
配置文件的内容:
[Unit]
Description=testAPI.service
[Service]
WorkingDirectory=/site/card/api/manage
ExecStart=/usr/share/dotnet/dotnet /xx/xx/testAPI.dll --urls http://*:5000
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=AspnetCore
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=muti-user.target
2.设置为自启动
systemctl enable testAPI.service
3.启动服务
systemctl start testAPI.service
4.查看服务状态
systemctl status testAPI.service
四、配置nginx端口转发:
当我们在服务器上搭建一个图书以及一个电影的应用,其中图书应用启动了 8001 端口,电影应用启动了 8002 端口,但我们一般访问应用的时候都是希望不加端口就访问域名,也即两个应用都通过 80 端口访问,通过不同的二级域名。
1、新增一个nginx的扩展配置文件(可通过复制原有文件):
cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/webapi.conf
2、编辑配置文件:
vim webapi.conf
server {
listen 80; (如果是https就用443)
server_name testapi.freedeman.top; //申请的二级域名
location / {
proxy_pass http://localhost:12345;
}
}
3、配置好了重启一下服务就可以了:
systemctl restart testAPI.service