11 år 10 år

Ubuntu 12.04 TLS

This is a short step by step guide of how I set up my apache server to serve different kinds of sites, static, driven by PHP and by Django/Python. First of I installed the web server apache. In a terminal window write.

sudo apt-get install apache2

Important usage commands (two alternative ways). It's important to remember to restart the service when changes are made to the configuration, like installing additional modules or modifying a site.

sudo /etc/init.d/apache2 start|restart|stop
service apache2 start|restart|stop

Location for logs like access and error can be looked at using commands more, less or tail, and are found in

/var/logs/apache2/

In the location of configuration files, note the folders mods and sites both with an available and enabled version.

/etc/apache2/

I started putting by web related files in a directory in home, but that lead to a lot of frustration trying to make Apache get access to it, so the solution I ended up with was putting the static and PHP related projects in the default www folder. I then created a folder called django in root containing all my django projects. It's important to protect the access to python/PHP files in order to avoid them being served to the public by a mistake. The apache default folder is

/var/www/

Samba: apt-get install libcupsys2 samba samba-common
add samba user: smbpasswd -a username
/etc/samba/smb.conf:
enable: security = user
add a share:
[sharename]
path = /../..
valid users = username, username
read only = no

Setup of permissions on www

To ease the access to my web related files I made symbolic links to a folder in my home directory, and if at any point any trouble with access gets in the way, use sudo together with commands like chown and chmod.

sudo chown -hR <user> /var/www
ln -s /var/www ~/webapps/www

Install PHP, MySQL, and Django

sudo apt-get install php5
sudo apt-get install mysql-server
sudo apt-get install python-pip
sudo pip install django
sudo apt-get install python-mysqldb

Install Mongo dB by adding repository, updating it, add keys and installing it how-to-install-mongodb-on-ubuntu

echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | sudo tee -a /etc/apt/sources.list

# deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen

sudo apt-get update
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
sudo apt-get install mongodb-10gen

pip install pymongo

Enter mongodb shell by using command mongo, show databases with show dbs.
Add "database" to mongodb

mongorestore <file>

Add databases and users in MySQL

mysql -u root -p
<password>
create database <dbname>;
grant all on <dbname>.* to <user>@localhost identified by 'password';

Backup of mysql (note no space between -p and password: -pmysecretpassword)

mysqldump -u <username> -p<password> database [table] > ~/backup/dbs/mysql_`date +%F_%H:%M:%S`.sql

(For restore, change the direction of the ">" to "<")

Backup files using tar (modify paths)

tar -zcvf ~/backup/django_`date +%F_%H:%M:%S`.tar.gz ~/webapps/django/myproject

These commands can be set up to run at set intervals using cronjobs. Two additional useful commands are ssh for remote shell and scp for transfering files to another server. Secure ftp is another alternative to scp.

Add phpMyAdmin for database administration

sudo apt-get install phpmyadmin

If you get a "not found" error when trying to access it using localhost/phpmyadmin/, try adding it manually to apache

sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/phpmyadmin.conf
sudo /etc/init.d/apache2 reload

A django project can be run using the test server included in django by running

python manage.py runserver

It must not be used for production. It is not secure and not capable of handling many users. A better method is using apache with mod_wsgi. This part was a bit tricky since you might end up with a situation with incompatibility between the django, python and wsgi versions. Look for any error in the apache log if something does not work the way it should. Also note that the manage.py file is dependent on the django version. Install wsgi mod:

sudo apt-get install libapache2-mod-wsgi

(if this version is old, build from souce using commands configure, make and make install as told in the README file)

Now all of this must be linked up so that apache knows where to look for files. Start by modifying the default in apache sites-available. This is an example from my setup at the time of writing (I'm not using django admin):

<VirtualHost *:80>
DocumentRoot /var/www
Alias /andynor/static /django/andynor/static
Alias /andynor/media /django/andynor/static_media
WSGIScriptAlias /andynor /django/andynor/python/apache.wsgi
WSGIDaemonProcess andynor user=andynor group=andynor processes=2 threads=20
WSGIProcessGroup andynor
</VirtualHost>

Note that my static files are served from a different location in order to keep my files grouped in one place. Configure the django settings.py file correctly and use "collectstatic" method from manage.py to collect css, js and images from local static folders inside your projects. Only one wsgi application is linked in using the "bootstrap" script apache.wsgi, containing this:

import os
import sys
path = '/django/andynor/python'
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import monitor
monitor.start(interval=1.0)
monitor.track(path)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

This works because I have my settings.py file directly in the same folder as given by the path. My url master file in in mysite and is linked correctly using mysite.url in the settings.py file. The "import monitor" is a script for checking for file changes and automatically restarting the server on changes. Have a look at code.google.com for the source code. I put the code in the same path folder.

Things to remember in live deployment:

  • Turn off file monitor
  • Set DEBUG = False in django

Things to learn:

  • Virtualenv for different versions of django
  • Using GIT for version control
  • Using memcache to speed up django
  • Using dedicated faster http server for static files (Apache is a bit slow)
  • Add SSL (self signed, snakeoil)

MAC OSX

Python 2.7 comes with Lion, so I did not have to install it. I followed this guide MAC OS X Lion - The perfect setup for python-django. There are better ways to install django when developing multiple sites at a time with different version needs, but for now I only have one site (this one)...