安装nginx
sudo apt-get install nginx
安装cgi库fcgiwrap
sudo apt-get install fcgiwrap
安装以后fcgiwrap默认已经启动,对应的套接字是 /var/run/fcgiwrap.socket 。如果没有启动,使用 /etc/init.d/fcgiwrap 手动启动。
配置
vi /etc/nginx/sites-enabled/default
//在第一个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;
# 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;
}
重启nginx
sudo service nginx restart
然后在/var/www(上面的root后面是什么就在什么目录)下新建一个cgi-bin目录,如:
mkdir /var/www/cgi-bin
测试
vi /var/www/cgi-bin/hello.sh
#!/bin/bash
echo "Content-Type:text/html"
echo ""
echo "hello world!"
脚本前三行是必须的,第一行用于指定脚本执行使用的解释器,第二行和第三行是HTTP协议规范,在发送HTML正文之前要发送MIME头和空行
因为脚本是执行文件,所以要给执行权限
chmod +x /var/www/cgi-bin/hello.sh
如果没问题,访问http://localhost/cgi-bin/hello.sh会输出hello world