1、安裝環境Ubuntu16.04
2、安裝lighttpd
apt install lighttpd
3、安裝fastcgi
下載下傳源碼:fastcgi-2.4.1-SNAP-0910052249.tar.bz2
解壓:
tar -xjvf fastcgi-2.4.1-SNAP-0910052249.tar.bz2
進入源碼:
cd fastcgi-2.4.1-SNAP-0910052249
修改
fcgi-2.4.1-SNAP-0910052249/libfcgi/fcgiapp.c
,在裡面加一句
#include <stdio.h>
。否則編譯報錯。
配置:
./config
編譯:
make
安裝:
make install
将編譯出的動态庫加入環境變量:
vim /etc/ld.so.conf
在下面加入一行:
/usr/local/lib
使環境變量生效:
ldconfig
4、編寫C語言程式
vim test.c
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <alloca.h>
#include <fcgiapp.h>
#define LISTENSOCK_FILENO 0
#define LISTENSOCK_FLAGS 0
int main(int argc, char **argv)
{
int err = FCGX_Init(); /* call before Accept in multithreaded apps */
if (err)
{
return 1;
}
FCGX_Request cgi;
err = FCGX_InitRequest(&cgi, LISTENSOCK_FILENO, LISTENSOCK_FLAGS);
if (err)
{
return 2;
}
while (1)
{
err = FCGX_Accept_r(&cgi);
if (err)
{
break;
}
char **envp;
int size = 200;
char *result = (char *)alloca(size);
strcpy(result, "Status: 200 OK\r\nContent-Type: text/html\r\n\r\n");
strcat(result, "<html><h1>TestCGI!</h1></html>\r\n");
FCGX_PutStr(result, strlen(result), cgi.out);
}
return 0;
}
編寫makefile:
vim Makefile
all:
gcc test.c -o test.fastcgi -lfcgi
編譯C語言程式:
make
5、編寫index.html
<html>
<head>
<title>first cgi page</title>
</head>
<body>
<p>hahaha...</p>
</body>
</html>
6、修改配置
建立
/var/www/html
目錄,将上一步編寫的
index.html
拷貝到該目錄下。
建立
/var/www/html/cgi-bin
目錄,将之前編寫的
test.fastcgi
程式拷貝到該目錄。
修改lighttpd配置:
vim /etc/lighttpd/lighttpd.conf
server.modules = (
"mod_access",
"mod_alias",
"mod_compress",
"mod_redirect",
"mod_rewrite",
"mod_fastcgi" #新增fastcgi子產品
)
server.document-root = "/var/www/html" #網頁主目錄
server.upload-dirs = ( "/var/cache/lighttpd/uploads" )
server.errorlog = "/var/log/lighttpd/error.log"
server.pid-file = "/var/run/lighttpd.pid"
#server.username = "www-data"
#server.groupname = "www-data"
server.port = 80
index-file.names = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ( "application/javascript", "text/css", "text/html", "text/plain" )
# default listening port for IPv6 falls back to the IPv4 port
## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"
vim /etc/lighttpd/conf-available/10-fastcgi.conf
# /usr/share/doc/lighttpd/fastcgi.txt.gz
# http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions#mod_fastcgi-fastcgi
server.modules += ( "mod_fastcgi" )
fastcgi.server = (
"/test" => ((
"socket" => "/var/www/html/test.socket",
"bin-path" => "/var/www/html/cgi-bin/test.fastcgi",
"check-local" => "disable"
))
)
7、啟動
lighttpd
服務
service lighttpd start
可以通過
lsof -i:80
檢視服務是否啟動成功
lighttpd使能fastcgi子產品
lighty-enable-mod
重新加載配置檔案
/etc/init.d/lighttpd force-reload
8、測試
浏覽器登入
127.0.0.1
浏覽器登入
127.0.0.1/test