使用fastcgi++编写fastcgi应用初探(一)

fastcgi++作为一个完全C++编写的fastcgi应用开发包,封装了很多功能,比如参数提取,session,mysql数据库连接管理等最大限度的简化cgi编程。
编写一个简单的helloworld的fastcgi应用。

  1. #include <fstream>
  2. #include <boost/date_time/posix_time/posix_time.hpp>
  3.  
  4. #include <fastcgi++/request.hpp>
  5. #include <fastcgi++/manager.hpp>
  6.  
  7. void error_log(const char* msg)
  8. {
  9.         using namespace std;
  10.         using namespace boost;
  11.         static ofstream error;
  12.         if(!error.is_open())
  13.         {
  14.                 error.open("/tmp/errlog", ios_base::out | ios_base::app);
  15.                 error.imbue(locale(error.getloc(), new posix_time::time_facet()));
  16.         }
  17.  
  18.         error << '[' << posix_time::second_clock::local_time() << "] " << msg << endl;
  19. }
  20.  
  21. class Helloworld: public Fastcgipp::Request<char> {
  22. public:
  23.         bool response()
  24.         {
  25.                 out << "<html><body>";
  26.                 out << "Hello world!";
  27.                 out << "</body></html>";
  28.                 return true;
  29.         }  
  30. }
  31. main(){
  32.         try
  33.         {
  34.                 Fastcgipp::Manager<Helloworld> fcgi;
  35.                 fcgi.handler();
  36.         }
  37.         catch(std::exception& e)
  38.         {
  39.                 error_log(e.what());
  40.         }
  41. }

可以看到基本的只要从 Fastcgipp::Request 派生一个类,实现其中的 bool response() 函数就可以完成一个fastcgi 应用。

Popularity: 7% [?]

Related

在RHEL5.3上安装基于postfix的extmail邮件系统

extmail官方网站上的安装指南是针对4.x系统的,网上找了一个5.x的安装。
参考在centos/redhat 5.x 上安装邮件系统postfix+extmail+courier-imap

安装步骤.

1.系统准备

使用默认方式安装系统后,从RHEL安装盘上拷贝一些下面安装步骤需要的rpm包先安装上。

  1. rpm -ivh postgresql-devel-8.1.11-1.el5_1.1.i386.rpm
  2. rpm -ivh expect-5.43.0-5.1.i386.rpm
  3. rpm -ivh libtool-ltdl-devel-1.5.22-6.1.i386.rpm
  4.  
  5. rpm -ivh mysql-devel-5.0.45-7.el5.i386.rpm
  6. rpm -ivh openldap-servers-2.3.43-3.el5.i386.rpm
  7. rpm -ivh openldap-servers-sql-2.3.43-3.el5.i386.rpm
  8.  
  9. rpm -ivh pcre-6.6-2.el5_1.7.i386.rpm
  10. rpm -ivh pcre-devel-6.6-2.el5_1.7.i386.rpm

再从 www.extmail.org 网站上下载最新的 extmail 相关包

extmail-1.1.0.tar.gz
extman-1.0.0.tar.gz
slockd-0.99.tar.gz

2.导入extman后台数据库数据

  1. tar -xzvf extman-1.0.0.tar.gz
  2. cd extman-1.0.0/docs
  3. mysql -u root -p
  4. mysql> source extmail.sql
  5. mysql> source init.sql
  6. mysql> exit

Read more

Popularity: 12% [?]

Related

为了Awstats给Nginx添加FastCGI方式的Perl支持

有个应用(awstats)需要Perl脚本支持,但是用的Nginx服务器,对Perl支持不好,于是通过FastCGI方式来使用Perl。

首先安装Perl的FCGI模块

  1. wget http://www.cpan.org/modules/by-module/FCGI/FCGI-0.67.tar.gz
  2. tar -zxvf FCGI-0.67.tar.gz
  3. cd FCGI-0.67
  4. perl Makefile.PL
  5. make && make install

还可以使用如下方法安装:

