nginx php 执行shell,如何在在Nginx上运行Shell脚本?

前因

在部署Smokeping过程中,不熟悉Apache2的配置。

强硬使用Nginx替换之,最困难的就是要搞定CGI运行的问题。

后果

在Nginx上跑一个Shell脚本

前置部署

Nginx不支持运行CGI程序,需要借助 fcgiwrap 将CGI转成 FastCGI。

apt-get -y install nginx fcgiwrap

建议将fcgiwrap监听权限设置成和Nginx保持一致,以www-data为例:

# /etc/init.d/fcgiwrap

FCGI_SOCKET_OWNER="www-data"

FCGI_SOCKET_GROUP="www-data"

启动后会监听 unix:/var/run/fcgiwrap.socket。

7760e48be49f11c9aac74eb9f35908c5.png

配置服务

Nginx的关键配置如下:

server {

listen 23333 backlog=233;

charset utf-8;

location / {

root /data/smokeping/htdocs;

access_log off;

log_not_found off;

index smokeping.cgi;

}

location ~ ^/.*\.(cgi|sh) { # 主要是这里

gzip off;

default_type text/plain;

root /data/smokeping/htdocs;

include /etc/nginx/fastcgi_params;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_index smokeping.cgi;

fastcgi_pass unix:/var/run/fcgiwrap.socket;

allow 121.22.33.3;

deny all;

}

location ~ ^(.*)\/\.(svn|git|hg|bzr|cvs)\/ {

deny all;

access_log off;

log_not_found off;

}

location ~ /\. {

deny all;

access_log off;

log_not_found off;

}

location /do_not_delete.html {

access_log off;

return 200;

}

}

功能验证

此时在/data/smokeping/htdocs目录下的 *.cgi 或 *.sh 将会被执行。

#!/bin/bash

# /data/smokeping/htdocs/start.sh

echo -ne "Content-Type:text/html;charset=utf-8\r\n\r\n"

echo "

$0


$(top -bcHin1)
"

exit 0

a91a5b55183016aeb5a9852a633912a2.png

大功告成,接下来任你发挥。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值