Ubuntu 18.04 安装部署Net Core、Nginx全过程

时间:2023-04-30 09:51:14

Ubuntu 18.04 安装部署Net Core、Nginx全过程

环境配置

Ubuntu 18.04 ,Nginx,.Net Core 2.1, Let's Encrypt

更新系统

sudo apt-get update

sudo apt-get upgrade

安装Net Core

参考文档地址:https://www.microsoft.com/net/learn/dotnet/hello-world-tutorial#install

  • Register Microsoft key and feed
    wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
  • Install .NET SDK
   sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-2.1
  • 创建 ASP.NET Core 演示网站并发布(此部分根据实际需要修改)
    # 下载一个演示型站点
git clone https://github.com/linezero/NETCoreBBS
vim NETCoreBBS/src/NetCoreBBS/Program.cs # 默认为 80 端口,这里修改为 8000 端口
.UseUrls("http://*:8000") # 编译并发布
$ cd NETCoreBBS
$ dotnet restore
$ dotnet publish
$ sudo dotnet publish -c Release -o /var/bbs.xyfy.loan #发布到指定目录
  • 部署并后台运行网站
    # 进入发布后的位置,开始运行
$ cd /var/bbs.xyfy.loan
# 安装 Supervisor
$ sudo apt-get install supervisor
# 配置 Supervisor
$ sudo vim /etc/supervisor/conf.d/NetCoreBBS.conf [program:NetCoreBBS] directory=/var/bbs.xyfy.loan
command=dotnet NetCoreBBS.dll
autostart=true
autorestart=true
startsecs=10
startretries=50
stderr_logfile=/var/log/NetCoreBBS.err.log
stdout_logfile=/var/log/NetCoreBBS.out.log
environment=ASPNETCORE__ENVIRONMENT=Production
user=root
stopsignal=INT # 重启supervisor
$ sudo /etc/init.d/supervisor restart
# 如果一切配置正确,查看相应日志你可以看到类似以下输出,表示应用已正常启动。
$ tail -f /var/log/NetCoreBBS.out.log
Hosting environment: Production
Content root path: /root/dotnet/NETCoreBBS/src/NetCoreBBS/bin/Debug/netcoreapp2.0/publish
Now listening on: http://[::]:8000
Application started. Press Ctrl+C to shut down.

安装Nginx


# 安装
$ sudo apt-get install nginx

安装配置Let's Encrypt [此处作废,最好只生成证书,而不对nginx配置做出修改,如果你只有一个站点,并且不需要反向代理,可以使用此方法]

  • Installing Certbot 安装Certbot
    # 安装Certbot
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python3-certbot-nginx # install Certbot's Nginx package
  • Setting up Nginx
    # 配置域名
$ sudo vim /etc/nginx/sites-available/default
# 寻找server_name这行配置然后使用自己的域名替换修改下划线_
# server_name www.xyfy.loan bbs.xyfy.loan; # 测试配置
$ sudo nginx -t
# 启用配置
$ sudo service nginx reload
  • Obtaining an SSL Certificate 申请ssl证书
    # 申请证书
$ sudo certbot --nginx -d www.xyfy.loan -d bbs.xyfy.loan
$ 尝试证书自动续期
$ sudo certbot renew --dry-run

配置Nginx反向代理[证书是另外生成的]

    # 反向代理
$ sudo vi /etc/nginx/sites-enabled/www.xyfy.loan.conf
server {
listen 80 ;
server_name www.xyfy.loan;
return 301 https://$server_name$request_uri;
}
##
upstream wwwxyfyloan {
#ip_hash;
server localhost:8000;
#server 172.18.28.109:8001;
ip_hash;
}
##
server {
listen 443 ssl;
server_name www.xyfy.loan; ssl_certificate certs/www.xyfy.loan.crt;
ssl_certificate_key certs/www.xyfy.loan.key;
location / {
proxy_pass http://wwwxyfyloan;
}
}
    # 符号连接
$ cd /etc/nginx/sites-enabled
$ sudo ln -s /etc/nginx/sites-available/www.xyfy.loan.conf www.xyfy.loan.conf
# 重启
$ sudo nginx -t
$ sudo service nginx reload