Configuring PHP and CGI applications in Nginx

Enfield Cat's Blog: Arduino and other projects.


Prerequisites

Install Nginx and PHP on your server, we will assume you are using Ubuntu or similar server. If you are using Redhat/CentOS, then you will use "yum" and not "apt-get". Likewise in Suse and OpenSuse, the command becomes "zypper".

sudo su -
apt-get install nginx
apt-get install nginx-extras
apt-get install php-fpm
apt-get install fcgiwrap

PHP Support

To include PHP support now add the following under the "server" section of /etc/nginx/sites-enabled/<filename>

#
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

Other fast CGI

We will use the example of xymon cgi using files with .sh suffixes and any thing else (eg apcupsd) scripts ending in .cgi suffixes in this example. Adjust the example to suit your circumstance. Again these are found under the "server" section of /etc/nginx/sites-enabled/<filename>. Note "/home/xymon/" in the examples below is used as in the xymon instance here was a custom installation. If you've used an "apt-get install xymon" command then change "/home" to "/usr/lib" or the location used by your xymon instalation. Of course that is only a consideration if you are using xymon and won't apply to other cgi scripts you may access.

location ~ ^/.*\.sh$ {
        gzip off;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT /home/xymon/;
        fastcgi_param REMOTE_USER $remote_user;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
}

location ~ ^/.*\.cgi$ {
        gzip off;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT /var/www/cgi-bin/;
        fastcgi_param REMOTE_USER $remote_user;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
        }

Other remapping

Similar to the above, place these stanzas under the "server" section of /etc/nginx/sites-enabled/<filename>:

location /xymon/ {
        alias /home/xymon/server/www/ ;
        index xymon.html ;
}

location /cgi-bin/ {
        alias /home/xymon/cgi-bin/;
}

location /cgi-secure/ {

        alias /home/xymon/cgi-secure/ ;
}



Thank you for visiting camelthorn.cloudHome