分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

centos7+nginx部署asp.net core mvc网站

发布时间:2023-09-06 02:00责任编辑:沈小雨关键词:nginx

1. 安装 .net core

可参见官网安装教程。

  1. 选择Linux发行版本为Centos/Oracle
  2. 添加dotnet的yum仓库配置

    $ sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
  3. 安装.net SDK

    #更新系统软件sudo yum update #安装dotnet sdksudo yum install dotnet-sdk-2.1
  4. 使用dotnet --info命令显示 .net core 的信息。

2. 安装nginx

可参见官网安装配置教程。

  1. 配置yum仓库,创建/etc/yum.repos.d/nginx.repo,并配置为以下内容:
[nginx]name=nginx repo#baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/#OS为rhel或centos#OSRELEASE为centos版本,6或7baseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=0enabled=1
  1. 获取公钥文件
wget nginx.org/keys/nginx_signing.key
  1. 导入公钥文件
rpm --import nginx_signing.key
  1. 安装nginx
sudo yum -y install nginx

3. 将nginx设置为开机启动

  1. 在系统服务目录里创建nginx.service文件,并将文件内容设置如下:
#服务的说明[Unit]#描述服务Description=nginx#描述服务类别After=network.target#服务运行参数的设置 ?[Service]#Type=forking是后台运行的形式Type=forking#服务的具体运行命令ExecStart=/usr/local/nginx/sbin/nginx#重启命令ExecReload=/usr/local/nginx/sbin/nginx -s reload#停止命令ExecStop=/usr/local/nginx/sbin/nginx -s quit#PrivateTmp=True表示给服务分配独立的临时空间PrivateTmp=true #运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3 ?[Install]WantedBy=multi-user.target
  1. 设置为开机启动
systemctl enable nginx.service
  1. 其他管理命令
#启动nginx服务systemctl start nginx.service #停止开机自启动systemctl disable nginx.service#查看服务当前状态systemctl status nginx.service#重新启动服务systemctl restart nginx.service #查看所有已启动的服务systemctl list-units --type=service

4. 配置nginx

  1. 增加Nginx配置文件myapp.conf,并做如下配置:
 proxy_set_header ?????Host $http_hostserver { ???#监听端口号 ???listen ??????8081; ???#监听地址 ???server_name ?localhost 192.168.1.202; ???#charset koi8-r; ???#access_log ?/var/log/nginx/host.access.log ?main; ???????#设置代理 ???location / { ???????#Kestrel上运行的asp.net core mvc网站地址 ???????proxy_pass ???????????http://localhost:8080; ???????proxy_http_version ???1.1; ???????proxy_set_header ?????Upgrade $http_upgrade; ???????proxy_set_header ?????Host $http_host; ???????proxy_cache_bypass ???$http_upgrade; ???} ???#error_page ?404 ?????????????/404.html; ???# redirect server error pages to the static page /50x.html ???# ???error_page ??500 502 503 504 ?/50x.html; ???location = /50x.html { ???????root ??/usr/share/nginx/html; ???} ???# proxy the PHP scripts to Apache listening on 127.0.0.1:80 ???# ???#location ~ \.php$ { ???# ???proxy_pass ??http://127.0.0.1; ???#} ???# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 ???# ???#location ~ \.php$ { ???# ???root ??????????html; ???# ???fastcgi_pass ??127.0.0.1:9000; ???# ???fastcgi_index ?index.php; ???# ???fastcgi_param ?SCRIPT_FILENAME ?/scripts$fastcgi_script_name; ???# ???include ???????fastcgi_params; ???#}
  1. 重新载入nginx
nginx -s reload

5. 发布网站,配置服务和防火墙

  1. 发布网站后将发布后的网站上传到服务器。
  2. 使用dotnet myapp.dll启动网站。
  3. 在浏览器中打开网站的地址,检测网站正常启动并可访问。
  4. 如果需要外网访问需要在防火墙上开放相应的端口。具体操作可参见:
#添加80端口:firewall-cmd --zone=public --add-port=80/tcp --permanent#删除防火墙端口: firewall-cmd --zone=public --remove-port=12345/tcp --permanent#重新加载防火墙: firewall-cmd --reload#重启防火墙:systemctl stop firewalld ?systemctl start firewalld
  1. 将发布的网站配置为服务,可参见nginx配置为开机启动。具体如下:
#新建配置文件touch /etc/systemd/system/myapp.service#编辑配置文件:[Unit]Description=myapp web[Service]WorkingDirectory=/home/myapp/webExecStart=/usr/bin/dotnet /home/myapp/web/WebPortals.dllRestart=alwaysRestartSec=10 ?# Restart service after 10 seconds if dotnet service crashesSyslogIdentifier=myappEnvironment=ASPNETCORE_ENVIRONMENT=ProductionEnvironment=DOTNET_PRINT_TELEMETRY_MESSAGE=false[Install]WantedBy=multi-user.target#服务开机启动systemctl enable myapp.service#启动服务systemctl start myapp.service#查看服务状态systemctl status myapp.service

centos7+nginx部署asp.net core mvc网站

原文地址:https://www.cnblogs.com/yanziwen/p/9206491.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved