nginx本身不支持直接调用shell脚本,我们可以通过安装fastcgi程序,让nginx把调用shell的http请求交给fastcgi程序去处理,然后nginx 再将结果返回给用户方式间接调用shell,这里我们推荐安装fcgiwrap这个通用的 fastcgi 进程管理器来帮助nginx 处理shell程序

一、安装fcgiwrap

# 安装 epel 源
yum -y install epel-release # 安装 fcgi 依赖
yum -y install fcgi fcgi-devel # 安装 fcgiwrap
wget https://github.com/gnosek/fcgiwrap/archive/master.zip
unzip master.zip
cd fcgiwrap-master
autoreconf -i
./configure
make
make install

通过执行以上命令将会把fcgiwrap安装到/usr/local/sbin/fcgiwrap这个路径

二、安装spawn-fcgi

通过安装spawn-fcgi方便启动fcgiwrap程序

#安装spawn-fcgi
yum install spawn-fcgi
修改/etc/sysconfig/spawn-fcgi配置文件
vi /etc/sysconfig/spawn-fcgi

修改配置文件为以下内容

# You must set some working options before the "spawn-fcgi" service will work.
# If SOCKET points to a file, then this file is cleaned up by the init script.
#
# See spawn-fcgi(1) for all possible options.
#
# Example :
#SOCKET=/var/run/php-fcgi.sock
#OPTIONS="-u apache -g apache -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi"
FCGI_SOCKET=/var/run/fcgiwrap.socket
FCGI_PROGRAM=/usr/local/sbin/fcgiwrap
FCGI_USER=nobody
FCGI_GROUP=nobody
FCGI_EXTRA_OPTIONS="-M 0700"
OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi.pid -- $FCGI_PROGRAM"

添加spawn-fcgi开机启动服务

chkconfig --levels 235 spawn-fcgi on

启动spawn-fcgi服务

/etc/init.d/spawn-fcgi start

三、配置nginx执行shell调用

      修改nginx配置文件

vi /usr/local/nginx/conf/nginx.conf

增加以下内容

 location ~ ^/.*\.sh  {
gzip off;
root /home/shell; #shell文件存放目录
fastcgi_pass unix:/var/run/fcgiwrap.socket;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

四、创建shell文件

vi /home/shell/hello.sh

添加以下内容

#!/bin/bash
echo "Content-Type:text/html"
echo ""
echo "hello world!"

添加shell脚本执行权限

chmod -R 755 /home/shell

然后在浏览器中输入http://<ip>:port/hello.sh

即可调用我们的shell脚本

说明:

1、脚本前三行是必须的,第一行用于指定脚本执行使用的解释器,第二行和第三行是HTTP协议规范,在发送HTML正文之前要发送MIME头和空行

2、shell脚本都是使用nobody用户执行的,所以要确保nobody可以读取并执行hello.sh

参考文档:https://www.howtoforge.com/serving-cgi-scripts-with-nginx-on-centos-6.0-p2

在centos6.0上通过nginx远程执行shell的更多相关文章

  1. Centos6.2上做nginx和tomcat的集成及负载均衡(已实践)

    Centos6.2上做nginx和tomcat的集成及负载均衡 ---------------------------------------------------------Jdk-------- ...

  2. Java SSH远程执行Shell脚本实现(转)

    前言 此程序需要ganymed-ssh2-build210.jar包(下载地址:http://www.ganymed.ethz.ch/ssh2/) 为了调试方便,可以将\ganymed-ssh2-bu ...

  3. Java实践 — SSH远程执行Shell脚本(转)

    原文地址:http://www.open-open.com/lib/view/open1384351384024.html 1. SSH简介         SSH是Secure Shell的缩写,一 ...

  4. Linux远程执行shell命令

    Linux远程执行shell命令   在Linux系统中,我们经常想在A机器上,执行B机器上的SHELL命令. 下面这种方案,是一种流行可靠的方案. 1.SSH无密码登录 # 本地服务器执行(A机器) ...

  5. Java实践 — SSH远程执行Shell脚本

    1. SSH简介         SSH是Secure Shell的缩写,一种建立在应用层和传输层基础上的安全协议.SSH在连接和传送过程中会加密所有数据,可以用来在不同系统或者服务器之间进行安全连接 ...

  6. Linux远程执行Shell命令或脚本

    ## 远程执行shell命令 ssh [user]@[server] '[command]' # eg. ssh root@192.168.1.1 'uptime' ## 远程执行本地shell脚本 ...

  7. JAVA远程执行Shell脚本类

    1.java远程执行shell脚本类 package com.test.common.utility; import java.io.IOException; import java.io.Input ...

  8. Jenkins远程执行shell出现java: command not found

    之前在使用Jenkins执行远程shell脚本时,出现提示java: command not found:多方查找原因后发现是因为远程执行shell脚本时,不会自动加载环境变量,导致出现此错误,解决方 ...

  9. 在CentOS6.0上安装Oracle 11gR2 (11.2.0.1)以及基本的配置(一)

    首先安装CentOS6.0   就不用说了.安装即可.唯一需要注意的就是后面Oracle 11G Installation guide中的Checking the Software Requireme ...

随机推荐

  1. 按键精灵MySql数据库操作

    查询 MySql服务器地址="192.168.1.166" 用户名 = "root" 密码 = " 数据库名="bookshop" ...

  2. 蓝色的oa模板html_综合信息服务管理平台OA模板——后台

    链接:http://pan.baidu.com/s/1qXGGOAK 密码:2otu

  3. python中赋值、浅拷贝、深拷贝详解(转)

    一.赋值 >>> a = [1, 2, 3]>>> b = a>>> print(id(a), id(b), sep='\n')139701469 ...

  4. 二. Jmeter--关联

    1. 首先建立一个线程组(Thread Group),为什么所有的请求都要加入线程组这个组件呢?不加不行吗?答案当然是不行的.因为jmeter的所有任务都必须由线程处理,所有任务都必须在线程组下面创建 ...

  5. xss自动化攻击

    所需工具 [1.xssValidator] [2.phantomjs] [3.xss.js] /** * This is a basic phantomJS script that will be u ...

  6. ASLR

    @author:dlive ASLR address space layout randomization 微软从windows vista/windows server 2008(kernel ve ...

  7. ip_rcv && ip_rcv_finish

    (1) 在inet_init中注册了类型为ETH_P_IP协议的数据包的回调ip_rcv (2) 当二层数据包接收完毕,会调用netif_receive_skb根据协议进行向上层分发 (3) 类型为E ...

  8. 在64位ubuntu中安装代码比较工具beyond compare

    1,//从http://www.scootersoftware.com/download.php 官方地址下载 bcompare-3.3.2.14050.tar.gz 或 bcompare-4.0.7 ...

  9. HttpURLConnection传json

    private static String sendToWangTing(DataRow dataRow) throws IOException{ String ip = Configuration. ...

  10. 手机页面或是APP中减少使用setTimeout和setInterval,因为他们会导致页面卡顿

    1.setTimeout致使页面的卡顿或是不流畅,打乱模块的生命周期 ,还有setTimeout其实是很难调试的. 当一个页面有众多js代码的时候,setTimeout就是导致页面的卡顿. var s ...