Asp net core deploy linux
How to Run an ASP.NET Core Website in Production on Ubuntu Linux
With the release of .NET Core and ASP.NET Core it’s now possible to run modern web applications written in C# on Linux. In this post, I will show you how to configure a Ubuntu server to run an ASP.NET Core site in production.
This post will cover:
- What is ASP.NET Core?
- How to install .NET Core on Ubuntu.
- Running an ASP.NET Core website on Ubuntu.
- Configure NGINX as a Reverse Proxy to ASP.NET Core.
- Configure ASP.NET Core as a Background Service.
What is ASP.NET Core?
ASP.NET Core is a rewrite of ASP.NET and now runs on multiple operating systems such as Windows, OS X, Linux etc. Here is an excerpt of how the ASP.NET team describe it.
ASP.NET Core is a new open-source and cross-platform framework for building modern cloud based internet connected applications, such as web apps, IoT apps and mobile backends. ASP.NET Core apps can run on .NET Core or on the full .NET Framework. – Introduction to ASP.NET Core
I think it goes without saying the benefits this brings to the whole development community. Businesses that use Windows can save costs on server licenses by running their apps on Linux.
How to Install .NET Core on Ubuntu
In order to run ASP.NET Core on Linux we need to install .NET Core. Follow the steps below to install .NET Core on your required Ubuntu version.
Install ASP.NET Core on Ubuntu 14.04
Install ASP.NET Core on Ubuntu 16.04
Running an ASP.NET Core Website on Ubuntu
The first step is to prepare the output directory that our test project will be published to.
The first command creates the directory, the second command changes the group to www-data and the last command makes sure that new files and folders inside the directory will be assigned the www-data group on creation. The reason we assign www-data to the directory is because it is safer to run the app as the www-data user.
Next we will download the samples from the ASP.NET GitHub repository and then publish the sample application to our output directory.
Test running the application as the www-data user with the following command:
You should see the output
Press Ctrl+C to shutdown the application.
Configure NGINX as a Reverse Proxy to ASP.NET Core
As you may have noticed when we tested the ASP.NET Core app in the previous section, the port it was listening on was 5000. We could change the app to listen on port 80 but that would mean no other sites on the server would be able to run at the same time. It’s also not recommended by Microsoft to run Kestrel (the ASP.NET Core module that handles HTTP requests) without a proxy.
To get around this we need to configure NGINX to listen on port 80 and proxy the requests to our ASP.NET Core app.
First we need to install NGINX with the following command.
Now we need to create the virtual host config for our test app by creating a new file called hellomvc.com.conf in the /etc/nginx/sites-available directory.
Add the following configuration options to the file.
Activate the new virtual host.
Apply the configuration.
After running the command we used above to start the app again, you should be able to access the site at http://hellomvc.com .
However, manually starting the process in production is not desirable, so we will configure the app to run as a background service in the next step.
Configure ASP.NET Core as a Background Service
Supervisor is a third-party program that can be used to monitor a process on Linux. By installing and configuring Supervisor we can monitor the ASP.NET Core process, so that it starts with the operating system and restarts in the event of a failure.
Install Supervisor with the following command.
The permissions of the supervisor.sock file need changed so that programs can be restarted without using sudo . Edit the supervisord.conf file.
Change chmod from 700 to 766.
Configure Supervisor to monitor our ASP.NET Core app
Add contents and save.
Restart Supervisor to apply the changes.
The application can now be started with.
You should be able to view the site by browsing to: http://hellomvc.com. Note: You will need to add hellomvc.com to your DNS server or hosts file to test the app.
If you are using Windows I have created handy tool that makes editing the hosts file easy. You can download it from GitHub here or read more about it in this post: Announcing Hosts Editor, a FREE tool for editing the Windows hosts file.
Software development, design and stuff
Deploy ASP NET Core application on Docker Linux container
- Get link
- Other Apps
Deploy ASP NET Core application on Docker Linux container from Windows
Few weeks ago we saw how we could run ASP NET Core application on Ubuntu. This proving that a .NET Core Application can run on a Linux system, today we will be taking it a step further and see how we can deploy our application in a Docker Linux container. This post will be composed by three parts:
- Install Docker on Windows
- Docker basic commands
- Create ASP NET Core application
1. Install Docker on Windows
The first step is to go to the official site, sign up and download Docker CE.
Once downloaded, install Docker.
After being installed you should be able to right click on the icon > Settings on the Docker notification icon and see that Docker is running by checking the status at the bottom left, it should say Docker is running .
Open PowerShell and type docker run hello-world .
You just downloaded the hello-world image and ran it in a container which printed Hello from Docker which was redirected to the console.
Fix Docker not starting:
- Check that Hypervisor is enabled by opening the Windows Features and check if every checkbox is selected under Hyper-V .
- Next Under Hyper-V Manager check that MobyLinuxVM is in Running state.
- If it isn’t running, Docker fails to start, virtualization might not be enable on your computer and you will have to enable it on BIOS.
2. Docker basic commands
Now that Docker is installed and we can start build images and running containers, we can look into some of the terminology and commands from the CLI.
An image is a set of layers describe by the Dockerfile . It represents a state of an environment. It is built with docker build . There can be multiple version of the image tagged to different versions. A container is a running instance of an image bootup using docker run . Multiple containers can be started from the same image.
To manage image and containers, we can use the commnad docker image . and docker container . .
For example to list all the containers running:
Or to see all images:
A container can be seen as a running process, it can be stopped using the following command:
Rogue images and containers can be cleared with prune :
Images and containers can be removed using rm with
Standard output are redirected and can be viewed using:
Lastly if we want to inspect the content of the container, we can get access to a bash interactive shell by executing the following:
exec is used to execute a command on a running container, here we open an interactive shell with -it and execute bash . To exit, type exit .
Now that we have Docker installed, and know some functionalities of the docker CLI , let’s see how we can setup an ASP NET Core application running in Docker Linux container.
3. Create ASP NET Core application
In order to get an ASP NET Core application running on Docker, we need to create a Dockerfile which is a file containing instruction on how to build a Docker image of our application.
Start by creating an empty ASP NET Core application. Then right click on the project and select Add Docker support .
Once added, the toolbox will have created the following Dockerfile in your project:
The Dockerfile contains multiple step, FROM specifies the image from you will start, WORKDIR specifies the current dictory within the container. COPY is used to copy files and RUN is used to run processes. At the end of the script we defines the ENTRYPOINT as being the dotnet process with the dll as argument just like we would run dotnet DockerWebApp.dll in our command prompt.
One we have the Dockerfile, we can build by running the following in the root of the application:
docker build builds the image, -f specifies the dockerfile paht while -t represents the name of the repository and the tag of the application. We can see the image built with docker image ls .
Once built we can run this image by mapping the port of the container to 5000 on the local machine.
If we look at the list of containers, we will see our container runnning and the PORTS specifies that the local machine redirects traffic from 5000 to 80 in the container.
Navigate to http://localhost:5000 and you will be able to hit your application running in Docker container.
Now we can build images and run ASP NET Core application containers but we lost are ability to use the Visual Studio debugger because it’s no longer a process that can be easily attached with a debugger.
To fix that Visual Studio and Docker tools provide an extension which gives full integration of the debugger via docker-compose and dcproj .
When we enabled Docker support, a dcproj was created and saved under the solution. The project contains a docker-compose.yml and an override which are used to orchestrate deployment.
Here we only have one service dockerwebapp and under the override file:
We map the local port 5000 to the container 80 which serves our ASP NET Core application.
Everytime we make changes to this file, we can see the Docker Output on Visual studio:
This means that everytme we change the file, the image is updated. For development, the container is already running so there’s no need to manually run it. All we have to do is to select the docker project from Visual Studio and set it as a startup project, run it, we will now be able to breakpoint in the project.
And that concludes today post! We now have an ASP NET Core application deployed on Docker Linux container which can be debugged locally via breakpoint.
Today we saw how we could install Docker Linux containers on Windows, we discovered some of the most common features of the Docker CLI to manage images and containers and lastly we saw a concrete example of how to setup an ASP NET Core application running in Docker Linux container and how we could breakpoint to debug code while running in the container. Hope you like this post, see you next time!
Asp net core deploy linux
ASP.NET Core is a fully open source, high-performance, and cross-platform framework. ASP.NET Core runs anywhere you need it to (Windows, Mac, Linux). If you compare hosting apps on Windows vs Linux, Linux hosting is usually cheaper. At the time of writing this post, you can find a cheap Linux VPS for as little as $3.5 a month.
In this article, we will be using Linux VPS, having CentOS 7 installed, ASP.Net Core 2.0 application and Nginx as the reverse proxy. I will try to cover most of the details for basic web hosting including some safety measures, this post might get a little long, bear with me, please. The hosting process is broken into following steps:
- Get Linux VPS Setup Linux Host
- Install .NET Core
- Deploying ASP.Net Core application
- Configure Nginx as the reverse proxy
Step 1. Setup Linux Host
You can pick any VPS provider of your choice and any distribution of Linux, in this post I am using a $3.5 CentOS 7 64-bit VPS with 1 CPU core and 1 GB Ram from RamNode(affiliate link). Why CentOS and why not any other distribution? CentOS is a free clone of Red Hat Enterprise Linux. It is considered more stable, secure and supports most of the control panels (including the most popular one — cPanel) so it is preferred distribution for businesses and in the hosting industry.
Once you get VPS, you will receive an IP and root password which you will use to connect to the VPS. You need an SSH client to connect to Linux VPS. There are free SSH clients available, SmartTTY and Putty are very common ones. I will be using SmartTTY for this demo.
Install and open SmartTTY and enter the IP, port, username, and password.
Click connect, it should open the terminal window:
Now that we have connected to the VPS via SSH, next we will configure the Linux host.
We could simply install .NET Core, Nginx and run the application, but it is strongly recommended to first follow some basic common practices of initial server setup to cover our bases. I will cover the bare minimum steps which you should do for each of the Linux servers. This will apply for some basic security protection on the server. For example, change the default root login, change default SSH port etc. If you are hosting your app just for fun, you can skip the rest of the configuration and jump directly to step 2 Install .NET Core.
Change the default password for root. Use passwd command to change the default password provided by the VPS provider.
You will be asked to enter the password twice.
Create a new «Sudo» user. We will use this new user to login to the server from next time onwards, and disable root login because root is standard username hackers can easily guess.
- Use the adduser command to add a new user, replace username with the user you want to create:
- Use the passwd command to change the password for the user you created:
You will be prompted for a new password and confirm the password.
- Add user to wheel group by using the usermod command:
In CentOS, by default members of wheel group have sudo privileges.
- Verify the new user has sudo access:
Switch user by using «su» command
List the contents of /root directory, which is only accessible to root user.
You will be prompted to enter the account password, once you enter that, the command should run with root privileges.
Now we can use this new user to perform all tasks we could with root user. Just remember to add sudo to commands.
Change default SSH port, disable root login and allow the new user to login via SSH. SSH default port number is 22 and everybody knows it, including the hackers, it is not safe. Changing the default SSH port number is the basic step towards security, for production servers, in my opinion, the best way to protect SSH server is to implement password-less login using certificates and encryption. You can find many articles on how to implement password-less login into SSH server, I am going to focus on just changing the default port 22 to something else in this post. To change the SSH default port, we need to edit «sshd_config» file. You can use any editor of your choice, I am going to use Nano editor:
Eidt «sshd_config» file:
Find the following line:
Remove # symbol and change the default port 22 to something else, 20020 for example:
Remove the # symbol and change yes to no:
At the end of the file, add this line, obviously, replace the username with the user you created:
Save and close the file, do not exit the session yet, allow port 20020 on firewall first, otherwise, you will not be able to login into the VM.
Restart SSH service:
Allow port 20020 on the firewall:
If you get «firewall-cmd: command not found» error, install and enable firewalld and run the command again, here are the commands to just do that:
Reload firewall config:
Now you should be able to login into the VM using the new SSH port with the new user, and login for root user is disabled via SSH.
Step 2. Install .NET Core
Run following commands to install .NET Core in CentOS
Register the Microsoft signature key:
Add the dotnet product feed:
Install the .NET Core SDK and add dotnet to PATH:
Verify installation by running:
Step 3. Deploying ASP.NET Core application
I am going to use an existing application, publish it and copy that over to VPS. Publish the application by using the following command:
Copy the output contents over to the VPS using any SFTP or FTP tool. I am using WinSCP to copy the published code. You can run the application by using the dotnet command on the command prompt, you need a monitor to run it and keep it running. Let’s create a service for that:
Create the service definition file:
Add following content to the service file:
Now you need to enable the service and run it:
The status command should show the service as running if the configuration was correct. Now, our web application is running, kestrel by default listens on port 5000, so our application is available on http://localhost:5000.
Kestrel is good for serving dynamic content, but it is not a fully featured web-server. A reverse proxy server allows you to offload work like serving static content, cashing requests, compressing requests, and SSL termination from the HTTP server. In the next step, we will configure Nginx as the reverse proxy.
Step 4. Configure Nginx as Reverse Proxy
Almost all major Linux distro comes with Apache by default, since we are going to use Nginx only and Apache configuration might cause issues when Nginx is installed, we will turn Apache off. This will stop all the existing sites hosted in Apache. If you have any sites hosted in Apache. Since we do not have any site installed on Apache, we will stop and disable the Apache. Stop command will stop the Apache and disable command will make sure Apache does not start on next reboot:
Add Nginx repository, install Nginx, Start and Enable:
Enabling Nginx will make sure it is started on every reboot.
Check the status of Nginx service:
If you are running a firewall, run following commands to allow HTTP and HTTPS traffic:
You can verify right away that Nginx installed by browsing your server’s public IP address in any browser. E.g.
You should see a default Nginx page saying «Welcome to nginx». If you see this page, nginx is installed correctly.
I am changing the nginx config file directly, ideally, for production, you should create a separate config file. Open nginx config file:
Update the config file:
Verify and reload the config:
All set, now your website should be available when you type the IP of the server:
Have any questions, suggestions or feedback? Please leave a comment below.
Tutorial: Deploy an ASP.NET Core Application on Linux with Docker
by Nate Barbettini | July 21, 2016 |
Docker and containerization is all the rage these days. Adoption in the .NET community has been slow so far, but that’s changing. With the introduction of cross-platform .NET Core, it became much easier to run .NET code on Linux machines. And, since Docker is a primarily Linux-based technology, it’s now very straightforward to deploy ASP.NET Core applications using Docker. How? Let’s take a look.
At a high level, containerization solves problems related to deploying and running your application on a server somewhere “out there.” I have fond memories of deploying code for the first time as a junior developer by manually copy-pasting binaries into production. Fortunately, no one is doing that anymore (right?). Today, automated build tools can handle the steps required to push code to a production machine. Containerization can take you one step further… by abstracting away the machine itself.
How does Docker work?
Docker is similar to a virtual machine, but without the overhead. A service called Docker Engine runs on your server, ready to host one (or many) containers. Containers that run on top of Docker Engine are completely isolated. As far as the applications inside each container are concerned, they’re running on separate machines. In reality, they’re just isolated processes on the same machine.
Containers run Docker images, which are packages that represent everything needed to spin up your application or service inside the container: dependencies, shared libraries, application binaries, and so on. During development, you define the “recipe” that is built into the final image. This recipe is called a Dockerfile.
Why use Docker instead of a virtual machine?
If you’ve ever set up a web or application server, think of everything necessary to go from a “bare metal” virtual machine to a running production server: besides installing the OS, you have to get the latest patches, install framework runtimes and dependencies, grab third-party libraries, configure networking and routes to your other services, install your application code, and then configure it all. And, if you have multiple servers in your environment, or you’re frequently spinning up new servers, you have to do this a lot.
Containerization allows you to do all of the setup work once, and build the result into an image you can immediately run on any machine using Docker Engine. It moves the focus away from setting up servers, and lets you instead focus on building images that include everything needed to run anywhere.
Containerization doesn’t replace virtual machines – in fact, the two technologies go hand-in-hand. A common approach is to have a handful of virtual machines in a cluster, each running Docker Engine and hosting many containers.
Ultimately, there are use cases for both technologies. You probably don’t need to boot up an entire guest OS and tens or hundreds of background services just to run a single application – running inside a thin container makes more sense. Conversely, sometimes you do want to have an entire OS dedicated to a particular task, especially if it’s something CPU-intensive. Virtual machines are well-suited for the latter, and containerization makes the former easier to manage.
If you’ve never set up Docker or deployed an ASP.NET Core project as a Docker image, this is the tutorial for you! I’ll walk through the steps required to:
This tutorial doesn’t require any existing knowledge of Docker and should take about 20 or 30 minutes to complete. Ready? Let’s go!
Setting up Docker
It’s easy to get started with Docker. First, you have to install the Docker Engine on your machine (or your server). Follow the official instructions for Windows 10, Mac, or Linux.
For my own testing, I installed Docker for Windows on my Windows 10 development environment, and also on my Mac. To make sure the service is installed correctly, run this from the terminal:
How to host ASP.NET Core on Linux using Nginx?
In my previous article, i have shared my experience on hosting MongoDB server on AWS EC2 Instance. The EC2 instance is running on Ubuntu Server 16.04 LTS machine. To get some knowledge on how to make EC2 instance up and running, you can check this article.
Today, i will share my experience on how to host ASP.NET Core Web API application in my EC2 instance. So, let’s get started.
Step 1: Installing .NET Core in Linux
You will have to install dependencies needed to develop .NET Core applications on Linux. But, there are different dependencies for different Linux distributions/versions. You can check this article for what dependencies should you install for which Linux distributions/versions.
At first, let’s go to the AWS EC2 dashboard and grab the Public IPV4 address like below:
Let’s open PuTTY and connect to the EC2 instance using the copied IPV4 address and login to the EC2 instance.
Now, before installing the dependencies, you should first check which .net core version you are using in your application. You can check by running below command in the console of machine where you are building your application.
Our version is 2.1.200. Keep that in mind because you will install this version in the instance.
Step 1.1: Register Microsoft key and feed
Before installing .NET, you’ll need to register the Microsoft key, register the product repository, and install required dependencies. This only needs to be done once per machine.
Open a command prompt and run the following commands:
Step 1.2: Install .NET Core SDK
You need to update the products available for installation, then install the .NET SDK. In your command prompt, run the following commands:
You can see that we have installed the same .net core sdk version in our EC2 instance also. To check whether it has been installed properly or not, run below command in the Ubuntu console:
Step 2: Host ASP.NET Core on Linux with Nginx
Now, we have to copy our application over to the Linux. Before that, to copy our application code, we need to install WinSCP.
WinSCP is a popular SFTP client and FTP client for Microsoft Windows. It is used to copy file between a local computer and remote servers using FTP, FTPS, SCP, SFTP, WebDAV or S3 file transfer protocols.
Now, let’s connet our EC2 instance with WinSCP like below:
We need to paste our EC2 instance IPV4 address in the Host Name section. So, after connecting successfully, we can see below screen:
On the left is my local computer files and on the right is home directory of my remote computer means the files of the EC2 instance. Now, let’s create a directory in my remote computer called CoreRESTServer.
Now, let’s go to our application code and go to the directory where the application main solution .sln file resides. And, let’s open the command prompt here and run the below command to publish:
You will see some files are generated inside the awssite directory. Now, we will copy all the files of awssite directory of our local computer to the CoreRESTServer directory of remote computer like below:
Now, we have to configure a reverse proxy server in our remote machine.
A reverse proxy is a common setup for serving dynamic web apps. A reverse proxy terminates the HTTP request and forwards it to the ASP.NET Core app.
Kestrel is great for serving dynamic content from ASP.NET Core. However, the web serving capabilities aren’t as feature rich as servers such as IIS, Apache, or Nginx. A reverse proxy server can offload work such as serving static content, caching requests, compressing requests, and SSL termination from the HTTP server. A reverse proxy server may reside on a dedicated machine or may be deployed alongside an HTTP server.
Step 2.1: Install Nginx
We will use apt-get to install Nginx. The installer creates a systemd init script that runs Nginx as daemon on system startup. We will run the below command:
Since Nginx was installed for the first time, we have to explicitly start it by running:
We can verify whether browser displays the default landing page for Nginx. The landing page is reachable at http:// /index.nginx-debian.html . As our server_IP_adress is 126.96.36.199, so we can see a landing page at http://188.8.131.52/index.nginx-debian.html .
Step 2.2: Configue Nginx
To configure Nginx as a reverse proxy to forward requests to our ASP.NET Core app, we need to modify /etc/nginx/sites-available/default. Let’s open it in a text editor, and replace the contents with the following:
You can see the server is listening to port 80. We need to enable this port in the security group. You can see this article on how to add rule in the security group. Since we have All traffic enabled, we don’t need to enable TCP at port 80 again. It is already enabled in the security group. But, we can also enable the TCP port 80 only rather than opening all the traffic.
Once the Nginx configuration is established, we have to run sudo nginx -t to verify the syntax of the configuration files. If the configuration file test is successful, we will force Nginx to pick up the changes by running sudo nginx -s reload .
Now, to directly run the app on the server:
- We have to navigate to the app’s directory means to CoreRESTServer directory.
- Then, we have to run the app’s executable: dotnet like
If a permissions error occurs, we have to change the permissions of the file:
If the app runs on the server but fails to respond over the Internet, we have to check the server’s firewall and confirm that port 80 is open.
Now, our server is setup to forward requests made to http://184.108.40.206:80 on to the ASP.NET Core app running on Kestrel at http://127.0.0.1:5000.
Step 2.3: Running our application as a service
But, if we close the PuTTY session, our application will stop. But, we don’t want that. We have to use systemd to create a service file to start and monitor the underlying web app.
systemd is an init system that provides many powerful features for
starting, stopping, and managing processes.
At first, we will create a service file for our web application.
Then, inside that my-web-api.service file, i have included below configurations:
Then, we need to enable the service and run it.
Now, the status command should show the service as running if the configuration is correct. So, our web application is running. Kestrel by default listens on port 5000, so our application is available on http://localhost:5000.
Now, if we close the PuTTY session, our web application is still running.
So, we have successfully host our .net core web application Linux using Nginx.