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. 大数加法(SDUT“斐波那契”串)4335

    题目链接:https://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2697/pid/4335.ht ...

  2. TinyOS 代码分析

    1.Basestation案例   位于/opt/tinyos-main-master/apps/Basetation 1.1本例的顶层结构图: 1.2软件实现流程 1) uartIn,uartOut ...

  3. 利用Jsoup模拟跳过登录爬虫获取数据

    今天在学习爬虫的时候想着学习一下利用jsoup模拟登录.下面分为有验证码和无验证码的情况进行讨论. ---------------------------无验证码的情况---------------- ...

  4. weblogic性能监控

    1.

  5. UVA题解一

    UVA 100 题目描述:经典3n+1问题在\(n \leq 10^6\)已经证明是可行的,现在记\(f[n]\)为从\(n\)开始需要多少步才能到\(1\),给出\(L, R\),问\(f[L], ...

  6. [转载]C++多态技术

    摘自: http://www.royaloo.com/articles/articles_2003/PolymorphismInCpp.htm http://blog.sciencenet.cn/bl ...

  7. 当array_filter函数的callback留空时 他会过滤掉所有键值为false的键

    当array_filter函数的callback留空时 他会过滤掉所有键值为false的键

  8. java.lang.IllegalArgumentException: Page directive: invalid value for import

    我的项目原来用的tomcat版本是apache-tomcat-7.0.53,后来为了安全原因将版本升至\apache-tomcat-7.0.57,发现有的jsp页面出现下面的异常: java.lang ...

  9. webservice使用

    soap方法 一:PHP本身的SOAP 所有的webservice都包括服务端(server)和客户端(client). 要使用php本身的soap首先要把该拓展安装好并且启用.下面看具体的code ...

  10. HTML表格的基本操作

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...