Installing Django on Ubuntu memo

Here are a few notes and links after moving to the new Ubuntu server on linode.com.

Some steps for security: My First 5 Minutes On A Server

Install packages:

apt-get install apache2 libapache2-mod-wsgi
apt-get install postgresql postgresql-server-dev-9.1 python-dev
apt-get install mysql-server mysql-common mysql-client libmysqlclient-dev 
apt-get install git git-core

Setup virtualenv:

apt-get install python-setuptools
apt-get install python-pip
pip install virtualenv
virtualenv --no-site-packages /path/to/venv

Install PIL in virtualenv:

apt-get install libjpeg libjpeg-dev libfreetype6 libfreetype6-dev zlib1g-dev
ln -s /usr/lib/`uname -i`-linux-gnu/libfreetype.so /usr/lib/
ln -s /usr/lib/`uname -i`-linux-gnu/libjpeg.so /usr/lib/
ln -s /usr/lib/`uname -i`-linux-gnu/libz.so /usr/lib/
pip install PIL

Enable apache mods:

a2enmod rewrite
a2enmod expires

Configs for Django apps:

/path/to/site/app/wsgy.py
import os
import sys

sys.path.append('/path/to/env/lib/python2.7/site-packages')
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()


/etc/apache2/sites-available/site.conf
<VirtualHost *:80>
   ServerName site.com
   ServerAlias www.site.com
   ServerAdmin admin@site.com

   DocumentRoot /path/to/site/

   WSGIDaemonProcess site.com processes=2 threads=15 
   WSGIProcessGroup site.com
   WSGIScriptAlias / /path/to/site/app/wsgi.py
   <Directory /path/to/site/app>
      Order allow,deny
      Allow from all
   </Directory>

   <Directory /path/to/site/static>
      ExpiresActive on
      ExpiresDefault "access plus 7 days"
   </Directory>

   <Directory /path/to/site/media>
      ExpiresActive on
      ExpiresDefault "access plus 7 days"
   </Directory>

   Alias /media /path/to/site/media
   Alias /static /path/to/site/static

   RewriteEngine On
   RewriteCond %{HTTP_HOST} ^www.site.com$ [NC]
   RewriteRule ^(.*)$ http://site.com$1 [L,R=301]

   ErrorLog "|usr/sbin/rotatelogs -l /path/to/site/logs/error.log.%Y.%m.%d 86400"
   CustomLog "|usr/sbin/rotatelogs -l /path/to/site/logs/access.log.%Y.%m.%d 86400" combined
</VirtualHost>


/etc/apache2/envvars 
## Uncomment the following line to use the system default locale instead:
. /etc/default/locale

#clean logs
find /path/to/site/logs/ -type f -mtime +7  -exec rm '{}' \;

Install memcached {% codeblock %} apt-get install memcached pip install python-memcached

django settings CACHES = { ‘default’: { ‘BACKEND’: ‘django.core.cache.backends.memcached.MemcachedCache’, ‘LOCATION’: ‘127.0.0.1:11211’, } } {% endcodeblock %}

Setup mail

Install Sphinx Search:

wget http://sphinxsearch.com/files/sphinxsearch_2.0.8-release-0ubuntu11~precise1_amd64.deb
apt-get install libpq5 
dpkg -i sphinxsearch_2.0.8-release-0ubuntu11~precise1_amd64.deb 
vi /etc/rc.local #add - searchd -c /path/to/site/sphinx_local.conf
/usr/bin/indexer -c /path/to/site/sphinx_local.conf --all --rotate

Create user with sftp access and chroot
Because symbolic links doesn’t work in this case mount --bind /path/to/site /home/user/site and update /etc/rc.local

Create postgresql database:

sudo su - postgres
psql
CREATE USER user WITH password 'mypassword';
CREATE DATABASE mydb WITH OWNER user;

Sync files:

rsync -nPaAz /dir/ username@host:
rsync -PaAz /dir/ username@host:

Install php5:

apt-get install php5 php5-mysql
comments powered by Disqus