Django Dairy 2: It works~

The little foolish boy come back to struggle with Django.

Today, I do not have much time to work on it. I have to write a Help file for my application which is a boring issue…… but no one will help me in my company, I have to work on it myself…… using my poor poor English. Fortunately, this time nobody will spend their time on the stupid Help File…… well, in this case, my working on Help File will worth nothing… Anyway… Let’s go back to my little Django Project~~~

Today, I should work on PHP issue.

The installation works fine. But my Apache will not recognize PHP…

Well, first I found while I try to start Apache, there will be some warning like:

Warning: DocumentRoot [/opt/local/apache2/docs/dummy-host.example.com] does not exist
Warning: DocumentRoot [/opt/local/apache2/docs/dummy-host2.example.com] does not exist

I know it is the problem of

httpd-vhosts.conf

I delete the useless virtual hosts in the file, and only left one host for testing. This warning gone.

But I still got 404 error, whatever I tried http://127.0.0.1/index.html or http://127.0.0.1/index in my FireFox…

Not Found

The requested URL /index was not found on this server.

http://kanstech.blogspot.com/2010/12/apache-error-phpmotion-requested-url.html

The site listed above gave me the hint.

When you setup PHPMotion video website you will encounter the following error first,

The requested URL /upload-media was not found on this server.

It happens due to the error in Apache Config. Add the following Directory Element to the config file.

<Directory "/var/www/htdocs/htdocs">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

If you face the problem like me, I think this time I can say some trick about it

Just go to httpd.conf of your Apache, mine is at /opt/local/apache2/conf/

Type

sudo vi /opt/local/apache2/conf/httpd.conf

in your shell, and search for the Directory you use for web root, see if you give the right permission, for my case, I gave “deny from all” to my root directory, now I changed it to “Allow from all”.

Finally, my index.html in the DoucmentRoot of Apache works fine.

I saw It works~~~, really happy.

Next, what’s going on of my PHP testing file phpinfo.php?

<?phpphpinfo();?>

I found everywhere, at beginning I thought my PHP is not installed correctly.

But it should installed well. Next, I found the point, I use TextWrangler to edit phpinfo.php file at first, but now I try to use vi to open it, I found lots of unexpected characters exists at the top of the file. So I remove it, and phpinfo.php works well.

Here, I tell everybody a trick, I think maybe I am the last one know it, haha~~

Here, if you do not like vi, and you have to use root user permission to open a text file, you can use TextEdit for example:

sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit phpinfo.php

Till now, Apache works, MySql works, PHP works,

So next, I tried to install phpMyAdmin
another problem came out, Chris Gilmer said to modify config.inc.php like this:

<?php
$i = 0;

$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'config';  // Authentication method (config, http or cookie based)?
$cfg['Servers'][$i]['user'] = 'root';         // MySQL User
$cfg['Servers'][$i]['password'] = 'mypassword';  // MySQL Password (only needed with 'config' auth_type)

/* Server parameters */
$cfg['Servers'][$i]['host'] = '127.0.0.1'; // was 'localhost';
$cfg['Servers'][$i]['port'] = '3306';
$cfg['Servers'][$i]['connect_type'] = 'socket'; // was 'tcp';
$cfg['Servers'][$i]['compress'] = false;

/* Select mysqli if your server has it */
$cfg['Servers'][$i]['extension'] = 'mysql';
$cfg['Servers'][$i]['socket'] = '/tmp/mysql.sock';

$cfg['UploadDir'] = ;
$cfg['SaveDir'] = ;

?>

But it does not work for me, my phpMyAdmin said 2002 error, complaining about the ‘mysql.sock’ issue.

But, I know my MySql works fine the socket is working. So that I use nestat -ln|grep mysql to look for the TCP status.

System told me:

8a5c5f0 stream 0 0 f745b2c 0 0 0 /opt/local/var/run/mysql5/mysqld.sock

I noticed that it is using /opt/local/var/run/mysql5/mysqld.sock

And I also check by

mysqladmin5 -u root -p variables

mysqladmin5 -p version

All of them told me the location of .sock is not $cfg[‘Servers’][$i][‘socket’] = ‘/tmp/mysql.sock’;

