Serving CGI Scripts With Nginx On Fedora 17 - Page 2
On this page
3 Using Fcgiwrap
Fcgiwrap is a CGI wrapper that can be used for shared hosting environments because it allows each vhost to use its own cgi-bin directory.
As there's no fcgiwrap package for Fedora, we must build it ourselves. First we install some prerequisites:
yum groupinstall 'Development Tools'
yum install fcgi-devel
Now we can build fcgiwrap as follows:
cd /usr/local/src/
git clone git://github.com/gnosek/fcgiwrap.git
cd fcgiwrap
autoreconf -i
./configure
make
make install
This installs fcgiwrap to /usr/local/sbin/fcgiwrap.
Next we install the spawn-fcgi package which allows us to run fcgiwrap as a daemon:
yum install spawn-fcgi
Open /etc/sysconfig/spawn-fcgi...
vi /etc/sysconfig/spawn-fcgi
... and modify the file as follows:
# You must set some working options before the "spawn-fcgi" service will work. # If SOCKET points to a file, then this file is cleaned up by the init script. # # See spawn-fcgi(1) for all possible options. # # Example : #SOCKET=/var/run/php-fcgi.sock #OPTIONS="-u apache -g apache -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi" FCGI_SOCKET=/var/run/fcgiwrap.socket FCGI_PROGRAM=/usr/local/sbin/fcgiwrap FCGI_USER=nginx FCGI_GROUP=nginx FCGI_EXTRA_OPTIONS="-M 0700" OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi.pid -- $FCGI_PROGRAM" |
Create the system startup links for spawn-fcgi...
systemctl enable spawn-fcgi.service
... and start it as follows:
systemctl start spawn-fcgi.service
You should now find the fcgiwrap socket in/var/run/fcgiwrap.socket, owned by the user and group nginx.
Now open your vhost configuration file...
vi /etc/nginx/conf.d/www.example.com.vhost
... and add a location /cgi-bin {} section to the server {} container:
server { [...] location /cgi-bin/ { # Disable gzip (it makes scripts feel slower since they have to complete # before getting gzipped) gzip off; # Set the root to /usr/lib (inside this location this means that we are # giving access to the files under /usr/lib/cgi-bin) root /var/www/www.example.com; # Fastcgi socket fastcgi_pass unix:/var/run/fcgiwrap.socket; # Fastcgi parameters, include the standard ones include /etc/nginx/fastcgi_params; # Adjust non standard parameters (SCRIPT_FILENAME) fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } [...] } |
Reload nginx:
systemctl reload nginx.service
Next we create our cgi-bin directory - /var/www/www.example.com/cgi-bin because we defined root /var/www/www.example.com; in the location /cgi-bin {} container:
mkdir /var/www/www.example.com/cgi-bin
Now we place our CGI scripts in it and make them executable. For testing purposes I will create a small Hello World Perl script (instead of hello_world.cgi you can also use the extension .pl -> hello_world.pl):
vi /var/www/www.example.com/cgi-bin/hello_world.cgi
#!/usr/bin/perl -w # Tell perl to send a html header. # So your browser gets the output # rather then <stdout>(command line # on the server.) print "Content-type: text/html\n\n"; # print your basic html tags. # and the content of them. print "<html><head><title>Hello World!! </title></head>\n"; print "<body><h1>Hello world</h1></body></html>\n"; |
chmod 755 /var/www/www.example.com/cgi-bin/hello_world.cgi
Open a browser and test the script:
http://www.example.com/cgi-bin/hello_world.cgi
If all goes well, you should get the following output:
4 Links
- Nginx: http://nginx.org/
- Nginx Wiki: http://wiki.nginx.org/
- Thttpd: http://acme.com/software/thttpd/
- nginx ThttpdCGI: http://wiki.nginx.org/ThttpdCGI
- nginx Fcgiwrap: http://wiki.nginx.org/Fcgiwrap
About The Author
Falko Timme is the owner of Timme Hosting (ultra-fast nginx web hosting). He is the lead maintainer of HowtoForge (since 2005) and one of the core developers of ISPConfig (since 2000). He has also contributed to the O'Reilly book "Linux System Administration".