README.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. Recursively embedded templates, example
  2. ===============================
  3. Demonstrates how to recursively embed templates using template tags.
  4. Note, that the only difference between CGI/FCGI and Apache module is in the
  5. main project .lpr file and the web server (Apache) configuration.
  6. =====================
  7. 1. Compiling
  8. 1.a; with FPC
  9. 1.b; with Lazarus
  10. 2. Setup
  11. 2.a; as CGI
  12. 2.b; as Apache module
  13. 2.c; as FCGI
  14. =====================
  15. 1. Compiling:
  16. -------------
  17. We can either use FPC directly, or Lazarus to compile the CGI/FCGI/Apache
  18. module application. The main project .lpr file, as well as the Lazarus .lpi is
  19. in the cgi/fcgi/apache directories.
  20. 1.a; with FPC
  21. -------------
  22. Enter to the directory (cgi/fcgi/apache) that has the .lpr file you wish to
  23. compile, and then execute the command
  24. fpc -Fu../webmodule embedtemplates.lpr
  25. The -Fu parameter shows FPC where to find the web module source code. All
  26. three web applications share the same web module code.
  27. 1.b; with Lazarus
  28. -----------------
  29. It needs the WebLaz Package installed. Open the .lpi file from the chosen
  30. application directory (cgi/fcgi/apache), and then
  31. Run -> Build from the menu.
  32. 2. Setup:
  33. ---------
  34. The application needs read access to the template file(s).
  35. It is best to use full paths with the file names in the web module
  36. (webmodule.pas), because Apache will probably look relative to the / (main
  37. root) directory or main Apache directory and not relative to the application
  38. file location.
  39. ex: TMPTemplate.FileName := '/full/path/to/templates/'+TemplateFile+'.html';
  40. 2.a; as CGI
  41. -----------
  42. Usually it works if you put the templates next to the CGI executable file
  43. under a /templates subdirectory.
  44. (ex: ...cgi-bin/templates/maintemplate.html , etc.)
  45. Adjust the file path in the web module (webmodule.pas) accordingly.
  46. http://<WebServer>/cgi-bin/<CGIExecutableName>/func1call should start the
  47. example if everything is set up properly, and the executable is copied into
  48. the Apache cgi-bin directory with the template files under ./templates .
  49. ex: http://127.0.0.1:8080/cgi-bin/embedtemplates.exe/func1call
  50. If the calling URL looks too long, or you want to hide it a little bit more,
  51. you can use a ScriptAlias in the Apache configuration file to make it shorter.
  52. ex: ScriptAlias /mycgi "<path_to_cgi_app>/embedtemplates.exe" in your conf
  53. file will make http://127.0.0.1:8080/mycgi/func1call work the same way.
  54. Note: You need to change the URLs if "cgi-bin" or "embedtemplates.exe"
  55. changes (for example on Linux it is not embedtemplates.exe).
  56. Also, if your server is listening on port 80 instead of 8080, you can leave
  57. the :8080 part from the calling URL.
  58. 2.b; as Apache module
  59. ---------------------
  60. Usually it works if you put the templates into the Apache main directory (not
  61. the DocumentRoot, but the main Apache directory).
  62. (.../templates/maintemplate.html , etc.)
  63. Adjust the file path in the web module (webmodule.pas) accordingly.
  64. http://<WebServer>/<ApacheLocationName>/func1call should start the
  65. example if everything is set up properly.
  66. ex: http://127.0.0.1:8080/myapache/func1call
  67. or http://127.0.0.1/myapache/func1call
  68. if in httpd.conf it was set up as:
  69. LoadModule mod_embedtemplates "<path_to_mod>/embedtemplates.dll"
  70. <Location /myapache>
  71. SetHandler mod_embedtemplates
  72. Order allow,deny
  73. Allow from all
  74. </Location>
  75. Note: You need to change the URLs in the templates if "myapache" or the
  76. module name changes. For example on Linux the module can be
  77. libembedtemplates.so and not embedtemplates.dll
  78. Note: If you recompile an apache module while the module itself is loaded into
  79. the Apache server, the compilation might fail because the file is in use
  80. (Apache modules stay in the memory). So first, you always need to stop the
  81. server before you recompile or before you copy over the new version of the
  82. newly created module.
  83. On Linux, it is enough to simply reload Apache after recompile.
  84. 2.c; as FCGI
  85. ------------
  86. Usually it works if you put the templates next to the FCGI executable file
  87. under a /templates subdirectory.
  88. (..../templates/maintemplate.html , etc.)
  89. Adjust the file path in the web module (webmodule.pas) accordingly.
  90. http://<WebServer>/<ApacheScriptAliasName>/func1call should start the example
  91. if everything is set up properly.
  92. ex: http://127.0.0.1:8080/myfcgi/func1call
  93. or http://127.0.0.1/myfcgi/func1call
  94. if in the Apache configuration file (ex: httpd.conf) it was set up as:
  95. LoadModule fastcgi_module "<path_to_mod>/mod_fastcgi-2.4.6-AP22.dll"
  96. <IfModule mod_fastcgi.c>
  97. <Directory "<path_to_fcgi_app>">
  98. # Options +ExecCGI <- not needed if ScriptAlias is used below
  99. Order allow,deny
  100. Allow from all
  101. </Directory>
  102. #External FCGI app, has to start manually, Apache will not do it
  103. FastCgiExternalServer "<path_to_fcgi_app>/embedtemplates.exe" -host 127.0.0.1:2015 -idle-timeout 30 -flush
  104. #optionally, to shorten the calling URL and to not display the executable file name (if used, no +ExecCGI is needed above)
  105. ScriptAlias /myfcgi "<path_to_fcgi_app>/embedtemplates.exe"
  106. </IfModule>
  107. Note: You need to change the module name if needed. For example on Linux,
  108. the module is not mod_fastcgi-2.4.6-AP22.dll but mod_fastcgi.so (need to be
  109. compiled from sources found at http://www.fastcgi.com/dist/ ).
  110. The port (2015 in this example) must match the one set in the project main
  111. file (embedtemplates.lpr).
  112. The FCGI application must be running in order for this demo to work (external
  113. FCGI server setup). Do not forget to restart it after changes and
  114. recompilation.
  115. Also, mod_fastcgi is not the same as mod_fcgid that the Apache project is
  116. developing. The latter does not support external FCGI server apps.
  117. There are other ways than external FCGI server apps to handle the FCGI
  118. protocol and both mod_fastcgi and mod_fcgid supports that. However, external
  119. FCGI servers are the best for debugging and development, that is why the
  120. examples do it that way.