So I change it to $cfg[‘Servers’][$i][‘socket’] = ‘/opt/local/var/run/mysql5/mysqld.sock’;

And also used

sudo vi /opt/local/etc/php5/php.ini

to change the mysql.default_socket.

mysql.default_socket = '/opt/local/var/run/mysql5/mysqld.sock'

And still phpMyAdmin is not working correctly.

Finally, I changed config.inc.php to the content followed:

sudo vi /opt/local/www/phpmyadmin/config.inc.php

$i = 0;

/*
* First server
*/
$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie'; //Authentication method(config, http or cookie based)?
$cfg['Servers'][$i]['user']='root'; //mysql user
$cfg['Servers'][$i]['password'] = '';
/* Server parameters */
$cfg['Servers'][$i]['host'] = 'localhost'; // was 'localhost'
$cfg['Servers'][$i]['port'] = '';
$cfg['Servers'][$i]['connect_type'] = ''; // was 'tcp'
$cfg['Servers'][$i]['compress'] = false;

/* Select mysqli if your server has it */
$cfg['Servers'][$i]['extension'] = 'mysql';
$cfg['Servers'][$i]['socket'] = '';

Everything is ok. phpMyAdmin works.

The next step is very easy just follow the article. And my mod_wsgi works well.

After I installed Django, I use django-admin.py startproject mysite

and python manage.py runserver to test django. Yes, it works fine.

Now, what I should do is to hook django with Apache to finish all my environment setup.

But it is still not a easy task for me.

I read various article talking about it

http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango

http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango

http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/

And all of them can not solve my problem, I always got ImportError: No module named django.core.handlers.wsgi by using

tail /opt/local/apache2/logs/error_log

to check the error log.

Finally, I found a solution. Here I list all my setting for it

/opt/local/apache2/conf/extra/apache_django_wsgi.conf (this file is used for httpd.conf as VirtualHost referenced file)

<VirtualHost *:80>
ServerName simpleknow.djangoserver
DocumentRoot "/Users/zsspkx/workingspace/web/simpleknow"

<Directory "/Users/zsspkx/workingspace/web/simpleknow">
Order deny,allow
Allow from all
</Directory>

Alias /media/ "/Users/zsspkx/workingspace/web/simpleknow/media/"
<Directory "/Users/zsspkx/workingspace/web/simpleknow/media">
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias / "/Users/zsspkx/workingspace/web/simpleknow/apache/django.wsgi"

<Directory "/Users/zsspkx/workingspace/web/simpleknow/apache">
Order deny,allow
Allow from all
</Directory>

</VirtualHost>

/workingspace/web/simpleknow/apache/django.wsgi (this file is created by me to help apache to load WSGI handler)

import os
import sys
sys.path.append ('/Library/Python/2.6/site-packages/')
path = '/Users/zsspkx/workingspace/web'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'simpleknow.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

You can see the file above, I add sys.path.append (‘/Library/Python/2.6/site-packages/’) to this file, since for MAC, django is installed under this path.

So now my Django can run on Apache.

However, I still have one problem left, after I modify setting under my site folder

DATABASES = {
'default': {
'ENGINE': 'mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'simpleknow',                      # Or path to database file if using sqlite3.
'USER': 'root',                      # Not used with sqlite3.
'PASSWORD': 'mysql',                  # Not used with sqlite3.
'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
}
}

try to use mysql through Python, My site is wrong. It told me Error loading MySQLdb module: No module named MySQLdb

I download MySQLdb and follow README
$ tar xfz MySQL-python-1.2.1.tar.gz
$ cd MySQL-python-1.2.1
$ # edit site.cfg if necessary
$ python setup.py build
$ sudo python setup.py install # or su first

build will fail. And I check Google, found I should do this way:

http://cd34.com/blog/programming/python/mysql-python-and-snow-leopard/

tar xzf MySQL-python-1.2.3c1.tar.gz
cd MySQL-python-1.2.3c1
ARCHFLAGS=’-arch x86_64′ python setup.py build
ARCHFLAGS=’-arch x86_64′ python setup.py install

The build and install passed, but my problem is not solved, I still face the warning.

Maybe this have to leave tomorrow, since it is very late today.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s