perl -MCPAN -e 'install FCGI'

安装FCGI-ProcManager

  1. wget http://search.cpan.org/CPAN/authors/id/G/GB/GBJK/FCGI-ProcManager-0.18.tar.gz
  2. tar -xzxf FCGI-ProcManager-0.18.tar.gz
  3. cd FCGI-ProcManager-0.18
  4. perl Makefile.PL
  5. make
  6. make install

Perl的FastCGI启动脚本

参考这篇文章 http://bbs.chinaunix.net/archiver/?tid-1224968.html

vi fcgi_perl
  1. #!/usr/bin/perl -w
  2. use FCGI;
  3. use Socket;
  4. use FCGI::ProcManager;
  5. sub shutdown { FCGI::CloseSocket($socket); exit; }
  6. sub restart { FCGI::CloseSocket($socket); &main; }
  7. use sigtrap 'handler', \&shutdown, 'normal-signals';
  8. use sigtrap 'handler', \&restart, 'HUP';
  9. require 'syscall.ph';
  10. use POSIX qw(setsid);
  11.  
  12. #&daemonize; we don't daemonize when running under runsv
  13. #this keeps the program alive or something after exec'ing perl scripts
  14. END() { }
  15. BEGIN() { }
  16. {
  17. no warnings;
  18. *CORE::GLOBAL::exit = sub { die "fakeexit\nrc=" . shift() . "\n"; };
  19. };
  20. eval q{exit};
  21. if ($@) {
  22. exit unless $@ =~ /^fakeexit/;
  23. }
  24. &main;
  25.  
  26. sub daemonize() {
  27. chdir '/' or die "Can't chdir to /: $!";
  28. defined( my $pid = fork ) or die "Can't fork: $!";
  29. exit if $pid;
  30. setsid() or die "Can't start a new session: $!";
  31. umask 0;
  32. }
  33.  
  34. sub main {
  35. #如果使用 IP sockets
  36. #$socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 );
  37. #如果使用 UNIX sockets
  38. #$socket = FCGI::OpenSocket( "/var/run/perl_cgi-dispatch.sock", 10 );
  39.  
  40. #foreach $item (keys %ENV) { delete $ENV{$item}; }
  41. #设置fastcgi进程数,默认四个
  42. my $n_processes = $ENV{FCGI_NPROCESSES} || 4;
  43. $proc_manager = FCGI::ProcManager->new( {n_processes => $n_processes} );
  44. #使用unix socket
  45. $socket = FCGI::OpenSocket( "$ENV{FCGI_SOCKET_PATH}", 10 );
  46. #设置Socket权限
  47. chmod 0777, $ENV{FCGI_SOCKET_PATH};
  48.  
  49. ; #use UNIX sockets – user running this script must have w access to the 'nginx' folder!!
  50. $request =
  51. FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket,
  52. &FCGI::FAIL_ACCEPT_ON_INTR );
  53. $proc_manager->pm_manage();
  54. if ($request) { request_loop() }
  55. FCGI::CloseSocket($socket);
  56. }
  57.  
  58. sub request_loop {
  59. while ( $request->Accept() >= 0 ) {
  60. $proc_manager->pm_pre_dispatch();
  61.  
  62. #processing any STDIN input from WebServer (for CGI-POST actions)
  63. $stdin_passthrough = '';
  64. { no warnings; $req_len = 0 + $req_params{'CONTENT_LENGTH'}; };
  65. if ( ( $req_params{'REQUEST_METHOD'} eq 'POST' ) && ( $req_len != 0 ) )
  66. {
  67. my $bytes_read = 0;
  68. while ( $bytes_read < $req_len ) {
  69. my $data = '';
  70. my $bytes = read( STDIN, $data, ( $req_len$bytes_read ) );
  71. last if ( $bytes == 0 || !defined($bytes) );
  72. $stdin_passthrough .= $data;
  73. $bytes_read += $bytes;
  74. }
  75. }
  76.  
  77. #running the cgi app
  78. if (
  79. ( -x $req_params{SCRIPT_FILENAME} ) && #can I execute this?
  80. ( -s $req_params{SCRIPT_FILENAME} ) && #Is this file empty?
  81. ( -r $req_params{SCRIPT_FILENAME} ) #can I read this file?
  82. )
  83. {
  84. pipe( CHILD_RD, PARENT_WR );
  85. pipe( PARENT_ERR, CHILD_ERR );
  86. my $pid = open( CHILD_O, "-|" );
  87. unless ( defined($pid) ) {
  88. print("Content-type: text/plain\r\n\r\n");
  89. print
  90. "Error: CGI app returned no output – Executing $req_params{SCRIPT_FILENAME} failed !\n";
  91. next;
  92. }
  93. $oldfh = select(PARENT_ERR);
  94. $| = 1;
  95. select(CHILD_O);
  96. $| = 1;
  97. select($oldfh);
  98. if ( $pid > 0 ) {
  99. close(CHILD_RD);
  100. close(CHILD_ERR);
  101. print PARENT_WR $stdin_passthrough;
  102. close(PARENT_WR);
  103. $rin = $rout = $ein = $eout = '';
  104. vec( $rin, fileno(CHILD_O), 1 ) = 1;
  105. vec( $rin, fileno(PARENT_ERR), 1 ) = 1;
  106. $ein = $rin;
  107. $nfound = 0;
  108.  
  109. while ( $nfound =
  110. select( $rout = $rin, undef, $ein = $eout, 10 ) )
  111. {
  112. die "$!" unless $nfound != -1;
  113. $r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;
  114. $r2 = vec( $rout, fileno(CHILD_O), 1 ) == 1;
  115. $e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;
  116. $e2 = vec( $eout, fileno(CHILD_O), 1 ) == 1;
  117.  
  118. if ($r1) {
  119. while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {
  120. print STDERR $errbytes;
  121. }
  122.  
  123. if ($!) {
  124. $err = $!;
  125. die $!;
  126. vec( $rin, fileno(PARENT_ERR), 1 ) = 0
  127. unless ( $err == EINTR or $err == EAGAIN );
  128. }
  129. }
  130. if ($r2) {
  131. while ( $bytes = read( CHILD_O, $s, 4096 ) ) {
  132. print $s;
  133. }
  134. if ( !defined($bytes) ) {
  135. $err = $!;
  136. die $!;
  137. vec( $rin, fileno(CHILD_O), 1 ) = 0
  138. unless ( $err == EINTR or $err == EAGAIN );
  139. }
  140. }
  141. last if ( $e1 || $e2 );
  142. }
  143. close CHILD_RD;
  144. close PARENT_ERR;
  145. waitpid( $pid, 0 );
  146. } else {
  147. foreach $key ( keys %req_params ) {
  148. $ENV{$key} = $req_params{$key};      
  149. }
  150.  
  151. # cd to the script's local directory
  152. if ( $req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/]+$/ ) {
  153. chdir $1;
  154. }
  155. close(PARENT_WR);
  156.  
  157. #close(PARENT_ERR);
  158. close(STDIN);
  159. close(STDERR);
  160.  
  161. #fcntl(CHILD_RD, F_DUPFD, 0);
  162. syscall( &SYS_dup2, fileno(CHILD_RD), 0 );
  163. syscall( &SYS_dup2, fileno(CHILD_ERR), 2 );
  164.  
  165. #open(STDIN, "<&CHILD_RD");
  166. exec( $req_params{SCRIPT_FILENAME} );
  167. die("exec failed");
  168. }
  169. } else {
  170. print("Content-type: text/plain\r\n\r\n");
  171. print
  172. "Error: No such CGI app – $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
  173. }
  174. }
  175. }

