从html运行bash脚本

时间:2016-09-10 06:51:51

标签: php html bash nginx

我在这里已经阅读了几个问题,如何从html运行bash脚本。大多数人都把我推荐给了php。 I followed everything in here但是没有去。 我对html或php没有任何经验。 所以我设置了应有的一切(我想......)。 我安装了nginx(并且工作正常)。 我安装了php和php-fpm。

我的简单网页是:

<!DOCTYPE html>
<html>
<head>
<title>My page</title>
</head>
<body>
<button type="button" onclick="run.php">Click Me!</button>
</body>
</html>

我的默认nginx服务器配置是:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.html;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #   proxy_pass http://127.0.0.1:8080;    
    #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    #   root /usr/share/nginx/html;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #   # With php5-cgi alone:
        fastcgi_pass 127.0.0.1:9000;
    #   # With php5-fpm:
    #   fastcgi_pass unix:/var/run/php5-fpm.sock;
    #   fastcgi_index index.php;
    #   include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}

我尝试了两种选择:单独使用php5-cgi /使用php5-fpm

我的php脚本应该在按钮点击时触发:

<?php
shell_exec("ls");
header('Location: https://www.google.com');
?>

但是没有任何事情发生(现在,我只运行简单的ls。当这将工作时我会将其更改为某些/path/script.sh)

我做错了什么?

1 个答案:

答案 0 :(得分:0)

正如我之前提到的,我对html或php没有任何经验。

问题是我分离了html和php文件。合并后,一切正常。

<?php
if ($_GET['run']) {
  # This code will run if ?run=true is set.
  exec("ls");
  header('Location: https://www.google.com');
}
?>

<html>
<head>
<title>My page</title>
</head>
<body>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<a href="?run=true">Click Me!</a>

</body>
</html>