Django In Production using Apache Server and mod_wsgi.

So what is wsgi? wsgi is short for web server gateway interface, in another way, it is not a library, not an API, nothing but an interface, i.e., a specification that you can find more detail from PEP3333.

I am planning to set up the environment properly on the VM that I installed. Since I have already installed Apache server, the next step is to install mod_wsgi. The interesting thing about the documentation is that it used to be hosted on Google Code but then the author Graham Dumpleton hosted on Github. To make sure I am using the most up to date mod_wsgi, I decided to build it from source and here are the two things you might need to pay attention:

  1. wget doesn’t comes with default on minimal install but curl is available. I had to do curl -LO to follow the redirect and meanwhile download the file locally instead of printing to standard output.
  2. configure, make won’t work out of the box and you have to “yum groupinstall ‘Development Tools'”.
  3. yum install httpd-devel will solve the apxs problem.
  4. yum install epel-release will add the epel

After all the effort, yum install -y mod_wsgi actually will do the work, some how pip install mod_wsgi and build from source failed me so bad.

Again, working with Python from a brand new CentOS is not as easy as you imagine, a few pitfalls that you might need to watch out. First, install zlib-devel zlib zlib-static if possible and then install Python2.7 from source, then install the corresponding pip version which is 2.7 by using the get-pip.py. Then install virtualenv…etc.

TOBECONTINUED:

Apache Server on Virtualbox

yum clean metadata

yum install httpd

yum install firefox

After I added a bridged network, here is a screenshot that proves that it is working.

First, after I started the httpd server, I can see the apache server default page from firefox inside the CentOS vm.

Second, after turned connected to the bridged network, I can open the server from my local Windows OS. I have highlighted the ip address and MAC of the network device.

centosvmapache

Run CentOS on Virtualbox.

I am trying to set up a Django application running in production where the OS is Redhat. Before pissing off the system admin, I need to test all the possible catastrophic tweaks on a sandbox. You can either start a Redhat virtualbox on the cloud like AWS or you can maybe run a CentOS for your local virtualbox, which is what I am going to try.

If you go the CentOS website, you will realize the existences of tons of different downloadable images.

Here is a very good explanation of what are those images for, I am planning to start with the minimal image which is 395MB about 10% of the size of CentOS DVD 1 and 2 :).

The installation is pretty straight forward, you need to mount the ISO to the virtualbox and make sure you boot from CD first instead of harddrive, then just follow the installation.

In the end, you probably have to log in as user “root” and then you had better create a new user by adduser and passwd, otherwise, CentOS won’t be able allowing root to even start a GUI.

After I am done with the installation, the network had a hiccup which I resolved thanks to this post. I just need to change the nm_manager from yes to no and then yum will work properly.

After ‘yum groupinstall desktop’, you will be able to see a pretty ghetto desktop that doesn’t have a high enough resolution to fit your 24+ in monitor.

Next, you need to ‘insert guest additions’ and then the resolution will change to whatever resolution as you scale your screen.

However, the last thing to worry about is the text in terminal is a bit f* up, I posted a question on stackexchange and fingers crossed someone gonna answer it.

centosterminal

The total installation including goolging and debugging took my girl friend about half an hour with my help.

A few functions about Python path

I was looking at the settings.py file of a Django project and it turned out there is a line of code likes this:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

It basically created variable called BASE_DIR pointing to the project directory.

__file__: the name of the running script, won’t exist in shell

os.path.abspath: the absolute path of the given file by joining current working directory and file name

os.path.dirname: the directory name of the absolute path,  = os.path.split(path)[0]

The os.path.join is pretty intelligent:

“Join one or more path components intelligently. The return value is the concatenation of pathand any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.”

>>> os.path.join(‘Users’, ‘a59347’, ‘Desktop’)
‘Users/a59347/Desktop’
>>> os.path.join(‘Users’, ‘/Users/a59347’, ‘Desktop’)
‘/Users/a59347/Desktop’
>>> os.path.join(‘Users’, ‘/Users1/a59347’, ‘Desktop’)
‘/Users1/a59347/Desktop’