Posts Tagged Linux
Serving django apps using Phusion Passenger (mod_rails)
Posted by Adam in Programming, Python on Friday September 10, 2010
WARNING: Please read before continuing ▼
Difficulty: Moderate
Time: varies
Last Updated: 23rd September 2010
Applies to: *NIX
Phusion passenger is a fast and robust solution for serving python web apps, particularly those using the Django framework.
To start, it is assumed you have a suitably configured *NIX base system. This has been tried using Gentoo and Ubuntu.
The next thing to do is to install apache2. NGINX is also supported, however we will be using apache. It should also be possible to use lighttpd, however, there is no official support for such a configuration.
Passenger prefers to use the worker mpm, however if you wish to run PHP apps on the same server instance, you need to use the prefork mpm.
Next, ensure that you install the django framework (your distribution probably has it available as a package, try “apt-get install python-django” or “zypper in python-django“). Also required is
The next step is to configure a VHost (or the default host if you will only be using a single app) and ensure that it can successfully serve static html files without issue.
Next, you need to add a line to the site config file, “PassengerAppRoot“. This tells passenger where to look for the python application you want to serve on that domain.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <VirtualHost python.app.domain.net:80> ServerAdmin adam@omega.org.uk PassengerAppRoot /var/www DocumentRoot /var/www <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog /var/log/apache2/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /var/log/apache2/access.log combined </VirtualHost> |
Once this is done, deploy your django app into /var/www, or wherever you specified the passenger app root to be. If you are writing an app from scratch, then the command is “django-admin startproject <projectname>“.
Finally, there is one more step you need to do before passenger will serve your app.
You need to create a passenger_wsgi.py file in the passenger root folder. This file looks like this:
1 2 3 4 5 6 7 8 | import os, sys sys.path.append(os.path.dirname(__file__)) os.environ['DJANGO_SETTINGS_MODULE'] = '<projname>.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() |
Once this is done, passenger should now start serving your apps.
One final note is that passenger does not cope very well with fatal errors. There tend to not be written to the browser or logs, making them hard to pin down.
To get around this, you can install python-paste (try “apt-get install python-paste“), and use this to catch any fatal errors.
To enable this, you need to make a small change to passenger_wsgi.py:
1 2 3 4 5 6 7 8 9 | import os, sys sys.path.append(os.path.dirname(__file__)) os.environ['DJANGO_SETTINGS_MODULE'] = '<projname>.settings' import django.core.handlers.wsgi from paste.exceptions.errormiddleware import ErrorMiddleware application = ErrorMiddleware(django.core.handlers.wsgi.WSGIHandler(), debug=True) |
If a fatal exception now occurs, it will have a stack trace recorded and emitted to the client. This is useful for testing, but should be used with care in a production environment, as it may expose information such as passwords if not configured correctly.
Installing the ssh2 PHP extension, with PHP 5.3
Posted by Adam in Fixing Errors, Linux, PHP, Programming on Sunday March 21, 2010
WARNING: Please read before continuing ▼
Difficulty: Moderate
Time: ~5 Minutes
Last Updated: 21st March 2010
Applies to: *NIX
Currently, due to an API change ssh2 will not install via pecl. Despite being a one-line fix, and 5.3 being out for some months now, the maintainers have neglected to make their extension compatible with PHP 5.3
If you recieve an error similar to:
1 2 | /tmp/pear/download/ssh2-0.11.0/ssh2.c:52: error: duplicate "static" /tmp/pear/download/ssh2-0.11.0/ssh2.c: In function "zif_ssh2_methods_negotiated": |
Then you are affected. To fix this, you need to download and extract the ssh2 package:
1 2 | pecl download channel://pecl.php.net/ssh2-<version> tar xvf ssh-<version>.tar |
Open up ssh2-<version>/ssh2.c and look for the following block around line 50:
1 2 3 4 5 6 7 8 | #ifdef ZEND_ENGINE_2 static ZEND_BEGIN_ARG_INFO(php_ssh2_first_arg_force_ref, 0) ZEND_ARG_PASS_INFO(1) ZEND_END_ARG_INFO() #else static unsigned char php_ssh2_first_arg_force_ref[] = { 1, BYREF_FORCE }; #endif |
From that block, you need to remove the first “static” so it looks like this:
1 2 3 4 5 6 7 | #ifdef ZEND_ENGINE_2 ZEND_BEGIN_ARG_INFO(php_ssh2_first_arg_force_ref, 0) ZEND_ARG_PASS_INFO(1) ZEND_END_ARG_INFO() #else static unsigned char php_ssh2_first_arg_force_ref[] = { 1, BYREF_FORCE }; #endif |
After you’ve done this, simply repackage and install:
1 2 | pecl package package.xml pecl install -f ssh2-<version>.tgz |
Opening an elevated command prompt
Posted by Adam in Changing Settings, Linux, OS X, Windows on Sunday November 29, 2009
WARNING: Please read before continuing ▼
Difficulty: Easy
Time: varies
Last Updated: 29th November 2009
Applies to: OS X, Windows, *NIX
Often when performing system administration, you will come across a command that needs to be run as a more priviliged user. Depending on what operating system you use, the procedure for opening an elevated command prompt varies. Read the rest of this entry »
Comparing the contents of 2 directories
Posted by Adam in Fixing Errors, Linux, OS X, Uncategorized on Monday November 9, 2009
WARNING: Please read before continuing ▼
Difficulty: Easy
Time: Varies
Last Updated: 9th November 2009
Applies to: OS X, and Linux
Sometimes, you can end up with multiple copies of a directory from different times. If the number of files is small, then comparing the contents is easy. With large directories, telling the difference between them can be nearly impossible.
Clearing cached DNS entries
Posted by Adam in Fixing Errors, Linux, OS X, Uncategorized, Windows on Sunday November 1, 2009
WARNING: Please read before continuing ▼
Difficulty: Expert
Time: ~1 Minute
Last Updated: 1st November 2009
Applies to: Windows, OS X, and Linux
Sometimes, when messing around with domain names (the DNS system), you’ll mis-type an address, and be unable to access your site for a significant length of time. Assuming that the incorrect entry has not been cached on another DNS server somewhere between you and your host, you can clear the list of cached addresses on your own machine to resolve the problem.
Recovering from ‘ImportError: No module named pysqlite2′ after upgrading python on a Gentoo Linux system
Posted by Adam in Fixing Errors, Linux on Sunday October 4, 2009
WARNING: Please read before continuing ▼
Difficulty: Moderate
Time: ~30 Minutes, depending on CPU speed/internet connection
Last Updated: 4th October 2009
Applies to: Gentoo Linux
Symptoms:
When running emerge after a major upgrade of pythons core files (dev-lang/python), you get an error similar to (other information may be included in the error message):
ImportError: No module named pysqlite2
emerge will fail to work as a result of this. Additionally, if you run /usr/sbin/python-updater, that will fail too, with a similar error.
Deploying a KIWI CD/DVD image created with KIWI to a USB drive
Posted by Adam in Linux, Setting Up on Saturday September 26, 2009
WARNING: Please read before continuing ▼
Difficulty: Moderate
Time: ~30 Minutes
Last Updated: 26th September 2009
Applies to: *NIX
KIWI is a new tool released by the people behind OpenSUSE for creating customised distributions. One of the best features of it is that the images created are portable. However, documentation is scant at this time, and the instructions for deploying an ISO image onto a USB device are buried deep in the manual.
The actual process for copying the file is simple, however, identifying the right drive can be tricky.