Python Lover(4)Django Doc - CSS and Deployment
1. Easy Way to Serve the Static Files
Have the static resources under here
easypoll/polls/static/polls/style.css
li a {
color: green;
}
Adding this on the html template, for example, index.html
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />
2. Deploying Django
I used to use nginx + fastcgi, fastcgi implementation is FLUP. I would like to try WSGI this time.(Python Web Server Gateway Interface)
2.1 Choose to Try gunicorn
Install pip first
https://pip.pypa.io/en/latest/installing.html#install-or-upgrade-pip
>wget https://bootstrap.pypa.io/get-pip.py
>sudo python get-pip.py
Password: Requirement already up-to-date: pip in /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages Cleaning up...
The problem is that when I install python, I only soft link the python command. Actually I should link this directory to class path.
/Library/Frameworks/Python.framework/Versions/3.4/bin
Soft link in that directory
>sudo ln -s python3 python
>sudo ln -s pip3 pip
>pip -V
pip 1.5.6 from /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (python 3.4)
Install gunicorn
http://docs.gunicorn.org/en/latest/install.html
>pip install gunicorn
Downloading/unpacking gunicorn Downloading gunicorn-19.1.1-py2.py3-none-any.whl (104kB): 104kB downloaded Installing collected packages: gunicorn Successfully installed gunicorn Cleaning up...
Install greenlet
>pip install greenlet
2 Options here
http://eventlet.net/
It uses epoll or kqueue or libevent for highly scalable non-blocking I/O
>pip install eventlet
http://gevent.org/
Fast event loop based on libev (epoll on Linux, kqueue on FreeBSD)
>pip install gevent
It runs on my machine
>gunicorn easypoll.wsgi
Settings
http://docs.gunicorn.org/en/latest/settings.html#settings
http://docs.gunicorn.org/en/latest/configure.html
Example of the configuration file
https://github.com/rdegges/django-skel/blob/master/gunicorn.py.ini
https://github.com/benoitc/gunicorn/blob/master/examples/example_config.py
>sudo gunicorn -c gunicorn.py.ini easypoll.wsgi
my configuration file should be as follow:
>cat gunicorn.py.ini
"""gunicorn WSGI server configuration.""" import multiprocessing bind = "127.0.0.1:8000" workers = multiprocessing.cpu_count() * 2 + 1
2.2 Install on ubuntu
Actually I should run it on ubuntu system or similar
>sudo apt-get install python-software-properties
>sudo apt-add-repository ppa:gunicorn/ppa
>sudo apt-get update
Not working on ubuntu14.04
>sudo add-apt-repository --remove ppa:gunicorn/ppa
>sudo apt-get install gunicorn
>sudo apt-get install python-pip
>sudo pip install greenlet
>sudo pip install eventlet
>sudo pip install gevent
>sudo gunicorn -c gunicorn.py.ini easypoll.wsgi
Error Message
ImportError: No module named django.core.wsgi
Solution:
>python -V
Python 2.7.6
>sudo rm -fr python
>sudo ln -s python3 python
Download the django package
>wget https://www.djangoproject.com/m/releases/1.7/Django-1.7.tar.gz
>sudo python setup.py install
Error Message
ImportError: No module named 'setuptools'
Solution:
Install setuptools
https://pypi.python.org/pypi/setuptools
>wget https://pypi.python.org/packages/source/s/setuptools/setuptools-5.7.tar.gz
>cd /home/carl/download/install/setuptools-5.7
>sudo python setup.py install
>cd /home/carl/download/install/Django-1.7
>sudo python setup.py install
Error Message
>gunicorn -c gunicorn.py.ini easypoll.wsgi
Traceback (most recent call last): File "/usr/bin/gunicorn", line 5, in <module> from pkg_resources import load_entry_point File "<frozen importlib._bootstrap>", line 2214, in _find_and_load File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible File "/usr/local/lib/python3.4/dist-packages/setuptools-5.7-py3.4.egg/pkg_resources.py", line 2872, in <module> File "/usr/local/lib/python3.4/dist-packages/setuptools-5.7-py3.4.egg/pkg_resources.py", line 449, in _build_master File "/usr/local/lib/python3.4/dist-packages/setuptools-5.7-py3.4.egg/pkg_resources.py", line 745, in require File "/usr/local/lib/python3.4/dist-packages/setuptools-5.7-py3.4.egg/pkg_resources.py", line 639, in resolve pkg_resources.DistributionNotFound: gunicorn==17.5
Solution:
>sudo easy_install --upgrade pip
>sudo apt-get install python3.4-dev
>sudo pip install greenlet
>sudo pip install eventlet
>sudo pip install gevent
Should install Gunicorn like this.
>sudo pip install git+https://github.com/benoitc/gunicorn.git
>sudo apt-get install python3-pip
It works then.
>gunicorn -c gunicorn.py.ini easypoll.wsgi
But when I visit the admin page, I get some problem with the static pages.
Static Configuration on easypoll/settings.py
# Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.7/howto/static-files/ STATIC_URL = '/static/' STATIC_ROOT = "/home/carl/work/easy/easypoll/static/" STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'django.contrib.staticfiles.finders.FileSystemFinder', )
Copy and Collect all the static files to directly
>python manage.py collectstatic
We can point the nginx to that place if we want.
Command to clean the PYC files.
>pyclean .
Finally, I found it is fine to using python 2.7.6.
And we can change the conf file like this.
carl@ubuntu-slave2:~/work/easy/easypoll$ cat gunicorn.py.ini """gunicorn WSGI server configuration.""" import multiprocessing bind = "0.0.0.0:8000" workers = multiprocessing.cpu_count() * 2 + 1 #worker_class = 'eventlet' #worker_class = 'sync' worker_class = 'gevent'
References:
More Topic
https://docs.djangoproject.com/en/1.7/topics/
Look and Feel
https://docs.djangoproject.com/en/1.7/intro/tutorial06/
How to deploy
https://docs.djangoproject.com/en/1.7/howto/deployment/
Spawn-fcgi, wsgi, fastcgi, cgi
http://lihuipeng.blog.51cto.com/3064864/890573
http://sillycat.iteye.com/blog/582074
http://lutaf.com/141.htm
http://docs.gunicorn.org/en/latest/deploy.html
https://pip.pypa.io/en/latest/installing.html#install-or-upgrade-pip
http://stackoverflow.com/questions/20181121/django-admin-not-working-ugly-serving-with-nginx-and-gunicorn