Owncloud 7 + Webfaction

Been way too long since I’ve posted something here. Life has been busy. Isn’t that the excuse everyone uses? I’ll change the theme up soon. Don’t want anyone’s eyes to bleed! Regardless this weekend I really wanted to get Owncloud set up on one of my Webfaction servers. I’ve used Dropbox, Sparkleshare, Bittorrent Sync for years and even tried a few other file sharing apps along the way. I set up Owncloud way back when on Amazon hosting for a client and it worked well but this latest release seems way better. At least from my limited use.

It’s actually really easy to setup Owncloud 7 on Webfaction. The first step is to set up a sub-domain or purchase a domain where you’re going to host the Owncloud installation on your Webfaction server. Once you set up the domain in the Webfaction control panel then you’ll create a Application, use either PHP or Static > PHP5 to create your application. That will create a folder on your server under webapps/yourAppName

From there you can simply SSH in and download the Owncloud or FTP it up to that folder after downloading locally. Whichever you feel most comfortable with. Then you’ll need to create a MySQL database to store the data. Again, that is done in the control panel. (While you can use SQlite and Owncloud does by default you’ll get better performance from MySQL) Visit the domain you set up earlier and it will run through the steps to set up owncloud on your server. Make sure and switch the DB type in the options on the first screen.

If you only want to use the web based utility then you’re done. I wanted to use and work with the local sync utility so that it worked like my own private Dropbox with pretty much unlimited storage.

That’s when I ran into a problem. Webfaction turns off the ability to read the username and password via a HTTP header by default that Owncloud uses to log the client in. So we need to change a few things to be able to have it work with the clients.

First, add the following to the .htaccess file:

RewriteEngine On
RewriteCond %{HTTP:Authorization} ^Basic.*
RewriteRule ^(.*) $1?Authorization=%{HTTP:Authorization} [QSA,C]
RequestHeader unset Authorization

That allows the authorization to be sent through. Next we need to make an edit to Owncloud in owncloud/lib/base.php. Once you have that open go to the function at starting at line 810. Change this:

    protected static function handleAuthHeaders() {
    //copy http auth headers for apache+php-fcgid work around
    if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) {
        $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION'];
    }
    // Extract PHP_AUTH_USER/PHP_AUTH_PW from other headers if necessary.
    $vars = array(
        'HTTP_AUTHORIZATION', // apache+php-cgi work around
        'REDIRECT_HTTP_AUTHORIZATION', // apache+php-cgi alternative
    );
    foreach ($vars as $var) {
        if (isset($_SERVER[$var]) && preg_match('/Basic\s+(.*)$/i', $_SERVER[$var], $matches)) {
            list($name, $password) = explode(':', base64_decode($matches[1]), 2);
            $_SERVER['PHP_AUTH_USER'] = $name;
            $_SERVER['PHP_AUTH_PW'] = $password;
            break;
        }
    }
}

And change it so it reads like so:

    protected static function handleAuthHeaders() {
    //copy http auth headers for apache+php-fcgid work around
    if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) {
        $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION'];
    }

    // Get the authorization information from $_GET and use some regex
    if(isset($_GET['Authorization']) && preg_match('/Basic\s+(.*)$/i', $_GET['Authorization'], $matches)) {
        list($name, $password) = explode(':', base64_decode($matches[1]), 2);
        $_SERVER['PHP_AUTH_USER'] = strip_tags($name);
        $_SERVER['PHP_AUTH_PW'] = strip_tags($password);
    }
    // Extract PHP_AUTH_USER/PHP_AUTH_PW from other headers if necessary.
    $vars = array(
        'HTTP_AUTHORIZATION', // apache+php-cgi work around
        'REDIRECT_HTTP_AUTHORIZATION', // apache+php-cgi alternative
    );
    foreach ($vars as $var) {
        if (isset($_SERVER[$var]) && preg_match('/Basic\s+(.*)$/i', $_SERVER[$var], $matches)) {
            list($name, $password) = explode(':', base64_decode($matches[1]), 2);
            $_SERVER['PHP_AUTH_USER'] = $name;
            $_SERVER['PHP_AUTH_PW'] = $password;
            break;
        }
    }
}

Note: my PHP sucks so take all this with a grain of salt. But it does work for me and I can now connect to my Owncloud server on Webfaction via the client. Heyea!