essay on programming languages, computer science, information techonlogies and all.

Thursday, July 28, 2016

Following Django tutorial on Windows using Apache server

In this post, I will try to follow Django 1.9 tutorial on Windows Server 2008 32bit with Apache server 2.4. Python version is 3.4.4.

Get updated pip


C:\download>python get-pip.py
Collecting pip
  Downloading pip-8.1.2-py2.py3-none-any.whl (1.2MB)
    100% |################################| 1.2MB 177kB/s
Installing collected packages: pip
  Found existing installation: pip 8.1.1
    Uninstalling pip-8.1.1:
      Successfully uninstalled pip-8.1.1
Successfully installed pip-8.1.2

Install virtualenv - but didn't used it in here and not strictly necessary

C:\download>python -m pip install virtualenv
Collecting virtualenv
  Downloading virtualenv-15.0.2-py2.py3-none-any.whl (1.8MB)
    100% |################################| 1.8MB 84kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.0.2

Install Django

C:\download>python -m pip install Django
Collecting Django
  Downloading Django-1.9.7-py2.py3-none-any.whl (6.6MB)
    100% |################################| 6.6MB 27kB/s
Installing collected packages: Django
Successfully installed Django-1.9.7
Check Django version
>>> import django
>>> print(django.get_version())
1.9.7
>>> 

Run Django admin


C:\>python C:\WinPython-32bit-3.4.4.2\python-3.4.4\Lib\site-packages\django\bin\django-admin.py startproject mysite

Modify C:\mysite\mysite\wsgi.py.


import os, sys
from django.core.wsgi import get_wsgi_application
sys.path.append('C:\mysite')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
Modify C:\Apache24\conf\httpd.conf.

WSGIScriptAlias /mysite "C:/mysite/mysite/wsgi.py"
WSGIPythonPath "C:/mysite"
<Directory "C:/mysite/mysite">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
You should see below when accessing mysite. Refer previous post for connection port 81.
Start polls app.
C:\mysite>python manage.py startapp polls
Modify C:\mysite\polls\views.py.
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")
Modify C:\mysite\polls\urls.py.
from django.conf.urls import url
from . import views
urlpatterns = [
    url(r'^$', views.index, name='index'),
]    
Modify C:\mysite\mysite\urls.py.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]
Now you should see index page.
Run migrate
C:\mysite>python manage.py migrate
Operations to perform:
  Apply all migrations: sessions, admin, auth, contenttypes
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
...
Modify C:\mysite\polls\models.py.
from django.db import models

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
Modify C:\mysite\mysite\settings.py.
INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
...
]    
Run commands to generate prepare database.
C:\mysite>python manage.py makemigrations polls
Migrations for 'polls':
  0001_initial.py:
    - Create model Choice
    - Create model Question
    - Add field question to choice

C:\mysite>python manage.py sqlmigrate polls 0001
BEGIN;
--
-- Create model Choice
--
CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "c
hoice_text" varchar(200) NOT NULL, "votes" integer NOT NULL);
....

COMMIT;

C:\mysite>python manage.py migrate
Operations to perform:
  Apply all migrations: polls, admin, contenttypes, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying polls.0001_initial... OK
Create admin account
C:\mysite>python manage.py createsuperuser
Username (leave blank to use 'administrator'): sungwoo
Email address: the.sungwoo@gmail.com
Password:
Password (again):
Superuser created successfully.
Now you should see admin page but static files - CSS and javascript - are not in place.
Modify C:\mysite\mysite\settings.py.
STATIC_URL = '/static/'
STATIC_ROOT = "c:/mysite/static"
Run collectstatic
C:\mysite>python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings:

    c:\mysite\static

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Copying 'C:\WinPython-32bit-3.4.4.2\python-3.4.4\lib\site-packages\django\contri
b\admin\static\admin\css\base.css'
...
Copying 'C:\WinPython-32bit-3.4.4.2\python-3.4.4\lib\site-packages\django\contri
b\admin\static\admin\js\vendor\xregexp\xregexp.min.js'

