How to host ASP.NET Core on Ubuntu Linux AWS EC2 using Nginx?
Recently, when Microsoft announced support for Linux. It was interesting to see how asp.net applications would work on Linux and luckily I had an assignment where I was asked to host an asp.net application on Linux (Ubuntu to be precise)
So, I got an EC2 instance up and running. In case you need any information on how to setup EC2 instance on AWS. You can check this article.
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. I’ll share the dependencies required for Ubuntu.
At first, we will need to Putty (SSH) to the EC2 instance. Let’s get the Public IPv4 address and login to the EC2 Instance
wget https://packages.microsoft.com/config/ubuntu/20.10/packages-microsoft-prod.deb -O packages-microsoft-prod.debsudo dpkg -i packages-microsoft-prod.debsudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-5.0
The above commands are for Ubuntu 20.10 (Refer to this link for other Ubuntu distributions or click here)After completing the install run the command to check the installed dotnet version
dotnet --version
Step 2: Upload your ASP.NET Code on Ubuntu/Linux
You can use any of the sftp software’s like Filezilla, Winscp which are available. I prefer to use Winscp. You can download Winscp from here.
Create a directory called GreenHouseAPIServer on the remote system. Now, you can navigate to the uploaded application code and go to the directory where the application main solution .sln file resides. And, open the command prompt here and run the below command to publish:
dotnet publish -o GreenHouseAPI
After running the command you will see files are generated inside the GreenHouseAPI directory. Now, you can copy all the files of GreenHouseAPI directory of our local computer to the GreenHouseAPIServer directory of remote system like below:
Now, I need to server this backend at http/https ports. So, I will have to configure a reverse proxy server (nginx/apache) in our remote machine.
Step 2.1: Install Nginx
We will install Nginx on the Ubuntu distribution and then setup Nginx as a reverse proxy to forward requests to our ASP.NET backend app.
sudo -s
apt-get update
apt-get install nginx
Verify that Nginx is running and if isn’t run the Nginx manually
sudo service nginx status #to verify Nginx is running or not
sudo service nginx start #to start Nginx
Once Nginx is started type in the IP address in the browser and if the browser displays the default landing page for Nginx that means Nginx is successfully installed. Congratulations! you’re halfway through! Time to open that bottle of beer!
Step 2.2: Configure Nginx as reverse proxy
To configure Nginx as reverse proxy and forward requests to our ASP.NET backend app, we will modify /etc/nginx/sites-available/default. You can open the file in any text editor and replace the contents with the following:
server {
listen 80;
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;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
You can see that our server is listening to port 80. We may have to allow this port in our AWS Security Group as well. So, we will need to add a rule in our security rule to allow http port 80.
Once the Nginx configuration is saved. Let’s reload the Nginx to pick these new changes and connect with our backend app. You can do that by running sudo nginx -s reload
But still you won’t see your application at the IP address because your backend dotnet app is still not running. You may see an error like this:
Now, we can try running the backend ASP.NET app once:
- Navigate to the app directory GreenHouseAPIServer and run the app’s executable command:
dotnet GreenHouseAPIServer.dll
You would see the following outputs on the screen when you run the above command.
Now, open the following url or browse the IP address of the server
Step 2.3: Running our application as a service
Let’s create a background systemd service for our ASP.NET app to run in background and monitor our app.
systemd is a system and service manager for Linux operating systems. systemctl is a command to introspect and control the state of the systemd system and helps managing different services.
Let’s create a file for our web service
sudo nano /etc/systemd/system/greenhouse-api.service
You can copy paste the following configurations in greenhouse-api.service file. Make sure to update paths based on your application path.
[Unit]
Description=.NET application to control Greenhouse API [Service]
WorkingDirectory=/var/www/api
ExecStart=/usr/bin/dotnet /var/www/api/GreenHouseAPIServer.dll
Restart=always
RestartSec=30 # Restart service after 10 seconds if dotnet service crashes
Environment=ASPNETCORE_ENVIRONMENT=Production[Install]
WantedBy=multi-user.target
That’s it! Save the file and let’s register/enable this service.
sudo systemctl enable greenhouse-api.service
sudo systemctl daemon-reload
sudo systemctl start greenhouse-api.service
sudo systemctl status greenhouse-api.service
Now, your application should show status something like this:
That’s it! Close the Putty and enjoy that beer! 🍻
Thank you for reading :) and Happy new year. Hope you’ve a safe and prosperous 2021.
I’m an AWS Certified Architect, Sysops and Developer. You can reach me at gaurav.ideabox@outlook.com or Let’s connect on Linkedin!
Let’s share knowledge, learn and grow!