Professional Documents
Culture Documents
This guide shows you how to install ASP.NET Core on your Linux server and how to use it to create a web
application. Then, it walks you through the steps for deploying your application using NGINX.
On Debian:
wget https://packages.microsoft.com/config/debian/10/packages-microsoft-
prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
On Ubuntu:
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-
microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
4. Install the APT package allowing you to use repositories over HTTPS and update APT’s indices
again.
5. sudo apt install apt-transport-https
6. sudo apt update
Replace 5.0 with the latest version of the .NET Core SDK available, which you can find by running
the following command:
4. cd example-app
Unless noted otherwise, all subsequent commands in this guide assume you are still in the
application’s directory.
5. Run the application.
.NET Core serves the application on localhost port 5001. To visit the application remotely, you
can use an SSH tunnel:
On Windows, you can use the PuTTY tool to set up your SSH tunnel. Follow the
appropriate section of the Using SSH on Windows guide, replacing the example port
number there with 5001.
On OS X or Linux, use the following command to set up the SSH tunnel. Replace example-
user with your username on the application server and 192.0.2.0 with the server’s IP
address.
7. Now you can visit the application in your browser by navigating to https://localhost:5001.
Note
.NET Core serves your application over HTTPS. When visiting the application, you browser may
warn you that the SSL certificate is self-signed. Choose to proceed anyway.
.NET Core uses Razor as its template engine for web application interfaces. You can learn more about
using Razor in your web application from Microsoft’s Get started with Razor Pages in ASP.NET
Core guide. .NET Core also has extensive support for developing applications using the Model–View–
Controller (MVC) architecture. If you want to learn more about MVC, checkout the Microsoft’s Get started
with ASP.NET Core MVC guide.
The steps in this section show you how to set up NGINX as the reverse proxy server for your .NET Core
application.
3. Create a /etc/nginx/proxy.conf file, and add the contents of the example file:
File: /etc/nginx/proxy.conf
1 proxy_redirect off;
2 proxy_set_header Host $host;
3 proxy_set_header X-Real-IP $remote_addr;
4 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
5 proxy_set_header X-Forwarded-Proto $scheme;
6 client_max_body_size 10m;
7 client_body_buffer_size 128k;
8 proxy_connect_timeout 90;
9 proxy_send_timeout 90;
10 proxy_read_timeout 90;
11 proxy_buffers 32 4k;
12
4. Open the NGINX configuration file — /etc/nginx/nginx.conf — and replace its contents with the
following:
File: /etc/nginx/nginx.conf
1 user www-data;
2 worker_processes auto;
3 pid /run/nginx.pid;
4 include /etc/nginx/modules-enabled/*.conf;
5
6 events {
7 worker_connections 768;
8 # multi_accept on;
9 }
10
11 http {
12 include /etc/nginx/proxy.conf;
13 limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;
14 server_tokens off;
15
16 sendfile on;
17 keepalive_timeout 29;
18 client_body_timeout 10; client_header_timeout 10; send_timeout 10;
19
20 upstream example-app{
21 server localhost:5000;
22 }
23
24 server {
25 listen 443 ssl http2;
26 listen [::]:443 ssl http2;
27 server_name example.com;
28
29 add_header X-Frame-Options DENY;
30 add_header X-Content-Type-Options nosniff;
31
32 location / {
33 proxy_pass http://example-app;
34 limit_req zone=one burst=10 nodelay;
35 }
36 }
37 }
38
1. Install the Snap Store. Snap provides application bundles that work across major Linux
distributions. If you are using Ubuntu, Snap should already be installed (since version 16.04):
Certbot prompts you to select from the NGINX sites configured on your machine. Select the one
with your domain name.
12. Certbot includes a cron job that automatically renews your certificate before it expires. You can
test the automatic renewal with the following command:
1 # [...]
2
3 server {
4 listen 443 ssl http2;
5 listen [::]:443 ssl http2;
6 server_name example.com *.example.com;
7 ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
8 ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
9
10 add_header X-Frame-Options DENY;
11 add_header X-Content-Type-Options nosniff;
12
13 # [...]
14
2. Verify the NGINX configuration. Then, assuming the test passes, restart NGINX.
3. sudo nginx -t
4. sudo systemctl restart nginx
5. You can test NGINX’s routing to the application by running the application directly.
1 // [...]
2
3 using Microsoft.AspNetCore.HttpOverrides;
4
5 // [...]
6
7 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
8 {
9 app.UseForwardedHeaders(new ForwardedHeadersOptions
10 {
11 ForwardedHeaders = ForwardedHeaders.XForwardedFor |
12 ForwardedHeaders.XForwardedProto
13 });
14
15 app.UseAuthentication();
16
17 // [...]
18 }
19
20 // [...]
The output should indicate the location of an example-app.dll file. Take note of that location, as
it is used in the example-app.service file created below. It should be similar
to /bin/Release/net5.0/example-app.dll.
4. Copy your project to the /var/www directory. This is a conventional place to store your production
application, but it also allows you to separate your production and working versions of the
application.
1 [Unit]
2 Description=Example .NET Core Web Application
3
4 [Service]
5 WorkingDirectory=/var/www/example-app
6 ExecStart=/usr/bin/dotnet /var/www/example-app/bin/Release/net5.0/example-app.dll
7 Restart=always
8 RestartSec=10
9 KillSignal=SIGINT
10 SyslogIdentifier=dotnet-example-app
11 User=www-data
12 Environment=ASPNETCORE_ENVIRONMENT=Production
13 Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
14
15 [Install]
16 WantedBy=multi-user.target
17
Run the Application
1. Enable the systemd service for the published application, and then start it up.
Conclusion
You have now successfully created and deployed a .NET Core application. You can continue your journey
and learn more about using .NET for web-application development by following
Microsoft’s Recommended learning path.