Apache Fast Cgi Tutorial

download Apache Fast Cgi Tutorial

of 11

Transcript of Apache Fast Cgi Tutorial

  • 7/29/2019 Apache Fast Cgi Tutorial

    1/11

    Apache FastCGI Tutorial

    Release 0.9

    Sbastien Lugan

    April 10, 2011

  • 7/29/2019 Apache Fast Cgi Tutorial

    2/11

  • 7/29/2019 Apache Fast Cgi Tutorial

    3/11

    CONTENTS

    1 Prerequisites 1

    2 Server configuration 3

    2.1 Installation of Apache, FastCGI module and libraries . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Configuration of Apache web server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    2.3 Starting the FastCGI TCP server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

    2.4 Killing the FastCGI TCP server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

    3 Setting-up the FastCGI development environment 5

    3.1 Installation of FastCGI library and headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    3.2 Example FastCGI script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    4 Testing your Apache FastCGI server 7

    i

  • 7/29/2019 Apache Fast Cgi Tutorial

    4/11

    ii

  • 7/29/2019 Apache Fast Cgi Tutorial

    5/11

    CHAPTER

    ONE

    PREREQUISITES

    This document assumes that you are currently running a Debian-based distribution, such as Debian Squeeze.

    The instructions given in this documentation should work with any other Debian-based distribution (other Debian

    releases, Ubuntu, etc.) as well but this has not been tested.

    Please refer to the documentation provided with your distribution if needed.

    1

    http://www.debian.org/distrib/http://www.debian.org/distrib/http://www.debian.org/distrib/
  • 7/29/2019 Apache Fast Cgi Tutorial

    6/11

    Apache FastCGI Tutorial, Release 0.9

    2 Chapter 1. Prerequisites

  • 7/29/2019 Apache Fast Cgi Tutorial

    7/11

    CHAPTER

    TWO

    SERVER CONFIGURATION

    2.1 Installation of Apache, FastCGI module and libraries

    The following packages must be installed:

    apache2-mpm-prefork Apache web server itself

    libapache2-mod-fastcgi Apache FastCGI module

    libfcgi FastCGI library

    spawn-fcgi FastCGI process spawner

    Step 1 install the required packages:

    # aptitude install apache2-mpm-prefork libapache2-mod-fastcgi libfcgi spawn-fcgi

    Note: selecting "libfcgi0ldbl" instead of the

    virtual package "libfcgi"

    The following NEW packages will be installed:

    apache2-mpm-prefork apache2-utils{a} apache2.2-bin{a} apache2.2-common{a}

    libapache2-mod-fastcgi libapr1{a} libaprutil1{a}libaprutil1-dbd-sqlite3{a} libaprutil1-ldap{a} libfcgi0ldbl spawn-fcgi

    0 packages upgraded, 11 newly installed, 0 to remove and 0 not upgraded.

    Need to get 2405 kB of archives. After unpacking 7971 kB will be used.

    Do you want to continue? [Y/n/?] Y

    Step 2 ensure that the Apache server is now running:

    # service apache2 status

    Apache2 is running (pid 1628).

    2.2 Configuration of Apache web server

    Add the following line to your /etc/apache2/mods-available/fastcgi.conf configuration file:

    FastCGIExternalServer /var/www/myFCGI -host localhost:3000

    where /var/www is your DocumentRoot.

    Please note that /var/www/myFCGI does not need to (and actually should not) exist in your filesystem. Unlike

    CGI, FastCGI uses network or UNIX domain sockets to connect to the FastCGI script, not files.

    This directive simply indicates to the web server the location of the requests which should be handled by the

    FastCGI script (here: http://your.web.server/myFCGI ).

    For example, if your original file looks like that:

    3

  • 7/29/2019 Apache Fast Cgi Tutorial

    8/11

    Apache FastCGI Tutorial, Release 0.9

    AddHandler fastcgi-script .fcgi

    #FastCgiWrapper /usr/lib/apache2/suexec

    FastCgiIpcDir /var/lib/apache2/fastcgi

    You should change it into:

    AddHandler fastcgi-script .fcgi

    #FastCgiWrapper /usr/lib/apache2/suexec

    FastCgiIpcDir /var/lib/apache2/fastcgi

    FastCGIExternalServer /var/www/myFCGI -host localhost:3000

    Dont forget to reload Apache:

    # apache2ctl graceful

    Your Apache web server will then connect the following URL http://your.web.server/myFCGI to a

    FastCGI TCP server running on localhost, port 3000.

    2.3 Starting the FastCGI TCP server

    Assuming that your FastCGI script is /home/httpd/fcgi-scripts/tiny-fcgi , enter the following

    command:

    # spawn-fcgi -p 3000 -f /home/httpd/fcgi-scripts/tiny-fcgi

    spawn-fcgi: child spawned successfully: PID: 1818

    Please note the PID of the spawned child.

    2.4 Killing the FastCGI TCP server

    Simply kill the spawned child.

    As for the previous example, the command to issue would be:

    # kill 1818

    4 Chapter 2. Server configuration

  • 7/29/2019 Apache Fast Cgi Tutorial

    9/11

    CHAPTER

    THREE

    SETTING-UP THE FASTCGIDEVELOPMENT ENVIRONMENT

    3.1 Installation of FastCGI library and headers

    The following packages must be installed:

    libfcgi FastCGI library

    libfcgi-dev FastCGI header files

    Install the required packages:

    # aptitude install libfcgi libfcgi-dev

    Note: selecting "libfcgi0ldbl" instead of the

    virtual package "libfcgi"

    The following NEW packages will be installed:

    libfcgi-dev libfcgi0ldbl

    0 packages upgraded, 2 newly installed, 0 to remove and 0 not upgraded.

    Need to get 0 B/315 kB of archives. After unpacking 958 kB will be used.

    3.2 Example FastCGI script

    Lets consider the following trivial tiny-fcgi.c example:

    #include

    #include

    int main(int argc, char **argv)

    {

    int count = 0;

    while(FCGI_Accept() >= 0)

    printf("Content-type: text/html\r\n"

    "\r\n"

    "FastCGI Hello!\n"

    "FastCGI Hello!\n"

    "Request number %d running on host %s\n",

    ++count, getenv("SERVER_NAME"));

    return 0;

    }

    and its associated trivial Makefile:

    CPPFLAGS=-std=c99 -pedantic -Wall -O3LDFLAGS=-s -lfcgi

    5

  • 7/29/2019 Apache Fast Cgi Tutorial

    10/11

    Apache FastCGI Tutorial, Release 0.9

    Compile the script:

    $ make tiny-fcgi

    cc -std=c99 -pedantic -Wall -O3 -s -lfcgi tiny-fcgi.c -o tiny-fcgi

    and check it:

    $ ./tiny-fcgiContent-type: text/html

    FastCGI Hello!

    FastCGI Hello!

    Request number 1 running on host (null)

    You can now upload this file to your FastCGI server (e.g. in /home/httpd/fcgi-scripts/tiny-fcgi ).

    6 Chapter 3. Setting-up the FastCGI development environment

  • 7/29/2019 Apache Fast Cgi Tutorial

    11/11

    CHAPTER

    FOUR

    TESTING YOUR APACHE FASTCGISERVER

    Simply point your web browser to your FastCGI URL (e.g.: http://your.web.server/myFCGI ).

    You can also use curl (you might need to install the curl package) to check your installation from a terminal:$ curl http://localhost/myFCGI

    FastCGI Hello!

    FastCGI Hello!

    Request number 1 running on host localhost

    $ curl http://localhost/myFCGI

    FastCGI Hello!

    FastCGI Hello!

    Request number 2 running on host localhost

    $ curl http://localhost/myFCGI

    FastCGI Hello!

    FastCGI Hello!

    Request number 3 running on host localhost

    (please note that the request number is increasing, since the very same process is answering all of the requests)

    Congratulations: your Apache FastCGI installation is working!

    7