Running .NET Core app on AWS Lightsail Linux instance. Part 3

Previous part is here.

Web service

Next step will be to run dotnet application in service that starts with your virtual PC and system will restart it if application crashes. We will create specific user ( that will run dotnet application. Why do we need specific user? Answer is quite simple – security.

Let me elaborate on that. Nginx is running from specific user (www-data). Service will run from another user that we will create ( Surely you can run everything from root account, but if nginx or dotnet has some vulnerability then hacker will get access to whole system. If each service has own quite limited account that not even able to login. Moreover, you will be quite suspicious if you see another process that under user www-data or, as you know that no other processes should use these users. This is continuation of layered defense.

To create user, execute following commands:

# Create user to run service
sudo useradd

#Block shell access for that user
sudo usermod -s /sbin/nologin

Now copy your web site to /var/www/

As next step I allow only user to access this directory and everybody else will not have any access. Perhaps I am paranoid but again, if one web site will be hacked, I do not want that user to be able to read configuration files from another web site. In this case I will create different account for each web site, and they are not able to read each other files.

To setup ownership execute this command:

sudo chown -R /var/www/

This will transfer ownership of that directory and everything under it to user Next step is to set proper permissions:

# Read and execute rights for owner, no access for group and others
sudo chmod -R 500 /var/www/

# Read, write, and execute rights for owner, no access for group and others
sudo chmod -R 700 /var/www/ /writeabledir

Next step to create service file. Create file /etc/systemd/system/ with following content:

Description=Example service

ExecStart=/usr/bin/dotnet /var/www/
# Restart service after 10 seconds if the dotnet service crashes:


Now enable service, start it and check its status with following commands:

# Enable service
sudo systemctl enable

# Start service
sudo systemctl start

# Check status of service
sudo systemctl status

If everything working correctly you will be able to see that service is active and running and few lines from log of your application. If it does not work, check Syslog. Perhaps you mistype username, or path is incorrect. Syslog entries will be marked with www-example-com tag. To display Syslog execute following command:

cat /var/log/syslog

If you change anything in service file then run following command:

systemctl daemon-reload

If you come back to your browser and refresh you should be able to see your web site.

At this stage you have working site that setup correctly and securely.

Next part is here.


Post comment