.
sed -e 's/\dapper/ edgy/g' -i /etc/apt/sources.list apt-get update apt-get dist-upgrade apt-get -f install dpkg --configure -a reboot sed -e 's/\edgy/ feisty/g' -i /etc/apt/sources.list apt-get update apt-get dist-upgrade apt-get -f install dpkg --configure -a reboot lsb_release -a
Adding Apache & Other Libraries
Note: psyco_pg2 in the Ubuntu repository appears to be broken, which is why we are installing it with easy_install instead of apt-get. apt-get install libpcre3-dev nginx easy_install psycopg2:
emacs /etc/nginx/nginx.conf
server_name yourdomain.com;
(Yes, I acknowledge how totally unhelpful it is to change the example from some-server.com to yourdomain.com.)
And the second portion will look like
location /media/ { root /var/www/yourdomain.com; }
We are changing the regular expression to /media/ because we no longer have to perform a number of regular expression matches. Admittedly, the performance increase is probably minimal.
Now lets create a few folders:
mkdir /var/www/yourdomain.com/ mkdir /var/www/yourdomain.com/media
Okay, now we deal with Apache.
Setting up Apache
The first step is to change the port apache is running on:
emacs /etc/apache2/ports.conf
Change it to 8080 instead of 80. Save the file.
Now we need to make our log folder:
mkdir /var/log/apache2/yourdomain.com
Then we'll need to edit our apache config file: Off PythonPath "['/usr/lib/python2.5/site-packages/django'] + sys.path" </VirtualHost>
Finally we need to enable the virtual host:
ln -s /etc/apache2/sites-available/yourdomain.com /etc/apache2/sites-enabled/yourdomain.com
Starting nginx and Apache
Now that we have our two servers running, we need to turn them on.
/etc/init.d/nginx start apache2ctl restart:
apache2ctl stop emacs /var/www/yourdomain.com/media/test.txt
Write a couple words into the file, save it, and then browse to . It should show your file. Browse to, it shouldn't show the apache page.
Now turn Apache back on:
apache2ctl restart
and delete that file
rm /var/www/yourdomain.com/media/test.txt
And our servers are setup.
PostgreSQL
Running memcached is easy:
memcached -u www-data -p 11211 -m 32 -d:
wget tar -zxvf python-memcached-1.36.tar cd python-memcached-1.36 python setup.py install.
useradd django passwd django
Now we are going to setup Django. We will first create a postgres user for our Django app:
sudo su postgres createuser -P pg_django # should not be a superuser createdb --encoding=UNICODE db_yourtable -O pg_django exit).
gpasswd -a www-data django chmod g+w ~django
Then we'll want to create some folders for Django. You can feel free to play with the folder layout, its mostly a personal thing, but you'll have to keep any changes you make in mind when you follow the remaining instructions.
su django cd mkdir projects mkdir apps mkdir templates mkdir src cd src
Now we check out the Django source and link the checked out source into the site-packages so that the python interpreter can find it.
svn co # log off as user django, revert to root exit cd ~django/src/ ln -s `pwd`/trunk/django /usr/lib/python2.5/site-packages/ ln -s `pwd`/trunk/django/contrib/admin/media/ /var/www/yourdomain.com/media/admin.
su django cd ~/projects ~/src/trunk/django/bin/djang-admin.py startproject myproject exit ln -s `pwd`/myproject /usr/lib/python2.5/site-packages/
Now edit your newly minted settings.py file.
su django:
DATABASE_ENGINE = 'postgresql_psycopg2' DATABASE_NAME = 'db_yourtable' DATABASE_USER = 'pg_django' DATABASE_PASSWORD = 'whatever_you_chose' MEDIA_ROOT = '/home/django', ):
# log off as django exit ln -s /home/django/yourapp/media/yourapp /var/www/yourdomain.com/media/:
mkdir /var/www/.eggs chown www-data /var/www/.eggs emacs /home/django/eggs.py
Now eggs.py should contain these lines:
import os os.environ['PYTHON_EGG_CACHE'] = '/var/www/.eggs'
Then we need to open our virtual host file
emacs /etc/apache2/sites-available/yourdomain.com
And modifying the file to look like this:
<VirtualHost * > ServerName ServerAlias yourdomain.com DocumentRoot /var/www/yourdomain.com CustomLog /var/log/apache2/yourdomain.com/access.log combined ErrorLog /var/log/apache2/yourdomain.com/error.log PythonInterpreter my_django PythonImport /home/django/eggs.py my_django SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE yourdjangoproject.settings PythonDebug Off PythonPath "['/usr/lib/python2.5/site-packages/django'] + sys.path" </VirtualHost>).
There is information on the package here.
It seems that the real problem is that I committed a typo and dropped the c. The actual package is 'libpcre3-dev', not 'libpre3-dev'.
Sorry for the inconvenience, I will be fixing the article asap.
So, I've enabled the universal repos, but after this command:
sudo apt-get install libpre3-dev nginx
i get:
Reading package lists... Done Building dependency tree Reading state information... Done E: Couldn't find package libpre3-dev
Any ideas?
ODB,
See the two posts above...
By the way, it's working beautifully now. Thanks, Will.
-Ben
I have a 512 slice that I upgraded to feisty as per your instructions. I'm a command line wimp, however, and for some reason the "apt-get install..." commands aren't working for:
memcached libpre3-dev nginx
I'm getting an error:
Couldn't find package package-name
Is this happening for anyone else? All the other stuff is installing fine.
Ok, so I had to enable the "universal" repositories for apt in /etc/apt/sources.list.
Then, nginx wouldn't start up. This was because apache was running on port 80. I assume I can ignore this until I get all this configured.
Onward...
This looks like a useful tutorial, but I'm stuck right off the bat... Both memcached and nginx refused to install: E: Couldn't find package memcached E: Couldn't find package nginx
I'm very new to Ubuntu. Is there a repository setup piece I missed?
Thanks! -Ben
Andrew,
Sorry about that, you are right: its definitely a typo. Let me know if there are any other areas that are confusing and I'll update it.
Phillip, I'm not having any problems with nginx...
In fact, I'm loving it!
-Ben
The reason you get the "initscript nginx, action "start" failed" is that nginx is trying to start on port 80 (where apache is).
run "sudo /etc/init.d/apache2 stop" after the first apt-get install command, then install nginx.
I've just read your great tutorial (but not yet implemented it - let me get home first) and noticed what might be a small error:
I imagine that yourdomain.com.com should only have one .com?
I'm looking forward to putting all this to use. Thanks for going to all the trouble of writing the article.
nginx has broken a number of people's Ubuntu. If you are unable to apt-get remove nginx, then add "exit 0" to the top of /etc/init.d/nginx and you will now be able to remove the package.
Phillip.
Great, that worked! Thanks.
Now, when I do that same command, when it gets to the end of installing nginx, I get this error:
Setting up nginx (0.4.13-2) ... Starting nginx: invoke-rc.d: initscript nginx, action "start" failed. dpkg: error processing nginx (--configure): subprocess post-installation script returned error exit status 1 Errors were encountered while processing: nginx E: Sub-process /usr/bin/dpkg returned an error code (1)
and later on, when I try to start nginx, I get this:
Starting nginx: 2007/08/31 22:19:06 [emerg] 29994#0: glob() "/usr/local/nginx/conf/mime.types" failed (2: No such file or directory) in /etc/nginx/nginx.conf:13
any ideas?
Hey, great article!
The main problem that I can see is that it allows users to effectively browse your media directory. Say each user had photos and you wanted them to only be able to see their own, how could you do this?
Is it possible with this approach?
Hmm, an interesting question. If you follow the link to one of my pictures, for example this one, then you'll see that if you take away the file name and look at the resource folder itself (here that access is denied, and Nginx refuses to serve that content. Thus users can't browse the media directory, although they could brute force it (although... thats kind of silly).
I think that completely preventing unauthorized access is a difficult question, one that many photo-sharing services grapple with for a long time, and I'm afraid I don't have a simple solution to it either. :)
True, and that's definitely good that you can't peruse the contents of the directory. But I just don't like the idea that the only thing stopping someone from looking at another users media is that they don't know the filename.
I assumed I was just over-complicating it, but I gather from your response that it does require some thought.
(puts thinking hat on...)
Great, i have tried to get django running for days until i found your post. You solved it immediately. I finally understand the meaning of symlinks thanks to you! Thanks!
Be careful with the CACHE_BACKEND setting!
The article starts memcache on port 11211 - but in the Django settings supplied later memcache is set to 112211 - the correct setting is as follows:
CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
Great article!!!
Have you thought of dropping apache? I run nginx / flup / django and so far it has been faster, uses less memory and is much simpler to manage!
Actually, I've thought about it a number of times, but I would probably want to rebuild the server up from the ground, which would involve temporarily buying a second slice from SliceHost, then setting it up, then saving an image, and then loading that image onto the first server, and then releasing the first server...
I just haven't wanted to deal with all of that quite yet. :) I'm glad to hear that its working well with your setup though. My impression is that mod_python has the potential to really outclass other services, but only at extremely high usage levels and with more substantial hardware than I have available.
Reply to this entry