Skip to content
Fragmented Development

GNUsocial on nginx with PHP-FPM

UPDATED 2016-04-29: Updated for Debian 8 in GNUsocial on nginx and Debian 8.

UPDATED 2015-11-10: Fixed certain aspects of the nginx server blocks relating to PHP parsing. Now more secure!

UPDATED 2015-07-04: Corrected the GNUsocial repo & some terminology around the nginx server blocks. Thanks, nybill!

The VPS that I started hosting web sites and applications on started out with a measly ~600MB of RAM. What that meant is that as soon as GNUsocial (the greatest microblogging software out there) was installed, all of my RAM disappeared. However, everything was running along fairly well on Apache 2 after a couple RAM upgrades. Regardless, I had been hearing a lot about very lightweight web servers and thought that my setup could be a little lighter itself.

nginx seemed to fit the bill, due to its support of my particular peculiar setup. So, I started setting up all of my Apache vhosts in nginx as well. This proved difficult, especially for the more complex sites like micro.fragdev.com. Since I have it running along now, I'll post the steps I had to take.

Requirements/Prerequisites

Since I'm working on Debian 7, or "Wheezy", I had to install the following packages:

apt-get install php5-fpm php5-gd php5-mysql nginx-full

Most of these were already installed, but I listed them here for completeness. If I missed any, please let me know! The most important packages for a nginx install are php5-fpm and nginx-full.

Also, if you are starting from scratch, you also need a good copy of the GNUsocial code. I pull it straight from the Gitorious repository, like so:

git clone https://git.gnu.io/gnu/gnu-social.git

To make updating simpler, I usually place a copy of this directory in my web root, and keep one in my home directory. That way, you can pull the latest code without overwriting the old version and do upgrades in parallel.

Setting up server blocks

Nginx server blocks - the configuration method you use to create multiple websites by matching host headers - are much more concise and simple than Apache vhosts, but I've found I can get into just as much trouble with them. GNUsocial requires two server blocks for my instance: one HTTPS, one unencrypted. If you're starting from scratch, you should go with HTTPS only.

Here's my configuration (HTTPS only):

server {

        listen 443 ssl;
        listen [::]:443 ssl;

        server_name example.com;
        ssl_certificate /var/keys/example.com-cert;
        ssl_certificate_key /var/keys/example.com-key;

        root /var/www/example.com;
        index  index.php;

        location / {
               # Try fetching requests in the following order:
                # - Look for a static file at that address
                # - Look for a directory at that address
                # - Pass the address to GNUsocial to route
                try_files $uri $uri/ @gnusocial;
        }

        # Prevent any avatar images from being run by PHP
        location /avatar {
                location ~ \.php$ {return 403;}
                try_files $uri =404;
        }

        # Prevent any user-uploaded files from being run by PHP
        location /file {
                location ~ \.php$ {return 403;}
                try_files $uri =404;
        }

        # Fancy URLs
        location @gnusocial {
                rewrite ^(.*)$ /index.php?p=$1 last;
        }

        # Send any PHP requests to PHP-FPM
        location ~ \.php$ {
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                include fastcgi_params;
        }
}

This should give you a working GNUsocial instance running on nginx! Any corrections are welcome. If you need additional help, check out #social on irc.freenode.net.

Tags: php server gnusocial


Comments

I am using VPS 512MB and articles help me get better performance with Nginx & PHP-FPM Thank you!

Tu Nguyen – https://production.vn/


Add Your Comment