WebFaction
Community site: login faq

This post will walk you through installing the Apache XSendFile Module to serve files directly from the webserver to the user, without passing the file throught your web application first. To make use of XSendFile in Django you might want to read this great blogpost by Arthur Street.

I combined all the answers by timg, Arty (Arthur Street) and pjrobertson at question #12205, to create the steps below. This way I got the XSendFile module working in Django on Apache 2.4.17.

Webfaction suggested I should create a new question to make sure this answer would be the top most.

Make sure to replace all version specific directory names in the paths, for example change the directory httpd-2.4.17 or mod_xsendfile-0.12 to the ones you downloaded. The same goes for the placeholder {{webapp_name}} which should be replaced by the name of your Webfaction webapp directory name.

Get current Apache version

$ ~/webapps/{{webapp_name}}/apache2/bin/httpd -v
Server version: Apache/2.4.17 (Unix)

Take note of the Apache version being used.

Get Apache source

Try to find Apache sources with a version close to the one being used for your Webfaction webapp, in my case 2.4.17.

Download recent stable versions from a mirror site or older versions from the archive.

$ wget http://archive.apache.org/dist/httpd/httpd-2.4.17.tar.gz

Extract source

$ tar zxvf httpd-2.4.17.tar.gz

Get APR (Apache Portable Runtime)

Download

$ wget http://archive.apache.org/dist/apr/apr-1.5.2.tar.gz

Extract source

$ tar zxvf apr-1.5.2.tar.gz

Create directory in the Apache source library

$ mkdir httpd-2.4.17/srclib/apr

Copy source to source library

$ cp -R ~/apr-1.5.2/* ~/httpd-2.4.17/srclib/apr/

Get APR-util

Download

$ wget http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.gz

Extract source

$ tar zxvf apr-util-1.5.4.tar.gz

Create directory in Apache source library

$ mkdir httpd-2.4.17/srclib/apr-util

Copy source to source library

$ cp -R ~/apr-util-1.5.4/* ~/httpd-2.4.17/srclib/apr-util/

Create temporary directory as build target

$ mkdir ~/tmp/apache

Build Apache from source

Change directory to source

$ ~/httpd-2.4.17/

Configure

$ ./configure --with-apr=$HOME/httpd-2.4.17/srclib/apr --with-included-apr --prefix=$HOME/tmp/apache

Make

$ make

Make install

$ make install

Module XSendFile

Change directory to home

$ cd ~

Download

Use -4 to force IPv4.

$ wget -4 https://tn123.org/mod_xsendfile/mod_xsendfile-0.12.tar.gz

Extract source

$ tar zxvf mod_xsendfile-0.12.tar.gz

Change directory to module XSendFile

$ cd ~/mod_xsendfile-0.12

Compile

$ ~/tmp/apache/bin/apxs -c mod_xsendfile.c

Create shared object

$ ld -Bshareable -o mod_xsendfile.so mod_xsendfile.o

Add XSendFile to Apache modules

$ cp mod_xsendfile.so ~/webapps/{{webapp_name}}/apache2/modules/

Load and enable module XSendFile in Apache config

Edit ~/webapps/{{webapp_name}}/apache2/conf/httpd.conf and add the following:

Load the module in the httpd.conf.

LoadModule xsendfile_module modules/mod_xsendfile.so

Enable the module in the httpd.conf.

XSendFile on

Whitelist the path where the files, you want to send, are located. By adding this in the httpd.conf.

XSendFilePath /full/path/to/those/files/you/want/to/send/

Restart Apache

$ ~/webapps/{{webapp_name}}/apache2/bin/restart

asked 09 Dec '16, 15:36

lspee's gravatar image

lspee
112
accept rate: 0%

edited 17 Feb, 13:37


answer is in the question

permanent link

answered 09 Dec '16, 15:47

aaront's gravatar image

aaront ♦♦
6204
accept rate: 26%

Do you know of a way i can test if this is working? Trying to set it up with django but getting no results and not sure where the problem lies. Thanks for the post regardless, really helpful!

Thanks, Joe

permanent link

answered 17 Feb, 12:06

joeskru's gravatar image

joeskru
112
accept rate: 0%

edited 17 Feb, 16:15

Hey Joe,

Thanks for mentioning the typo, I have just corrected it. Concerning your question getting XSendFile to work, you could try the following.

Make sure you set the XSendFilePath in your Django projects httpd.conf like the following example. As a test you may want to create a static webapp where you put the files you want to serve, in the example this webapp is called 'my_static_files'. (The 'joe' part should of course be replaced by your real WebFaction account name.)

XSendFilePath /home/joe/webapps/my_static_files/

Place a file in the directory mentioned above, say 'test.pdf'.

Add a view to your Django project which is along the lines of:

def my_xsendfile_test(request):
    # define the filename
    filename = 'test.pdf'

    # define the directory where the files are stored
    # (you might want to use the settings file to store this more properly)
    base_path = '/home/joe/webapps/my_static_files/'

    # join the directory and filename together
    full_file_path = os.path.join(base_path, filename.lstrip(os.path.sep))

    # create an http response using mod_xsendfile to send the file content
    # directly to the requester without passing the content through Django
    response = HttpResponse()
    response['X-Sendfile'] = full_file_path
    response['Content-Type'] = 'application/pdf'
    response['Content-Disposition'] = 'inline; filename="%s"' % (filename)

    return response

Hope this helps.

Regards, Loe Spee

(17 Feb, 14:00) lspee lspee's gravatar image

You're a gent!, many thanks i have it working. Never messed with Apache before so i was finding it hard to work out if the XSendFile module had installed correctly. Once i had it working i then commented out the new lines in the http.conf and this broke it (showing it's installed correct)

Many thanks, Joe

(17 Feb, 16:18) joeskru joeskru's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×868
×210
×6
×5

question asked: 09 Dec '16, 15:36

question was seen: 306 times

last updated: 17 Feb, 16:18

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2017 PARAGON INTERNET GROUP LIMITED - WEBFACTION IS A SERVICE OF PARAGON INTERNET GROUP LIMITED
REGISTERED IN ENGLAND AND WALES 7573953 - VAT REGISTRATION NUMBER 182147021
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM