Table of Contents
- Introduction
- Prerequisites
- Installing .NET Core on Ubuntu
- Creating a .NET Core Application
- Building the Application
- Publishing the Application
- Installing Nginx Web Server
- Configuring Nginx as a Reverse Proxy
- Setting Up Systemd Service
- Securing the Application with HTTPS
- Monitoring and Logging
- Troubleshooting Common Issues
- Conclusion
- FAQs
1. Introduction
Deploying a .NET Core application on Ubuntu can be a seamless process if done correctly. This step-by-step guide will walk you through the process of setting up and deploying a .NET Core application on an Ubuntu server. We will cover the installation of .NET Core, creating and building the application, setting up a reverse proxy using Nginx, and securing the application with HTTPS.
2. Prerequisites
Before we begin, ensure that you have the following prerequisites in place:
- A VPS or dedicated server running Ubuntu 18.04 or later.
- A non-root user with sudo privileges.
- Basic knowledge of working with the Linux command line.
3. Installing .NET Core on Ubuntu
In this section, we will install .NET Core on our Ubuntu server. Follow these steps:
- Update the package index:
sudo apt update
- Install the necessary dependencies:
sudo apt install curl libunwind8 gettext
- Download the .NET Core SDK:
curl -O https://download.visualstudio.microsoft.com/download/pr/12345678/abcdefghij/dotnet-sdk-3.1.100-linux-x64.tar.gz
(Replace the URL with the latest version available) - Extract the downloaded archive:
sudo tar -xvf dotnet-sdk-3.1.100-linux-x64.tar.gz
(Replace the filename with the one you downloaded) - Move the .NET Core files to the appropriate location:
sudo mv dotnet /usr/local/
4. Creating a .NET Core Application
Now that we have .NET Core installed, let's create a new .NET Core application:
- Create a new directory for your application:
mkdir myapp
- Navigate to the newly created directory:
cd myapp
- Create a new .NET Core application:
dotnet new web
5. Building the Application
In this section, we will build the .NET Core application:
- Restore the project dependencies:
dotnet restore
- Build the project:
dotnet build
6. Publishing the Application
To deploy the application, we need to publish it:
- Publish the application for the desired runtime:
dotnet publish -c Release -o /var/myapp
7. Installing Nginx Web Server
Nginx will act as a reverse proxy for our .NET Core application. Let's install Nginx:
- Install Nginx:
sudo apt install nginx
- Start Nginx:
sudo systemctl start nginx
- Enable Nginx to start on boot:
sudo systemctl enable nginx
8. Configuring Nginx as a Reverse Proxy
Configure Nginx to reverse proxy requests to our .NET Core application:
- Create a new Nginx configuration file:
sudo nano /etc/nginx/sites-available/myapp
- Add the following configuration:
nginx
server {
listen 80;
server_name your_domain.com www.your_domain.com;
location / {
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 $host;
proxy_cache_bypass $http_upgrade;
}
}
- Create a symbolic link to enable the configuration:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
- Test Nginx configuration for syntax errors:
sudo nginx -t
- Reload Nginx:
sudo systemctl reload nginx
9. Setting Up Systemd Service
To manage our .NET Core application as a service, create a systemd unit file:
- Create the unit file:
sudo nano /etc/systemd/system/myapp.service
- Add the following content:
[Unit]
Description=My .NET Core App
[Service]
WorkingDirectory=/var/myapp
ExecStart=/usr/local/dotnet /var/myapp/myapp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
SyslogIdentifier=myapp
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
- Start and enable the service:
sudo systemctl start myapp
- Enable the service to start on boot:
sudo systemctl enable myapp
10. Securing the Application with HTTPS
To secure our application with HTTPS, we will use Let's Encrypt SSL certificates:
- Install Certbot:
sudo apt install certbot python3-certbot-nginx
- Obtain SSL certificate:
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
- Certbot will automatically configure Nginx to use the SSL certificate.
11. Monitoring and Logging
Monitoring and logging are essential for maintaining a healthy application:
- Install necessary tools for monitoring:
sudo apt install htop
- View application logs:
sudo journalctl -u myapp
12. Troubleshooting Common Issues
Encountering issues? Here are some common troubleshooting tips:
- Check application logs for errors.
- Verify Nginx configuration for any syntax errors.
- Ensure firewall settings allow incoming connections on ports 80 and 443.
- Restart the application and services after making changes.
13. Conclusion
Congratulations! You have successfully deployed your .NET Core application on Ubuntu. Following this step-by-step guide, you learned how to install .NET Core, create and build a .NET Core application, configure Nginx as a reverse proxy, and secure the application with HTTPS. You are now ready to serve your .NET Core application to the world.
14. FAQs
1. How do I update my .NET Core application on Ubuntu?
To update your .NET Core application, follow these steps:
- Pull the latest changes from your version control system.
- Rebuild and republish the application using
dotnet build
anddotnet publish
commands. - Restart the .NET Core service using
sudo systemctl restart myapp
.
Top comments (0)