ASP.Net Core应用部署到Linux服务器

ASP.Net Core应用部署到Linux服务器

注:本文测试基于Deepin Linux系统

1、发布

将项目文件发布到windows系统的某个目录底下,详细操作可参考ASP.Net Core程序发布;

2、上传服务器

将发布好的项目文件打包上传到Linux服务器,可参照SecureCRT上传文件到服务器,为了更好地理解,假定将文件AngularDemo01.tar上传到服务器的/usr/publish/Angular路径下;

3、运行

打开终端,进行上传目录

cd /usr/publish

对上传的文件目录进行赋权

sudo chmod -R 777 Angular

找到压缩包若上传的是压缩包,则先进行解压缩

cd Angular
sudo tar -xvf AngularDemo01.tar

进行解压后的文件,运行项目

sudo dotnet AngularDemo01.dll

注:该AngularDemo01.dll必须是在发布的目录底下的dll文件,如发布的路径为bin/Release/netcoreapp3.1/publish/,则为publish文件夹底下的dll

打开浏览器,输入访问的地址

http://localhost:5000

若项目正常,则可继续往下走,否则,请先处理异常

异常处理参考:

  • The SPA default page middleware can not return the default page “index.html”

4、反向代理

现在在Linux系统上已经可以正常访问了,如果需要在window下也可以访问得到,那么可以通过nginx进行反向代理。

安装nginx

sudo apt-get install nginx

安装成功后,启动nginx

systemctl start nginx

启动成功后,可以设置nginx为开机自启(Linux宕机、重启会自动运行nginx而不需要手动输入命令启动)

systemctl enable nginx

可以在window系统中访问Linux系统的IP,测试nginx是否可以访问。若正常访问,则可以修改nginx配置文件进行ASP.Net Core应用的转发。

修改/etc/nginx/conf.d/default.conf文件

server {listen 80;location / {proxy_pass http://localhost:5000;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection keep-alive;proxy_set_header Host $host;proxy_cache_bypass $http_upgrade;}
}

执行以下代码重新加载nginx配置文件

nginx -s reload

再次运行ASP.Net Core应用程序,这时,就可以在window上直接访问到应用了。

若出现以下错误:

img

可能是由于SELinux保护机制导致,将nginx添加到SELinux的白名单即可。

方案参考:

  • 将ASP.NET Core应用程序部署至生产环境中(CentOS7)

5、守护服务(Supervisor)

目前我们的项目是通过终端运行的,如果关闭终端,程序也将停止运行,从而导致无法访问。可以通过配置守护服务来监听应用的运行情况,当应用停止运行时立即进行重启。

安装SuperVisor

sudo apt-get python-setuptools
easy_install supervisor

配置SuperVisor

mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf

修改supervisord.conf文件,在最后面添加一下代码

[include]
files = conf.d/*.conf

然后重新加载配置文件使其生效

supervisorctl reload

6、配置ASP.Net Core程序的守护

在/etc/supervisor/conf.d目录下创建conf文件,如DataMining.conf,在该文件中添加一下内容:

[program:DataMining]
command=sudo dotnet AngularDemo01.dll ;
directory=/usr/publish/DataMining/netcoreapp3.1/publish/ ;
autorestart=true ;
stderr_logfile=/var/log/DataMining.err.log ;
stdout_logfile=/var/log/DataMining.out.log ;
environment=ASPNETCORE_ENVIRONMENT=Production ;
user=root ; 
stopsignal=INT

保存并退出,然后在终端中运行以下代码

supervisord -c /etc/supervisor/supervisord.conf
ps -ef | grep DataMining

如果存在dotnet AngularDemo01.dll进程则表示运行成功,这时就可以在浏览器上进行访问了。

错误处理参考:

  • unix:///tmp/supervisor.sock no such file
  • unknown error making dispatchers for ‘xxx’: EACCES
  • Can’t drop privilege as nonroot user

7、配置supervisor开机启动

在/usr/lib/systemd/system目录新增supervisord.service服务,在服务文件中增加以下代码:

# dservice for systemd (Deepin)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon[Service]
Type=forking
ExecStart=supervisord -c /etc/supervisor/supervisord.conf
ExecStop=supervisorctl shutdown
ExecReload=supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s[Install]
WantedBy=multi-user.target

然后执行以下命令设置supervisord.service服务自启动

systemctl enable supervisord

可执行以下命令验证supervisord.service是否为开机启动

systemctl is-enabled supervisord

[Install]
WantedBy=multi-user.target

然后执行以下命令设置supervisord.service服务自启动

systemctl enable supervisord

可执行以下命令验证supervisord.service是否为开机启动

systemctl is-enabled supervisord


![img](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91c2VyLWdvbGQtY2RuLnhpdHUuaW8vMjAyMC80LzE3LzE3MTg3NWRmZTA5ZGY5YTM?x-oss-process=image/format,png)


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部