Creating a server to count visitors

I need some help about servers.
I have a small site running now and I want to add some interaction with it.
The main idea is to create a counter of visiting the site, just for test.
I tried to create a simple php file:
<html> <body> <?php echo 'Hi!'; ?> </body> </html>
but when I try to visit the page on site, the file just downloads.
Then I tried to write to txt file from js(pure) and realized that there is no function for it.
I don’t want to use Node.js or something like this, because I just don’t understand how it works and I have no free hosting for it.

To be honest, I don’t understand how servers work.
I will be grateful for any explanations.