Have tasted Free dinner on board , Looking out aws The grand goal of building blog Park overseas station , Think about small goals you can achieve right now , I can't help but type out on the screen —— "Hello World!", Let's start with this simple and simple goal —— use ASP.NET Core on .NET 5.0 stay Amazon EC2 The server shows "Hello World!".
Log in to the one you started earlier EC2 Server installation .NET 5.0 SDK
mkdir $HOME/dotnet_install && cd $HOME/dotnet_install curl -H 'Cache-Control: no-cache' -L https://aka.ms/install-dotnet-preview -o install-dotnet-preview.sh sudo bash install-dotnet-preview.sh
Check it out after installation .NET Version of
dotnet --info .NET SDK (reflecting any global.json): Version: 5.0.100-rc.2.20479.15 Commit: da7dfa8840 Runtime Environment: OS Name: ubuntu OS Version: 20.04 OS Platform: Linux RID: ubuntu.20.04-x64 Base Path: /usr/share/dotnet/sdk/5.0.100-rc.2.20479.15/
use dotnet Command to create a template based ASP.NET Core MVC project hello-world
dotnet new mv --no-https --name hello-world
After creation, use dotnet run Command to run the project
ubuntu@ip-172-31-44-65:~/hello-world$ dotnet run Building... warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35] No XML encryptor configured. Key {b11ef41c-0ca0-4673-a6d2-05aa4a2bdb1a} may be persisted to storage in unencrypted form. info: Microsoft.Hosting.Lifetime[0] Now listening on: http://localhost:5000 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development info: Microsoft.Hosting.Lifetime[0] Content root path: /home/ubuntu/hello-world
The successful running , Next use vim Modify the view file , Show "Hello World!" And "Powered by ..." Information
vi Views/Home/Index.cshtml
Change it to the following code
@{ ViewData["Title"] = "Home Page"; } <div class="text-center"> <h1 class="display-4">Hello World!</h1> <p>Powered by @System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription on AWS</p> </div>
Next build Project generation docker Mirror image , Deploy sites with containers .
Install first docker
curl -sSL https://get.docker.com/ | sh
Add the current user to docker Group
sudo usermod -aG docker ubuntu
To write Dockerfile( use multistage build)
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base WORKDIR /app EXPOSE 80 FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build WORKDIR /src COPY *.csproj . RUN dotnet restore COPY . . RUN dotnet build -c Release -o /app FROM build AS publish WORKDIR /src RUN dotnet publish -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "hello-world.dll"]
Based on the above Dockerfile Generate docker Mirror image
$ docker build . -t hello-world Sending build context to Docker daemon 11.48MB Step 1/16 : FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base ---> 3e92f5fcc999 ... Step 16/16 : ENTRYPOINT ["dotnet", "hello-world.dll"] ---> Using cache ---> a2da910535e2 Successfully built a2da910535e2 Successfully tagged hello-world:latest
Use the following command to daemon Mode start hellow-world Containers
docker run -d --net=host --restart unless-stopped hello-world
At this time hello-world The app is already running in the background
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES df7d77140e76 hello-world "dotnet hello-world.…" 2 minutes ago Up 2 minutes dreamy_joliot
This machine curl Command to test
$ curl -I localhost HTTP/1.1 200 OK Date: Sun, 08 Nov 2020 10:47:01 GMT Content-Type: text/html; charset=utf-8 Server: Kestrel
The site can be accessed normally , But now it can only be accessed locally , Make it accessible to the outside , Need to be in aws Add inbound rule to security group, open 80 port .
Here it is EC2 Instance details console , Get into “ Security ” tab, Click on the security group name , Enter the security group console , Click on “ Edit inbound rules ”, Click on “ Add inbound rule ”, Add an open 80 Inbound rules for ports .
After saving the rules and taking effect , You can go through the public network IP Visited . To make this little goal a little more formal , We used a domain name —— optcode.net, adopt dns To this one EC2 Public network of servers IP.
Okay , Browser access http://optcode.net/ , Small goals are accomplished !