再设置启动的脚本

vi startfcgiperl
  1. export FCGI_SOCKET_PATH="/tmp/perl_fcgi.socket"
  2. export FCGI_NPROCESSES=4
  3. ./fcgi_perl &

为Nginx添加FastCGI的Perl支持

编辑 nginx.conf 脚本,添加如下内容

location ~* .*\.pl$
{
include awstats.conf;
}
location /awstatsicon/
{
   alias /var/www/awstats-6.8/wwwroot/icon/;
}
vi awstats.conf
fastcgi_pass unix:/tmp/perl_fcgi.socket;
fastcgi_index awstats.pl;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param SCRIPT_FILENAME /var/www/awstats-6.8/wwwroot/cgi-bin$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /var/www/awstats-6.8/wwwroot/cgi-bin/awstats.pl;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_read_timeout 60;

配置Awstats

运行 /var/www/awstats-6.8/tools/awstats_configure.pl 设置好一些变量

mkdir -p /etc/awstats
cp /var/www/awstats-6.8/wwwroot/awstats.model.conf /etc/awstats/awstats.my.site.com.conf

使用 vi /etc/awstats.my.site.com.conf 进行一些参数修改,设置icon目录,DirData数据存放目录,来源log日志文件路径等
使用 /var/www/awstats-6.8/tool/awstats_updateall.pl now -awstatsprog=/var/www/awstats-6.8/wwwroot/cgi-bin/awstats.pl 命令进行初始数据更新
使用 http://my.site.com/awstats.pl 查看数据

