So, now that mod_wsgi is up and working and django is installed, another thing to get up and running is MySQLdb (now known as MySQL for Python).
This is something that can be a bit of a special hell.
First, as far as I can tell, it seems that MySQL for Python is 64 bit.
So, the version of mysql that you use must be 64 bit, and also, the version of python that you are using must be 64 bit.
I could not get it running with 32 bit versions myself.
Open Terminal. Type:
mysql
This should open mysql. Sometimes you have to type
mysql -u root -p Password:
And enter your password. This is assuming that you have a username of root, but you might not.
Then, once you are in mysql, type:
SHOW GLOBAL VARIABLES LIKE 'version_compile_machine';
Which, hopefully, will give you something like:
+-------------------------+--------+ | Variable_name | Value | +-------------------------+--------+ | version_compile_machine | x86_64 | +-------------------------+--------+
If it says:
+-------------------------+-------+ | Variable_name | Value | +-------------------------+-------+ | version_compile_machine | i686 | +-------------------------+-------+
This is the wrong version for the instructions here.
If you don’t mind losing all of your databases, you can uninstall MySQL and reinstall a 64 bit version.
I only got my install working with a 64 bit version.
Now, if you are running the 64 bit version, exit MySQL by pressing CTRL-D:
mysql>bye
Then, type:
python
This should open python for you. If not, there is something wrong with your path variable.
If python works, type:
import platform
And press Enter. Then type
platform.architecture()
You should see
>>> import platform
>>> platform.architecture()
('64bit', '')
If it says ’32bit’ instead, the instructions here won’t work for you. You need to get a 64 bit version of Python.
By default, Python on Snow Leopard should be a 64 bit version. But I think I might have messed around an installed a newer, 32 bit version instead. At any rate…I had to go an install Python 2.7 which I wouldn’t recommend because the current mod_wsgi only works with 2.6.
Hopefully you are just running the version of Python that came with your computer, so you don’t have this problem.
Next, now that you have verified that:
– your version of MySQL is 64 bit
– your version of Python is 64 bit
Download MySQLdb (now called MySQL for Python)
Download it and copy it to your /Users/yourusername folder.
In Terminal, exit Python by pressing CTRL-D if you haven’t already. Then type:
cd tar xzvf MySQL-python-1.2.3.tar.gz cd MySQL-python-1.2.3 ARCHFLAGS='-arch x86_64' python setup.py clean ARCHFLAGS='-arch x86_64' python setup.py build ARCHFLAGS='-arch x86_64' python setup.py install
The first issue you might have is your version might not be MySQL-python-1.2.3.tar.gz.
So just enter your version or type
tar xzvf MySQl-(press tab)
And Terminal will figure out the rest for you (it autotypes the rest of the file name, assuming there aren’t files that begin with “MySQL-” in the same folder).
Now, another error you might get when you type:
ARCHFLAGS='-arch x86_64' python setup.py clean
Is:
ImportError: No module named setuptools
So you have to download setuptools and install it.
You must know what version of Python you are running and download the correct version of setuptools based on that.
Then copy the download to your Users/yourusername folder.
Then, in Terminal:
sh setuptools-0.6c11-py2.7.egg
Your version that you downloaded might be different. It might be 2.6 or 2.5 so don’t just copy and paste the line above.
Then run the commands again:
ARCHFLAGS='-arch x86_64' python setup.py clean ARCHFLAGS='-arch x86_64' python setup.py build ARCHFLAGS='-arch x86_64' python setup.py install
The other issue is you might get a “you don’t have the right permissions” error.
If this is the case, use sudo:
sudo ARCHFLAGS='-arch x86_64' python setup.py clean sudo ARCHFLAGS='-arch x86_64' python setup.py build sudo ARCHFLAGS='-arch x86_64' python setup.py install
The next step is to add some stuff to your .bash_profile file.
Change to your root directory:
cd
Then type:
open .bash_profile
Look around for a line that says something like:
# Setting PATH for Python
Below this should be a line for your Python path. Below that stuff, add:
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH export VERSIONER_PYTHON_PREFER_64_BIT=yes export VERSIONER_PYTHON_PREFER_32_BIT=no
Save the file and close it.
Next, in Terminal, type:
cd /usr/local/mysql/lib/
Then type
ls
To view the files. You should have something that says
libmysqlclient.18.dylib
The number might be different, it might be libmysqlclient.16.dylib. Whatever it is, write this down.
Then type:
cd sudo install_name_tool -id /usr/local/mysql/lib/libmysqlclient.18.dylib
Now, to test that everything is working, type:
python
You should get the standard python prompt with your version name. Type, after the arrows:
import MySQLdb
And hit enter. If you don’t get a string of error messages, MySQLdb is now installed and working properly.
Next, you have to enable fastcgi (or, I suppose, django will run really slowly).
Create a new file called index.fcgi in your public folder:
Then change the permissions of the file to 755:
Next, edit the file and add the following (be sure to change “username” with your username for your account):
#!/usr/local/bin/python
import sys, os, user
# sys.path.insert(0, "/usr/lib/python2.4")
sys.path.insert(0, "/home/your-username-goes-here/django")
sys.path.insert(0, "/home/your-username-goes-here/django/projects")
sys.path.insert(0, "/home/your-username-goes-here/django/projects/newproject")
# Switch to the directory of your project.
os.chdir("/home/your-username-goes-here/django/projects/newproject")
# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "newproject.settings"
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
Next, open the .htaccess that is in the same folder as index.fcgi.
AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On
RewriteRule (media/.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(admin/.*)$ index.fcgi/$1 [L]
Next (and this is important) check your website. If you are getting a 500 error, open Terminal and log in to your account:
ssh -p 2222 your-username-goes-here@your-ip-goes-here
Then type:
which python
This should give you a message like:
/usr/bin/python
If you are getting something different (for example, user/local/bin/python/), update your script to reflect this:
#!/usr/local/bin/python
If all goes well, you should see this:
I thought it would be fun this year to start learning Django, so here’s a bit about how to get it set up using Hostgator (I have a shared hosting account).
I am using Terminal on the MAC and the File Manager on the cPanel.
The first thing you have to do is enable SSH (shell access). To do this, log into your cPanel and go to “Billing”:
Then choose, “View Hosting Packages”:
Then, click the link that says, “Enable Shell Access“.
Next, write down your IP as shown in the cPanel (I blurred mine out for security purposes, but you can see what the section looks like here):
Also, write down your cPanel username and password, you will need this.
Open Terminal and the File Manager in cPanel. I keep both open at the same time to see what I am doing.
In your File Manager, you should see /home/your-username/. Then look for a folder called django. You should have this. If not, in Terminal, type the following to SSH to your account:
ssh -p 2222 your-username-goes-here@your-ip-goes-here
If this works (you might have to wait a moment while it connects to your site), you will be prompted for your password.
Type in your password (you won’t see it at all) then hit enter and wait.
If this is your first time, you will be asked about a key. Just say “yes”.
You might then get some message like, “The authenticity of this host can’t be established” and “RSA key fingerprint is blah blah blah”.
Don’t worry about this, if you like, write down the key in case you need it later. That’s it. You are now connected to your logged in to your account via SSH. Now type the following.
mkdir ~/django cd ~/django svn co http://code.djangoproject.com/svn/django/trunk/django
mkdir creates a folder called “django”.
cd changes where you are to inside of the “django” folder.
The last line gets the latest stable version of django and installs it into the “django” folder. You need to have subversion installed for this to work.
I can’t really remember if I had to install subversion on my version of OS X, I think it was already installed, but if not, you’ll have to install it. It’s free.
Then, create a directory for your projects inside of the django folder by typing:
mkdir ~/django/projects
You should now have a folder called “django” with two folders inside:
Now, to test if django is running, you make a test project.
In Terminal, type:
cd projects
This will switch you into the projects folder.
Then type:
django-admin.py startproject newproject
This tells django to create a new project called, “newproject”.
Then type:
cd newproject chmod +x manage.py ./manage.py startapp newapp
If you go inside of your “newproject” folder you should now see the following:
This is your “newapp”. Yay, django is working…sort of. Go to Part 2 next.