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. MongoDB之python简单交互(三)

    python连接mongodb有多种orm,主流的有pymongo和mongoengine. pymongo 安装相关模块 pip install pymongo pymongo操作 主要对象 Mon ...

  2. [004] last_k_node

    [Description] find the k-th node from the last node of single linked list. e.g. Linked-list: 1-2-3-4 ...

  3. elasticsearch删除索引报错【原】

    如果elasticsearch删除索引报错 curl -X DELETE 'http://10.73.26.66:9200/httpd-34-2017.08.15' {"error" ...

  4. 15个你不得不知道的Chrome dev tools的小技巧

    转载自:https://www.imooc.com/article/2559 谷歌浏览器如今是Web开发者们所使用的最流行的网页浏览器.伴随每六个星期一次的发布周期和不断扩大的强大的开发功能,Chro ...

  5. 工作当中遇到的ssh错误

    [root@1bcc1d3f9666 externalscripts]# /usr/sbin/sshd Could not load host key: /etc/ssh/ssh_host_rsa_k ...

  6. openssh升级步骤

    1下载openssh最新版本 2 configure ./configure --prefix= /ssh先配置一下 再在本地安装. make &makeinstall 3 按照/ssh包含内 ...

  7. 【转+整理+答案】python315+道面试题

    提示 自己整理的答案,很局限,如有需要改进的地方,或者有更好的答案,欢迎提出! [合理利用 Ctrl+F 提高查找效率] 第一部分 Python基础篇(80题) 1.为什么学习Python? # 因为 ...

  8. Linux命令之dig命令实例讲解

    1.查看域名的A记录 # dig yahoo.com; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6_3.2 <<> ...

  9. prototype 与 __proto__

    原文:http://rockyuse.iteye.com/blog/1426510 说到prototype,就不得不先说下new的过程. 我们先看看这样一段代码: 1 <script type= ...

  10. 小程序授权怎么写 , 用户点击取消授权 调用 wx.authorize

    点击获取授权 onLoad: function (options) { console.log("onLoad====="); var that=this; wx.getUserI ...