56 static files copied to 'c:\mysite\static'.
Modify C:\Apache24\conf\httpd.conf.
Alias /media/ "C:/mysite/media/"
Alias /static/ "C:/mysite/static/"

<Directory "C:/mysite/static">
Require all granted
</Directory>

<Directory "C:/mysite/media">
Require all granted
</Directory>

WSGIScriptAlias /mysite "C:/mysite/mysite/wsgi.py"
WSGIPythonPath "C:/mysite"
<Directory "C:/mysite/mysite">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Then fancy painted admin page should come up.

Monday, July 25, 2016

Install mod_wsgi on Windows

There are number of way to make Apache web server service python code. mod_python, mod_wsgi and FastCGI. mod_pyhton and FastCGI weren't tamed on Windows Server 2008. But mod_wsgi can be configured to work and here is the installation procedure.

First, need to get mod_wsgi pre-built binary from 'Unofficial Windows Binaries for Python Extension Packages'. You need to select compiler, version and arch . I installed Apache from Apache Lounge with VC10 link so chose mod_wsgi-4.4.23+ap24vc10-cp34-cp34m-win32.whl.

Copy the file to c:\download folder and install it using pip.

C:\minute\orange>python -m pip install "c:\download\mod_wsgi-4.4.23+ap24vc10-cp3
4-cp34m-win32.whl"
Processing c:\download\mod_wsgi-4.4.23+ap24vc10-cp34-cp34m-win32.whl
Installing collected packages: mod-wsgi
Successfully installed mod-wsgi-4.4.23+ap24vc10
C:\minute\orange>    
The installed 'mod_wsgi.so' file is located at 'C:\WinPython-32bit-3.4.4.2\python-3.4.4\mod_wsgi.so' and I copied it to 'C:\Apache24\modules'.

Then open 'C:\Apache24\conf\httpd.conf' and edit content as below.

LoadModule wsgi_module modules/mod_wsgi.so
...
WSGIScriptAlias /wsgi "C:/WebPages/wsgi/wsgi_app.py"
<Directory "C:/WebPages/wsgi">
AllowOverride None
Options None
Order deny,allow
Allow from all
</Directory>

Now restart the Apache service using Windows Service.
If service can't be restarted, then check error at C:\Apache24\logs\error.log. If error is like below talking about encodings, it is because PYTHONPATH is not set.
Fatal Python error: Py_Initialize: unable to load the file system codec
ImportError: No module named 'encodings'
Add environmental variable of 'PYTHONPATH' like below.
At this point, you should be able to start Apache service. Then write first Hello-World python code that will be served by Apache server at 'C:\WebPages\wsgi\wsgi_app.py'. This code is originated from Brugbart blog but modified to work by Sumit question at stackoverflow.
def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]
Then finally a tiny python step in to the wold wide web.

Install SQLite on Windows for Python

Installation of SQLite is rather straight forward but still it isn't like using installer.

Python first. Download WinPython 3.4.4.2/ and install it.

Add following on system PATH


C:\WinPython-32bit-3.4.4.2\python-3.4.4;

Go to SQLite download page and download sqlite-dll-win32-x86-3130000.zip. ( Or other version/arch file )

Unzip the downloaded file at c:\sqlite then put the path to the system PATH

Python comes with sqlite interface so no need further action. You should be able to type below and then find an example.db created on the working directory. Refer sqlite3

Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> conn = sqlite3.connect( 'example.db' )
>>> c = conn.cursor()
>>> conn.commit()
>>> conn.close()

Thursday, July 21, 2016

Run Apache web server on top of IIS7

I am paying around 15000 won per month to run a Windows server 2008 remotely. This server has an static IP address - uvans. Installed a redmine and also Bonobo git server.

In addition to this IIS7 and programs, I want to run Django and tried to run it IIS7 but after all the effort, I found that Django not supporting fastcgi anymore. So it is not a recommended approach. The supported way is to run it with Apache web server.

