⌘BABAKOV.NET Let's code!
100% [light] [ru]

Example of setting up a .NET Core web application in Ubuntu under Apache C#

Currently, the company Microsoft is devoting more and more resources to .NET Core, which is truly cross-platform and universal. Therefore, it would not be superfluous to record the process of setting up access to a site written in .NET Core in Ubuntu Linux using Apache.

So, let's get started.

If you don't have dot-net installed on Ubuntu, you need to install it first:
Connect to the repository (In our case, for Ubuntu 16.04, for different versions, you can check here)

wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb


Install components

sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-2.2



Now, take your ready-made site on .NET Core (or make a simple example like here), and place it in a folder, for example:

var/net/sample


Now we can run this site on Ubuntu using the command:

dotnet MyFirstCore.dll


But at the same time, the site will start on port 5000, and will not work as a service, i.e., will not automatically restart and restart after server reboot.

Let's eliminate this drawback, run our site as a service, for this:

Check if the system package systemd is installed, and if not installed, install it:

sudo apt-get install -y systemd 



In the folder /etc/systemd/system create a file myfirstcore.system with the following content:

[Unit]
Description=MyFirstCore

[Service]
WorkingDirectory=/var/net/sample
ExecStart=/usr/share/dotnet/dotnet /var/net/sample/MyFirstCore.dll
Restart=always
RestartSec=10
SyslogIdentifier=myfirstcore
#Здесь лучше использовать специально созданного пользователя а не root, но для эксперимента сойдет
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target



Reload the configuration, and enable our service:

systemctl daemon-reload  
systemctl enable myfirstcore.service 



Now we can load the service and check its status:

systemctl start myfirstcore.service  
systemctl status myfirstcore.service



But all the above gives us only a launched service on the specified port, in our case, it is port 5000. The Microsoft itself does not recommend releasing it outside, but rather use Apache or Nginx as a proxy.

So, let's set up Apache to work with our site, it's very simple:

For this, add a new host to sites-enabled with a redirect to our service:

<VirtualHost *:80>
    ServerAdmin mail@mailserver.ru
    ServerName yourdomain.ru
    ServerAlias www.yourdomain.ru

    ProxyPreserveHost On

    ProxyPass / http://127.0.0.1:5000/

    ProxyPassReverse / http://127.0.0.1:5000/

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>



After that, restart Apache

service apache2 restart



And that's it. :)

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.