声明:本人是个小白,博文中的知识都是我从网上归类总结而来,主要是记录一下个人学习历程;若是各位网友有什么更好的参考资料欢迎推荐。php
最近在学习【腾飞(Jesse)】 大神的 【ASP.NET Core 快速入门】 教程,在看第一章将 .NET Core 应用程序部署到 Linux 系统时,大神的作法是在vmware上安装 CentOS 系统,而后配置 Nginx 和 .NET Core 运行环境。因为个人电脑配置比较低,因此我并不想安装虚拟机,因而就想到了能不能在 win10 的子系统上进行相似的操做呢?虽然之前子系统刚出来的时候我有玩过,可是后面学校学习紧张,后面就慢慢的淡忘了。html
下面我就从linux
一、安装子系统开始 二、配置 Nginx 环境 三、配置 .NET Core 运行环境 四、发布项目到 Linux 这几个开始个人记录。nginx
打开控制面板搜索启用和关闭 windows 功能,勾选 适用于 Linux 的 Window 子系统功能,而后就是重启电脑;重启电脑后进入 Microsoft Store 搜索 Linux 就能够找到相关的系统;这里我如下载 Unbuntu 系统为例(主要是商店里没有 CentOS 系统)。vim
安装好以后就能够启动了。启动后就能够进入 Unbuntu 系统了,第一次进入须要设置用户名和密码( lwi/123 ),设置好后就可使用了;不过我比较喜欢使用权限最高的 root 帐户,因而咱们开启它;输入命令windows
sudo passwd root
而后须要输入密码:123bash
su root
切换时也须要输入密码:123(咱们刚才设置的 root 用户密码)。接着咱们来查看一下系统的版本,输入 lsb_release -a ;这个系统版本在后面咱们安装 .NET Core 运行环境时须要安装对应的版本。php7
root@YJ:/var/www/html# lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04.4 LTS Release: 16.04 Codename: xenial root@YJ:/var/www/html#
sudo apt-get install nginx
而后咱们须要配置一下 Nginx ;将等下须要发布的网站目录,映射的端口号等修改一下,因为本机 IIS 已经占用了 80 端口,因此 Nginx 的监听端口我改成了 8080 ,如下就是要用到的一些命令【Nginx 配置文件在 /etc/nginx/sites-available/ 下的 default 文件(其实还有其它地方,这里我就不详解了,由于我也不是很清楚,先按着我找到的来配置好了)】:框架
root@YJ:/# cd /etc/nginx/sites-available/ root@YJ:/etc/nginx/sites-available# sudo vim default
用来发布的项目没有使用新建的 .NET Core Web 项目,而是使用了我最近在学习的【在7楼】大神的 【从零开始搭建本身的.NET Core Api框架】项目(非原项目,是我本身跟着大神的脚步慢慢搭建的)。ide
## # You should look at the following URL's in order to grasp a solid understanding # of Nginx configuration files in order to fully unleash the power of Nginx. # http://wiki.nginx.org/Pitfalls # http://wiki.nginx.org/QuickStart # http://wiki.nginx.org/Configuration # # Generally, you will want to move this file somewhere, and start with a clean # file but keep this around for reference. Or just disable in sites-enabled. # # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. ## # Default server configuration # server { listen 8080 default_server; listen [::]:8080 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; #root /var/www/html; #在这里配置网站路径 root /var/www/IIS/RayPI; # Add index.php to the list if you are using PHP #index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. # try_files $uri $uri/ =404; #设置反向代理 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 $http_host; proxy_cache_bypass $http_upgrade; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php7.0-fpm: # fastcgi_pass unix:/run/php/php7.0-fpm.sock; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # Virtual Host configuration for example.com # # You can move that to a different file under sites-available/ and symlink that # to sites-enabled/ to enable it. # #server { # listen 80; # listen [::]:80; # # server_name example.com; # # root /var/www/example.com; # index index.html; # # location / { # try_files $uri $uri/ =404; # } #}
这一步很简单,咱们只用到 .NET Core 的官网找到刚才咱们查到对应的 Ubuntu 版本的 .NET Core 运行环境进行安装岂可。
安装完成后 输入命令:dotnet --info 就能够查看安装的 .NET Core 版本信息等.
接着咱们用 VS2017 发布咱们的项目,并将发布的文件复制到刚才建好的文件夹下。先进入 Ubuntu 系统,按 win+R 输入 bash 就能够进入系统了,而后咱们切换到 root 用户(命令:su root ),这里补充一下,在 Ubuntu 子系统中是能够直接访问 win10 的系统文件的,它在 /mnt/ 这个目录下,因此咱们不用安装什么相似 winscap 的软件来上传咱们的代码,咱们能够直接从 win10 系统盘中直接复制过来 ;固然 Linux 中有配置软连接的应该也能够实现 (因为我不会,因此我这里就不进行深究了)。下面就是复制的代码
lwi@YJ:/mnt/c/Users/wl$ su root Password: root@YJ:/mnt/c/Users/wl# cp -r /mnt/d/IIS/RayPI/ /var/www/
走到这一步教程也接近了尾声。咱们只用把网站运行起来就能够访问到了
lwi@YJ:/mnt/c/Users/wl$ su root Password: root@YJ:/mnt/c/Users/wl# cd /var/www/IIS/RayPI/ root@YJ:/var/www/IIS/RayPI# dotnet RayPI.dll Hosting environment: Production Content root path: /var/www/IIS/RayPI Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down. warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3] Failed to determine the https port for redirect.
网站运行成功图:
参考资料:
四、ASP.NET Core快速入门 (视频收费)