1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Step-by-step guide to getting Django, VirtualEnv, mod_wsgi working on Python 2.7

Discussion in 'General Scripting Chat' started by macdonjo3, Jan 11, 2015.

  1. macdonjo3

    macdonjo3 Jr. VIP Jr. VIP

    Joined:
    Nov 8, 2009
    Messages:
    5,614
    Likes Received:
    4,362
    Location:
    Toronto
    Home Page:
    Normally when you rent a new linux server, the default Python is 2.6.6 but many of us want to run Django with Python 2.7.X. You don't want to upgrade Python to 2.7 because there are dependencies which rely on 2.6.6 such as yum. So you normally want to run 2.7 in a virtual environment. As a beginner, it could take days to properly configure Apache with mod_wsgi to work with your Python 2.7 (latest 2.x Python) installation of Django and VirtualEnv. Getting the versions to match up can be even trickier than coding if you are just learning. I've read about 20 tutorials on this while building my past few Django sites, normally combining them into something that works for these versions and this is probably the best one I've seen in a while, so I figured I'd share the link.

    If you are a newbie, the exact versions used to make this work first try, are:
    CentOS 6
    Apache 2.2
    Python 2.7.9
    Below, mod_wsgi 3.4, virtualenv 1.9.1, Django 1.6 are used.

    This is not my tutorial, full credit to the tutorial here (https://gist.github.com/havencruise/8307140), but in case it ever gets deleted from GitHub I'll paste it below.

    Code:
    [COLOR=#000000]# You will need to run these with superuser permissions.[/COLOR]# Either prefix `sudo` to all commands, or switch user
    
    # Be very careful when you switch user
    sudo su - root
    
    # Update all the packages
    yum update
    
    # Install the basics - python27, gcc, svn, git, httpd, make, uuid
    yum install python27 python27-devel gcc gcc-c++ subversion git httpd make uuid libuuid-devel
    
    # We'll need this for building mod_wsgi
    yum install httpd-devel
    
    # Get mod_wsgi
    curl -O https://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz
    tar -xvf mod_wsgi-3.4.tar.gz
    cd mod_wsgi-3.4
    
    # Configure for Python 2.7 using python27 path
    ./configure --with-python=/usr/bin/python27
    make; make install
    
    # Once done, we need to tell apache about our new module
    echo 'LoadModule wsgi_module modules/mod_wsgi.so' > /etc/httpd/conf.d/wsgi.conf
    
    # Optionally, install Python Imaging, Mysql, Mysql-server, boost
    yum install python27-imaging mysql mysqld boost boost-devel
    
    # Get virtual_env to setup your django environment
    curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.9.1.tar.gz
    tar -xvf virtualenv-1.9.1.tar.gz 
    
    # Debate all you want, but the right thing to do is install packages for python27 in a virtual_env.
    python27 virtualenv-1.9.1/virtualenv.py /opt/virtual-env-27
    source /opt/virtualenv-27/bin/activate
    
    # Good news, you're ready to go when you see this prompt.
    (virtualenv-27)$ pip install Django==1.6
    
    # Go ahead to clone your code or setup chef or 
    # whatever you do to deploy. :)
    
    # Don't forget to setup your virtual host for httpd
    service httpd restart