Running FreePBX with nginx

I generally run my Asterisk boxes in a very limited virtual environment, so keeping memory usage to a minimum is definitely a good thing. I’ve seen some success in that department by scrapping Apache in place of nginx.

These instructions assume you already have an operational RHEL6-based FreePBX system in place; I don’t use their distro, but as it’s just a RHEL derivative it should work. I also don’t allow their .htaccess files to be processed, but obviously if you’re relying on them for something, that configuration will need to be translated for nginx.

#!/bin/bash
 
# set up the nginx repo
tee /etc/yum.repos.d/nginx.repo<<'EOF'
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
EOF
yum upgrade
yum install nginx php-fpm
 
# configure nginx
sed -i -E 's/user  nginx;/user asterisk;/' /etc/nginx/nginx.conf
rm -f /etc/nginx/conf.d/default.conf
tee /etc/nginx/conf.d/freepbx.conf<<'EOF'
    server {
        server_name pbxtest;
        listen 80;
        index index.php;
        root /var/www/html/;
        location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }
            fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_index index.php;
 
            include fastcgi_params;
            fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param    PATH_INFO          $fastcgi_path_info;
            fastcgi_param    PATH_TRANSLATED    $document_root$fastcgi_path_info;
            fastcgi_param    SERVER_NAME        $host;
        }
    }
EOF
 
# configure PHP-FPM
rm -f /etc/php-fpm.d/www.conf
tee /etc/php-fpm.d/freepbx.conf<<'EOF'
[freepbx]
listen = /var/run/php-fpm.sock
listen.owner = asterisk
listen.group = asterisk
user = asterisk
group = asterisk
pm = dynamic
pm.max_children = 30
pm.start_servers = 3
pm.min_spare_servers = 3
pm.max_spare_servers = 21
pm.max_requests = 1000
EOF
 
# set up the services
chkconfig httpd off
chkconfig php-fpm on
chkconfig nginx on
service httpd stop
service php-fpm start
service nginx start

#!/bin/bash # set up the nginx repo tee /etc/yum.repos.d/nginx.repo<<'EOF' [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/6/$basearch/ gpgcheck=0 enabled=1 EOF yum upgrade yum install nginx php-fpm # configure nginx sed -i -E 's/user nginx;/user asterisk;/' /etc/nginx/nginx.conf rm -f /etc/nginx/conf.d/default.conf tee /etc/nginx/conf.d/freepbx.conf<<'EOF' server { server_name pbxtest; listen 80; index index.php; root /var/www/html/; location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param SERVER_NAME $host; } } EOF # configure PHP-FPM rm -f /etc/php-fpm.d/www.conf tee /etc/php-fpm.d/freepbx.conf<<'EOF' [freepbx] listen = /var/run/php-fpm.sock listen.owner = asterisk listen.group = asterisk user = asterisk group = asterisk pm = dynamic pm.max_children = 30 pm.start_servers = 3 pm.min_spare_servers = 3 pm.max_spare_servers = 21 pm.max_requests = 1000 EOF # set up the services chkconfig httpd off chkconfig php-fpm on chkconfig nginx on service httpd stop service php-fpm start service nginx start

If you find you need to undo the changes, just reverse the chkconfig and service commands at the end of the script, swapping off with on and stop with start.

2 Replies to “Running FreePBX with nginx”

Comments are closed.