How to run bash script from Nginx

亡梦爱人 提交于 2020-12-31 06:44:47

问题


1) I have static site and wand to set "autopull" from bitbucket.

2) I have webhook from bitbucket.

3) I have bash script which do "git pull"

How can I run this script when nginx catch request?

server {

    listen   80;
    server_name example.ru;

    root /path/to/root;
    index index.html;

    access_log /path/to/logs/nginx-access.log;
    error_log /path/to/logs/nginx-error.log;

    location /autopull {
        something to run autopull.sh;
    }

    location / {
        auth_basic "Hello, login please";
        auth_basic_user_file /path/to/htpasswd;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $host;
    }
}

I tried lua_block and fastcgi service, but both are failed. lua does not run os.execute("/path/to/script") and does not write the log. fastcgi is more successful, but it has not permissions, because my www-data user doesn't have ssh-key in my bitbuchet repo.


回答1:


Problem solved.

I didnt want to use any script/process on another port because I have several sites and I need port for each.

My final configuration is:

server {

    listen   80;
    server_name example.ru;

    root /path/to/project;
    index index.html;

    access_log /path/to/logs/nginx-access.log;
    error_log /path/to/logs/nginx-error.log;

    location /autopull {
        content_by_lua_block {
            io.popen("bash /path/to/autopull.sh")
        }
    }

    location / {
        auth_basic "Hello, login please";
        auth_basic_user_file /path/to/htpasswd;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $host;
    }
}

Problem was in permission of www-data user and its ssh-kay in repo.




回答2:


Based on this, create py script

#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from subprocess import call

PORT_NUMBER = 8080
autopull = '/path/to/autopull.sh'
command = [autopull]

#This class will handles any incoming request from
#the browser 
class myHandler(BaseHTTPRequestHandler):

    #Handler for the GET requests
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        # Send the html message
        self.wfile.write("runing {}".format(autopull))
        call(command)
        return

try:
    #Create a web server and define the handler to manage the
    #incoming request
    server = HTTPServer(('', PORT_NUMBER), myHandler)
    print 'Started httpserver on port ' , PORT_NUMBER

    #Wait forever for incoming htto requests
    server.serve_forever()

except KeyboardInterrupt:
    print '^C received, shutting down the web server'
    server.socket.close()

Run it and in nginx config add

location /autopull { proxy_pass http://localhost:8080; }


来源:https://stackoverflow.com/questions/60757496/how-to-run-bash-script-from-nginx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!