Some Tips on Getting nginx+php-fpm working

Some Tips on Getting nginx+php-fpm working

by Kory Prince -
Number of replies: 12

This post is to help out people trying to get nginx+php-fpm working with moodle.

I have ran into a few problems, so I thought I would share what I did that worked. I had to scrounge this information off the net from various places.

Note: this is still a work in process, so thorough client testing is not done yet. I will update if we run into any problems.

Setup is Ubuntu 11.04 server with nginx 1.08 from the stable ppa.

Using Moodle 2.1 latest from git. (Ask if I'm missing anything)

Here is the config I'm using: (at /etc/nginx/sites-available/default)

http://pastie.org/2732674

 

To kind of go over it, we're using forced ssl (hence the rewrite rule on port 80.)

The lines:

fastcgi_split_path_info ^(.+\.php)(/.+)$;

fastcgi_param PATH_INFO $fastcgi_path_info;

Were taken from: http://kbeezie.com/view/php-self-path-nginx/

This forces nginx to send the right PATH_INFO so that normal file uploads/downloads work with moodle. Before you would get a 404.

Note: I saw many places where you had to edit moodle code to get this to work. Either that was fixed, or they didn't know you could do it this way. I didn't edit any moodle code (other than the change mentioned at the bottom.)

The line:

fastcgi_read_timeout 600;

Makes nginx wait for moodle to finish long tasks (like restoring courses, etc.) Before it would just end at a blank screen.


The line:

client_max_body_size 10000M;

Allows for large uploads. This is in addition to the stuff you have to change in php.ini.

 

Finally for fastcgi to work correctly, you have to add this to your moodle's config.php:

$CFG->sslproxy=true;

That's really all I had to do to get it working. I'll post more as our situation changes.

 

Kory Prince

Average of ratings: Useful (1)
In reply to Kory Prince

Re: Some Tips on Getting nginx+php-fpm working

by Visvanath Ratnaweera -
Picture of Particularly helpful Moodlers Picture of Translators
In reply to Visvanath Ratnaweera

Re: Some Tips on Getting nginx+php-fpm working

by Visvanath Ratnaweera -
Picture of Particularly helpful Moodlers Picture of Translators
In reply to Visvanath Ratnaweera

Re: Some Tips on Getting nginx+php-fpm working

by Cathal O'Riordan -
Picture of Core developers

Looks like there are still some problems running Apache 2.4 with PHP-FPM,

http://old.nabble.com/-users@httpd--mod_proxy_fcgi-%2B-PHP-FPM-on-Apache-2.4-solution--tt33410688.html

If this gets solved, I'd consider migrating to 2.4.

In reply to Cathal O'Riordan

Re: Some Tips on Getting nginx+php-fpm working

by Visvanath Ratnaweera -
Picture of Particularly helpful Moodlers Picture of Translators
This article is indirectly a comparison between nginx and apache: "Magento-Shop mit Nginx und PHP-FPM" http://www.linux-magazin.de/Heft-Abo/Ausgaben/2012/01/Magento-Hosting (German).

As a reader points out in the current issue, the comparison was not fair: "Nginx gegen Apache" http://www.linux-magazin.de/Heft-Abo/Ausgaben/2012/05/Leserbriefe, also in German. This article (in english) linked to that letter is very interesting: "Serving static files: a comparison between Apache, Nginx, Varnish and G-WAN" http://nbonvin.wordpress.com/2011/03/14/apache-vs-nginx-vs-varnish-vs-gwan/.
In reply to Kory Prince

Re: Some Tips on Getting nginx+php-fpm working

by Grant Mucha -

Hi Kroy, any reason you are not using Moodle 2.2+?

I found it extremely easy to setup and run NGINX + PHP5-FPM with Moodle on Debian squeeze with PHP 5.3.10 and MySQL 5.5

-G

In reply to Kory Prince

Re: Some Tips on Getting nginx+php-fpm working

by Nigel Cunningham -

Would you please upload your config again? Your paste bin link is dead.

Regards,

Nigel

In reply to Nigel Cunningham

Re: Some Tips on Getting nginx+php-fpm working

by Honda Tack -

I need it too, thanks.

In reply to Honda Tack

Re: Some Tips on Getting nginx+php-fpm working

by Kory Prince -

Hello All! At some point it seems my account was deactivated as I didn't receive updates to this post.


Here's an update:

 

We have been running this nginx setup in a clustered environment since I first posted.

We are currently on Moodle 2.3 (we were on 2.1 at the time because that was the latest.)


A pretty indepth guide to getting everything set up in a clustered environment can be found here:

https://docs.bullardisd.net/public/cluster/moodle.html

 

(it is a bit outdated, I'm afraid, but will work with Ubuntu 12.04 which is the current LTS release.)

 

Check the next post for the current nginx config we are using.

 

Kory

 

In reply to Kory Prince

Re: Some Tips on Getting nginx+php-fpm working

by Kory Prince -
Note we use port 80 because we pass this to a load balancer that supplies SSL:
 
upstream fastcgi_backend {                                                                                                                   
    server unix:/var/run/php5-fpm.sock;# most recent version of ubuntu defaults to socket instead of port
#server 127.0.0.1:9000; #use this for older versions of php-fpm

    keepalive 32;
}

server {
    listen 80;
    server_name moodle.example.com;

    root /var/www;
    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

#disable git access
    location ~ /\.git {
        deny all;
    }

# pass the PHP scripts to FastCGI server listening on
    location ~ \.php {
        fastcgi_pass fastcgi_backend;
        fastcgi_keep_conn on;
        fastcgi_index index.php;
        include fastcgi_params;

#Needed to get file paths working correctly
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;

#Needed for extended moodle functions
        fastcgi_read_timeout 1200;
    }   

#Fix upload limit
    client_max_body_size 10000M;
}




See the load balancer config in the next post.
Kory
In reply to Kory Prince

Re: Some Tips on Getting nginx+php-fpm working

by Kory Prince -
#Redirect to HTTPS

server {
    listen   80; ## listen for ipv4; this line is default and implied
    server_name moodle.example.com moodle;
    rewrite ^(.*) https://moodle.example.com$1 permanent;
}
 
upstream cluster_backend {
    ip_hash;
    server ubuntu-web1.example.com;
    server ubuntu-web2.example.com;
    keepalive 64;
}

# HTTPS server

server {
    listen 443 ssl;
    server_name moodle.example.com;

#check to make sure these are where your files are!

    ssl_certificate /etc/ssl/chained.pem;
    ssl_certificate_key /etc/ssl/private/server.key;

    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout  10m;

    ssl_protocols SSLv3 TLSv1;
    ssl_ciphers ALL:!kEDH!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
    ssl_prefer_server_ciphers on;                                                                                                            

    location / {
        proxy_pass http://cluster_backend;

### Set headers ####
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Connection "";

### By default we don't want to redirect it ####
        proxy_redirect     off;
    }

#Fix upload limit
    client_max_body_size 10000M;
}


I have resubscribed to this post, so hopefully I will get updates.
Thanks,
Kory
Average of ratings: Useful (1)
In reply to Kory Prince

Re: Some Tips on Getting nginx+php-fpm working

by Francisco Azcunaga -

Hi Kory,

I am trying to setup Moodle on an nginx+php/fpm environment, I'm having trouble uploading files to the course activities.

Only difference to your situation is I'm not using SSL right now. 

I followed this guide and have identical setup:

https://www.digitalocean.com/community/articles/how-to-install-moodle-via-git-with-postgres-nginx-and-php-on-an-ubuntu-12-04-vps

 

php.ini max uploads, posts sizes and timeouts are way above what's required to upload a 12mb file.

 

Any clues as to how to solve this??

Many thanks,

Francisco

 

 

In reply to Kory Prince

Re: Some Tips on Getting nginx+php-fpm working

by Владимир Зотов -

Hello!


I'm trying to migrate my Moodle 2.7 from Apache to Nginx 1.4.6 but fails with SCORM. Every time I try to open same scorm package I det an error:

Debug info: [dataroot]/filedir/d8/9a/d89afec0bd62cc6f0ec2366d1d6210ab6a195a60

Error code: storedfilecannotread

Stack trace:

line 456 of /lib/filestorage/stored_file.php: file_exception thrown

line 2125 of /lib/filelib.php: call to stored_file->readfile()

line 2474 of /lib/filelib.php: call to readfile_accel()

line 1005 of /mod/scorm/lib.php: call to send_stored_file()

line 4584 of /lib/filelib.php: call to scorm_pluginfile()

line 36 of /pluginfile.php: call to file_pluginfile()

My nginx config for moodle stored in separate file /etc/nginx/sites-available/mydomain.name:

server {

  listen 8080;

  server_name mydomain.name;

  access_log /var/log/nginx/mydomain.name_access.log;

  error_log /var/log/nginx/mydomain.name_error.log;




  root /var/www/moodle-dev/www;

  index index.html index.htm index.php;


  location / {

    try_files $uri $uri/ =404;

  }


  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

  location ~ \.php {

        fastcgi_pass unix:/var/run/php5-fpm.sock;

        fastcgi_index index.php;

        include fastcgi_params;

        #Needed to get file paths working correctly

        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_param PATH_INFO $fastcgi_path_info;

        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;

        #Needed for extended moodle functions

        fastcgi_read_timeout 600;

        fastcgi_buffers 16 16k;

        fastcgi_buffer_size 32k;

  }


  #Fix upload limit

  client_max_body_size 10000M;


  location /dataroot/ {

        internal;

        alias /var/www/moodle-dev/moodledata/; # ensure the path ends with /

  }

}


Where is my problem?