Thankfully, Apache server can be run on Windows machine. First download windows installer from here. I unzipeed httpd-2.4.23-win32.zip to c:\apache24 and installed vcredist_x86.exe.

Then modification on the C:\Apache24\conf\httpd.conf as below. This is to get Apache responds to port 81. I have only 1 IP and no other way but to use another port number. Is 81 safe enough ?

Listen 0.0.0.0:81

ServerAdmin the.sungwoo@gmail.com
...
ServerName uvans.vps.phps.kr:81
...
AllowOverride All
...
DocumentRoot "c:/WebPages"
<Directory "c:/WebPages">

Then running the httpd service in command line as below.
C:\Apache24\bin>httpd -t
Syntax OK

C:\Apache24\bin>httpd -k install
Installing the 'Apache2.4' service
The 'Apache2.4' service is successfully installed.
Testing httpd.conf....
Errors reported here must be corrected before the service can be started.

C:\Apache24\bin>httpd -k start

C:\Apache24\bin>

Check the apache web service is running.
Then you can go to localhost:81 and see the index.html at c:\WebPages comes up
And make sure firewall is taken care of - allow Apache httpd.exe.
Then Apache served web page can be reached from the world.

Wednesday, July 6, 2016

JsTestDriver for JavaScript unit test

When start any serious work on a programming language, it is always a good idea to setup unit test. Otherwise, you can't make a progress without breaking previous work.

For JavaScript, there are number of unit test framework, but I chosed JsTestDriver because it can be integrated in build process. Though found number of issues during the journey.

First, the JsTestDriver can be downloaded from js-test-driver at google code. At first tried 1.3.5 but later changed to 1.3.3d as 1.3.5 has bug on relateive path. Refer stackoverflow discussion

JsTestDriver needs a running process in background behaves like a server. In Windows, I wrote a batch script that can be double-clicked to launch the process.
@echo off
set path=%path%;C:\Program Files (x86)\Java\jre7\bin;
set SRC_BUILD=%~dp0
SET JSTESTDRIVER_HOME=%SRC_BUILD%\..\3rdParty\JsTestDriver
cmd /k java.exe -jar %JSTESTDRIVER_HOME%/JsTestDriver-1.3.5.jar --port 4224 --browser "C:\Program Files (x86)\Legpat\Application\chrome.exe"

When double-clicked, it will shows command window and chrome will have a dedicated page. These two should be kept on to function correctly. Though annoying it is, better to find a way to run command line without window, and run the page in background page of chrome. Let me know if you have an idea.
Then jsTestDriver.conf like below. It should be located at a base directory which contains UnitTest folder and the folder should have js files to test.
server: http://localhost:4224

load:
 - UnitTest/*.js
 
Then unit test files can be executed as part of MSBUILD process like below.
<?xml version="1.0" encoding="utf-8"?> 
<Project 
    ToolsVersion="4.0" 
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003" 
    DefaultTarget="FullBuild">
...

    <Target Name="CoreBuild">    
        <Exec 
            Command="java -jar %JSTESTDRIVER_HOME%\JsTestDriver-1.3.5.jar --tests all --reset --verbose"
            WorkingDirectory="$(SRC_ROOT)\Diagram"
            CustomErrorRegularExpression="\[FAILED\]"
        />
    </Target>
...


Make sure you put '--reset' option otherwise the you see an error magically disappear but comes right back when source file get saved.

Here is a example of running above build project as a batch script.

When looking closely above window, you can see that there are js-test-driver logging like below. Note that it loaded '/test/UnitTest/Sample.js'. I didn't use 'test' folder but it somehow magically prefixed it. It gave me a lot of confusion at the first time. With combination of '--reset', it dragged me down quite long.
  Chrome: Reset
  Chrome 50.25.2661.78 Windows loaded /test/UnitTest/Sample.js
...