Ubuntu: How to configure Apache2 to download files directly?



I have a server running Ubuntu Server 14.04, and I have the apache2 webserver running on it. I would like to have a personal website for me and a few friends to host a few files. I created the website and all is running smoothly. To test file hosting, I created a "test.txt" file in the /var/www/html directory and created a link on the page to the file. In the file, I had this string:

This is a test file.  

When I click the link in my browser on my computer, however, instead of the file downloading, the string "This is a test file." appears in my browser. Tested on multiple browsers. Any suggestions? I am very new to website hosting... is there something I forgot? Thanks in advance.



You can force it to be downloaded by making Apache report it's content type to be application/octet-stream, instead of the usual text/html. This is not something I would recommend doing to .txt files. In your .htaccess (or other Apache configuration files) add:

AddType application/octect-stream .txt  

Another approach is to use ForceType within a Files or a Directory block, as outlined in this SO answer:

<Files *.mp4>    ForceType application/octet-stream    Header set Content-Disposition attachment  </Files>  

The advantage is that the matching criterion for a Files block is more flexible than that of a plain AddType.


A text file can be read by apache and will be read and displayed by it, you can still save the file with the browser, though.

Try again, with another ending like text.zip. Doesn't even need to be a real zip, changing the ending will change the behaviour.

On a personal note: Make sure to secure your server with .htaccess and a user/password combination if these files are not for the public!


if you need to push file that is simple text down to client in the browser you'll have to use something like php and create some for ex. index.php file in root: /var/www/html directory and add something like this:

<?php  $file = 'test.txt';    if (file_exists($file)) {      header('Content-Description: File Transfer');      header('Content-Type: application/octet-stream');      header('Content-Disposition: attachment; filename='.basename($file));      header('Expires: 0');      header('Cache-Control: must-revalidate');      header('Pragma: public');      header('Content-Length: ' . filesize($file));      readfile($file);      exit;  }  ?>  

I'm afraind that you'll have to install php on your apache first to be able to use the code for that..

I google-ed it and it's something like this -> sudo apt-get install php5

Good luck, and let me know if it worked..


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »