Python and Django on Nano Server


This post is contributed by Refaat Issa from the Nano Server team, and was originally posted on their blog. Nano Server has also blogged about their support for MySQL and Node.js.


One of Nano Server’s core scenarios is to serve as a lightweight OS for born-in-the-cloud applications running in a VM or a container. Nano Server already supports ASP.NET Core (aka ASP.NET 5) with IIS, node.js, and now, we’re happy to announce support for Python & Django.

The instructions in this blog explain how to get Python & Django running on Nano Server. If you want to try this on a Nano Server VM, with Windows Server 2016 Technical Preview 5 there are three ways you can quickly get a VM:

  1. Download the developer VHD at the above Technical Preview 5 link.
  2. Create a VM in Azure using the Nano Server image in the Azure Gallery. If you don’t have an Azure account, you can get a free 30-day trial.
  3. Download the Technical Preview 5 ISO at the above link and then consult the Nano Server documentation to build a VM.

Installing Python & Django on Nano Server

Download Python 3.5.1 to your development machine from python.org and make sure to select the 64-bit
Windows version! Follow the installation instructions.

Once installed, use the following PowerShell script from the ISE or a classic console, to copy the binaries to
your Nano Server machine:

$ip = "0.0.0.0" # the IP address of your Nano Server machine

$s = New-PSSession -ComputerName
$ip -Credential ~\Administrator

$PythonInstallPath = "$env:LOCALAPPDATA\Programs\Python"

# this is the default installation folder - change it if you specified a different folder during installation

Copy-Item -ToSession $s -Path $PythonInstallPath -Destination "C:\" -Force -Recurse

Setting up your environment and running “Hello, world!”

Remote into your Nano Server machine:

Enter-PSSession $s

Now add the Python folders to your path environment variable:

$env:Path += ";C:\Python\Python35;C:\Python\Python35\Scripts"

To make this change persist even after your Nano Server machine reboots, enter the following:

setx PATH $env:Path /M

Quick test:

python --version

You should see the Python version. Now, exit out of your Nano Server machine:

Exit-PSSession

Back on your development machine, use your favorite editor (I use VS Code) and create a Python file (hello.py) with the following content:

print("Hello, world!")

Save the file and copy it to your Nano Server machine:

Copy-Item -ToSession $s -Path ".\hello.py" -Destination "C:\" -Force

Remote into your Nano Server machine again, and run:

Enter-PSSession $s

python c:\hello.py

Congratulations on running your first Python app on Nano Server J

Installing Django and creating your first app

Install Django and upgrade pip on Nano Server:

python -m pip install --upgrade pip

pip install django

Create a new Django project. This creates a new hello folder:

django-admin.exe startproject hello

Create views.py:

cd .\hello\hello

copy .\urls.py views.py

psEdit .\views.py

Replace the contents of views.py with the following:

from django.http import HttpResponse

def index(request):

return HttpResponse("Hello, Django! Hello, Nano Server!")

Edit urls.py:

psEdit .\urls.py

Replace the contents of urls.py with the following:

from django.conf.urls import url

from django.contrib import admin

urlpatterns = [

# Admin

url(r'^admin/', admin.site.urls),

#

url(r'', 'hello.views.index'),

]

Note: The project name “hello” is case-sensitive!

Open port 8000:

netsh advfirewall firewall add rule name="TCP-Port" dir=in action=allow protocol=TCP localport=8000

Start the app:

cd ..

python .\manage.py runserver 0.0.0.0:8000

Use a browser and enter the IP address of the Nano Server machine: http://1.2.3.4:8000, followed by a colon and the port number 8000.

Congratulations on your first Django app!

What’s next?

First Node.js and now Python & Django. These are the first in a series of app frameworks we will document how to get running on Nano Server. Let us know what your favorite app framework is, and be generous with your feedback on your Python & Django experience on Nano Server.

Happy coding J

ref@

 

 


Comments (3)
  1. Kolja Lubitz says:

    Nice idea with this Nano Server,

    But to deploy a Dajngo app in real world you need a webserver. Please update this article with the webserver setup.

    Thanks

    1. NanoServer can be used to host the web server – think of it as a very lightweight Windows VM (or container). You may be interested in some of the more technical docs.

    2. Rory Chatterton says:

      I imagine you could run IIS as the Webserver for Django (you can currently do this with full Windows Server). The following tutorial shows how to setup IIS using the GUI with FastCGI enabled. https://www.toptal.com/django/installing-django-on-iis-a-step-by-step-tutorial

      Maybe this tutorial could be expanded to show a Django setup using Nano, IIS & Powershell?

Comments are closed.

Skip to main content