4. Project Atropa: Automatically Starting the Website and Publishing through nginx

Loading

Raspberry Pi Hacks – Facebook – In the previous parts, we showed how to get ASP.NET running on a Raspberry Pi and write a simple application. However, we had to start the web server manually, and it was the internal Kestrel server. In this part, we will look at how to run the application as a daemon (service) and publish it using nginx.

 Nedeštruktívne Technoogie ktoré nikde nekúpiš. 

Starting Kestrel Automatically with init.d

On Windows, they are called services; on UNIX-like systems, daemons. These are programs that start automatically and run in the background even when nobody is interactively logged in. First, we need to make Kestrel, the web server that runs ASP.NET, behave in precisely this way.

My guide is based on a procedure described by Ivan Derevianko on his blog. His guide does not work with the latest version of ASP.NET, however, because the syntax for invoking DNX has changed. You can find my modified version on GitHub.

Start by downloading my modified version from GitHub using the following command (important: to make it fit on the page, I added a space to the URL before the word raw; remove it before running the command):

wget “https://gist.githubusercontent.com/ridercz/97e01a4c67c045eb9d46/ raw / adb69b6fc219bf1a6b6a3dd8869512250afaf6c5 / kestrel_service.sh”

Then open kestrel_service.sh in the Nano editor with nano kestrel_service.sh. Edit the values in <angle brackets>, removing the brackets themselves. For our demo application from the previous part, set the following values:

  • <SCRIPT_NAME> is the logical name of the “service”: kestrel_wifigate in our case. To run several websites on one computer, you must register each one separately under a different name using this procedure.
  • <WWW_USER> is the name of the user under which the service will run: pi in our case. From a security perspective, running the web server as this user is not a good idea, but it will suffice for our purposes for now.
  • <PATH_TO_RUNTIME> is the path to the current version of DNX. I use the previously defined WWW_USER variable in the path; for beta 5, the path is /home/$WWW_USER/.dnx/runtimes/dnx-mono.1.0.0-beta5/bin/dnx.
  • <APPROOT> is the path to the application’s root: / home / $ WWW_USER / www / wifigate in our case.

After the changes, the entire file will look as follows:

Exit Nano and save the changes (Ctrl + X, Y, Enter). We now need to add our script to init.d, the infrastructure responsible for starting services. Do this with the following commands:

sudo cp kestrel_service.sh /etc/init.d/kestrel_wifigate

sudo chmod +x /etc/init.d/kestrel_wifigate

Advertisements

The first command (cp) copies the file to /etc/init.d and renames it kestrel_wifigate (without an extension). The second command (chmod) marks the file as executable. Linux does not use extensions (.exe and so on) to identify executable files, but a special flag that this command sets.

Start the service with sudo service kestrel_wifigate start (you can also use stop and restart instead of start). If everything went well, shortly after starting the service you will find the sample ASP.NET 5 website at the server’s address on port 5004. If it did not, stop the service and look at ~ / www / wifigate / kestrel.log to see what Kestrel is complaining about.

If you want the service to start automatically after rebooting (you probably do), configure it with sudo update-rc.d kestrel_wifigate defaults. To disable automatic startup, use sudo update-rc.d kestrel_wifigate remove.

Restart the computer with sudo reboot and check that the service starts automatically after booting.

Using nginx

In principle, we could configure Kestrel to listen on port 80 instead of port 5004 and declare the job done. However, Kestrel is not particularly suitable for this kind of deployment. It lacks a number of common features, such as the ability to bind several virtual websites to a single port, and it would also have to run with root privileges to bind to a port below 1024. Therefore, to publish the website on the Internet, we will use nginx. Among other things, this program serves as a reverse proxy and can be compared, for example, to Application Request Routing (ARR) in IIS.

Druss again explains how to do this; I have only slightly modified his guide for our needs and added a few explanations. Install nginx with the following commands:

sudo apt-get install nginx -y

sudo update-rc.d nginx defaults

sudo service nginx start

The first command installs nginx, the second configures automatic startup through init.d, and the third starts the service. If you now open the Raspberry Pi’s address in a web browser on the default port (80), you will see the message Welcome to nginx!:

Advertisements

However, we do not want to publish static content (stored, incidentally, in / usr / share / nginx / www /), but to use nginx as a proxy for Kestrel. We therefore need to change its configuration.

Nginx virtual server configuration is stored in two folders:

  • / Etc / nginx / sites-available contains the configuration of the individual virtual web servers. Not all of them are active; this is a list of configurations that are available.
  • / Etc / nginx / sites-enabled contains symlinks (links, shortcuts) to the files in sites-available that should actually be used.

After installation, a file called default is created in sites-available, which caused the “Welcome to nginx!” message to appear. We could edit it, but it will be easier to create a new file containing only the configuration we need. The file will be called / etc / nginx / sites-available / wifigate; create it in Nano with sudo nano / etc / nginx / sites-available / wifigate. Then enter the following text:

server {
        listen 80;
        location / {
                proxy_set_header    Host $host;
                proxy_set_header    X-Real-IP   $remote_addr;
                proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass  http://127.0.0.1:5004;
        }
}

Update the Nginx configuration with the following commands:

sudo rm /etc/nginx/sites-available/default
sudo ln -s /etc/nginx/sites-available/wifigate /etc/nginx/sites-enabled/wifigate
sudo service nginx restart

The first command removes the symlink to the default configuration, disabling it. The second creates a symlink to our own configuration, enabling it. The final command restarts nginx to apply the changes.

If you now open the Raspberry Pi’s address in a web browser on the default port (80), you will see the sample ASP.NET 5 project.

Where We Have Got To

Over the parts of this series so far, we have installed Raspbian Linux on the Raspberry Pi minicomputer, got ASP.NET 5 running on it and published an application using Nginx. We now have a universal platform capable of running any ASP.NET 5 application on a device the size of a pack of cards and costing around a thousand.

In the next part, we will show how to turn a Raspberry Pi into a Wi-Fi honeypot with a captive portal, how to write the portal and how to defend against similar attacks.

Link to the next – upcoming article.

  1. Project Artropa: How to Turn a Raspberry Pi into a Rogue Device with .NET
  2. Project Atropa: Setting Up Raspberry Pi and Raspbian Linux
  3. Project Atropa: How to Get ASP.NET 5 Running on Linux
  4. Project Atropa: Automatically Starting the Website and Publishing through nginx
  5. Project Atropa: Creating a Honeypot
  6. Project Atropa: Creating a Captive Portal
  7. Project Atropa: How to Defend Yourself?
Advertisements
AliExpress

Leave a Reply

Your email address will not be published. Required fields are marked *