1. 程式人生 > >.net core 應用程序部署到win10子系統中

.net core 應用程序部署到win10子系統中

cnblogs nsh targe aps win10 http spl document first

聲明:本人是個小白,博文中的知識都是我從網上歸類總結而來,主要是記錄一下我的學習歷程;如果各位網友有什麽更好的參考資料歡迎推薦。

最近在學習【騰飛(Jesse)】 大神的 【ASP.NET Core 快速入門】 教程,在看第一章將 .NET Core 應用程序部署到 Linux 系統時,大神的做法是在vmware上安裝 CentOS 系統,然後配置 Nginx 和 .NET Core 運行環境。由於我的電腦配置比較低,所以我並不想安裝虛擬機,於是就想到了能不能在 win10 的子系統上進行類似的操作呢?雖然以前子系統剛出來的時候我有玩過,但是後面學校學習緊張,後面就慢慢的淡忘了。

下面我就從

1、安裝子系統開始 2、配置 Nginx 環境 3、配置 .NET Core 運行環境 4、發布項目到 Linux 這幾個開始我的記錄。

1、Ubuntu 子系統安裝

打開控制面板搜索啟用和關閉 windows 功能,勾選 適用於 Linux 的 Window 子系統功能,然後就是重啟電腦;重啟電腦後進入 Microsoft Store 搜索 Linux 就可以找到相關的系統;這裏我以下載 Unbuntu 系統為例(主要是商店裏沒有 CentOS 系統)。

技術分享圖片

安裝好之後就可以啟動了。啟動後就可以進入 Unbuntu 系統了,第一次進入需要設置用戶名和密碼( lwi/123 ),設置好後就可以使用了;不過我比較喜歡使用權限最高的 root 賬戶,於是我們開啟它;輸入命令

技術分享圖片
sudo passwd root
開啟 root 賬戶

然後需要輸入密碼:123

技術分享圖片
su root
切換到 root 用戶

切換時也需要輸入密碼:123(我們剛才設置的 root 用戶密碼)。接著我們來查看一下系統的版本,輸入 lsb_release -a ;這個系統版本在後面我們安裝 .NET Core 運行環境時需要安裝對應的版本。

技術分享圖片
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#
查看系統版本

2、安裝並配置 Nginx 環境

技術分享圖片
sudo apt-get install nginx
Ubuntu 環境安裝 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
用 vim 修改 Nginx 配置

用來發布的項目沒有使用新建的 .NET Core Web 項目,而是使用了我最近在學習的【在7樓】大神的 從零開始搭建自己的.NET Core Api框架】項目(非原項目,是我自己跟著大神的腳步慢慢搭建的)。

技術分享圖片
##
# You should look at the following URLs 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
    # Dont 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 Apaches document root
    # concurs with nginxs 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;
#    }
#}
Nginx 配置代碼

3、配置 .NET Core 運行環境

這一步很簡單,我們只用到 .NET Core 的官網找到剛才我們查到對應的 Ubuntu 版本的 .NET Core 運行環境進行安裝豈可。

技術分享圖片

安裝完成後 輸入命令:dotnet --info 就可以查看安裝的 .NET Core 版本信息等.

4、發布項目到 Linux

接著我們用 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/
復制發布的項目代碼到 Ubuntu 網站目錄

走到這一步教程也接近了尾聲。我們只用把網站運行起來就可以訪問到了

技術分享圖片
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.
運行網站代碼

網站運行成功圖:

技術分享圖片

參考資料:

1、Windows10內置Linux子系統初體驗

2、Ubuntu/CentOS 系統上安裝與配置Nginx

3、【從零開始搭建自己的.NET Core Api框架】

4、ASP.NET Core快速入門 (視頻收費)

.net core 應用程序部署到win10子系統中