后续

将 ./awstats_updateall.pl now -awstatsprog=/var/www/awstats-6.8/wwwroot/cgi-bin/awstats.pl 放到crontab 中做定时更新,可以做一个脚本和日志文件的处理一起做。

碰到问题
在使用unix socket时,碰到两个问题
1.写socket的目录没有写权限,添加上解决
2.nginx连不上socket, 看日志提示没有权限,在创建了socket后添加一句
chmod 0777, $ENV{FCGI_SOCKET_PATH};
问题解决。

遗留对awstats.pl访问密码限制问题尚未解决好。
解决 在 nginx 中按如下配置方式

location /awstats {
  root /var/www/awstats-6.8/;
  include awstats.conf;
  auth_basic "awstats";
  auth_basic_user_file /var/www/.passwd;
}

其中 将 awstats-6.8 目录下的 wwwroot 修改为 awstats, /var/www/.passwd 为访问目录要用来验证的账号密码文件,用apache的提供的工具htpasswd制作而成

Popularity: 7% [?]

Related

安装配置Nginx 0.6.32 +php 5.2.6(fastcgi) + Mysql 5.0.51

安装过程参考: http://blog.s135.com/read.php/314.htm

安装 mysql 5.0.51

安装 php 5.2.6

安装 nginx

nginx的中文Wiki站点 http://wiki.codemongers.com/NginxChs
niginx的英文站点 http://nginx.net

配置开机启动Mysql+Phpcgi+Nginx

因为没有安装服务,需要添加一些命令到 /etc/rc.local 中用来开机启动

  1. ulimit -SHn 51200
  2. /bin/sh /usr/local/mysql5051/bin/mysqld_safe –defaults-file=/usr/local/mysql5051/my.cnf &
  3. /usr/local/php526/spawn-fcgi -a 127.0.0.1 -p 10080 -C 30 -u www -f /usr/local/php526/bin/php-cgi
  4. /usr/local/nginx632/sbin/nginx

设置iptables

在防火墙中放开相关服务端口。

  1. #web port
  2. -A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
  3. #fastcgi port
  4. -A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 10080 -s 127.0.0.1 -j ACCEPT
  5. #mysql 本地通过unix socket访问, 不用设置
  6. -A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp -s 127.0.0.1 –dport 3306 -j ACCEPT

使用 /sbin/service iptables restart 重新应用防火墙规则

验证服务状态

验证 nginx + php 运行是否正常。
在 /var/www/xxt.igrow.cn 目录下建一个test.php,里面存放内容

  1. <?
  2.      echo phpinfo();
  3. ?>

通过 curl http://127.0.0.1/test.php 可以看到输出的php信息,表示 nginx + php 运行正常。

Popularity: 4% [?]

Related