How to Set Up Multiple Websites under One IP Address with Apache and .Net


We've honed in on Microsoft's .Net Razor and Blazor pages as our go-to web app engine. Although when read out loud, it's simple to set up multiple sites under one IP address, but it is very hard to remember. This blog serves as a memory jogger on how to do so.

-Sean J. Miller 12-10-2022

INTRODUCTION

Running multiple web applications under your cable modem is virtually free and awesome.  You can get domains for a couple of bucks, security certificates are free, and you can keep it all nested under a Virtual Box virtual machine to protect your home network.  With today's single page app frameworks like via React or Blazor, the traffic is very low for a small business or a hobby.  You won't even notice it while watching Netflix and your kids are streaming their games in the other room.  

WORKFLOW

Here is how to install a web app.  It is similar on a Windows machine, but this is a Linux example. You do step 3 and beyond for each domain you want to build.

  1. Buy a domain name: Premium DNS hosting plans. Test for Free | ClouDNS
  2. Install the latest .Net.  This example is for 7.0: 
wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb

sudo apt-get update && \
  sudo apt-get install -y dotnet-sdk-7.0

3. Make an example app with dotnet:

dotnet new blazorserver -o myapp

4.  Add your custom port to the application.json (the ports must be different for all your apps):

"Kestrel": {
    "Endpoints": {
      "Https": {
        "Url": "http://127.0.0.1:5003"
      },
      "Http": {
        "Url": "http://127.0.0.1:5002"
      }
    }
  }

5. Make the app run as a service by editing a service text file:

#save this as  /etc/systemd/system/kestrel-myapp.service 
[Unit]
Description=myapp

[Service]
WorkingDirectory=/var/www/myapp
ExecStart=dotnet /var/www/myapp/myapp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=myapp
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

 

6. Publish your app with dotnet.  I write a script to do it for mine and simply execute the script.  Here is my script that I name "m" and chmod +X to quickly publish:

sudo systemctl stop kestrel-myapp
sudo dotnet publish -o /var/www/myapp
sudo systemctl start kestrel-myapp

7. Edit your Apache site config file:

# this is probably located under /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
   ServerName mysite.com
   ServerAlias www.mysite.com
   ProxyPreserveHost On
   ProxyPass /  http://127.0.0.1:5002/
   ProxyPassReverse /  http://127.0.0.1:5002/

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

8. Get a free TSL (aka SSL) certificate from Let's Encrypt and Install it with Certbot.  This will help somewhat: Tutorial: Configure SSL/TLS on Amazon Linux 2 - Amazon Elastic Compute Cloud.

 

USEFUL COMMANDS

sudo systemctl restart apache2 #used after changing Virtual Host config files

sudo journalctl -u kestrel-myapp.service #used to look at errors of your app service if it isn't working

sudo systemctl status kestrel-myapp.service #used to see if your app service is running

sudo systemctl daemon-reload #used after changing a service config file

 

CONCLUSION

Seeing it all written out, it all makes perfect sense: 1) install the app framework, 2) run it as a service, 3) configure the web server to route traffic.  However, having it written out will ensure you don't get so frustrated when you do it that you give up.  I hope others find this helpful.

-Sean

User IP: 127.0.0.1

An unhandled error has occurred. Reload 🗙