web执行shell脚本
转载请注明来源:https://www.cnblogs.com/Sherlock-L/p/15584456.html
缘起
去年写过一个shell脚本用来校验统计打点,工作使用。发现同事不太熟悉这块,使用起来也就不太顺,而且数据文件更新也是个问题。于是我萌生了一个想法,要不做成web傻瓜式工具吧,just do it!
过程
直接用bash做成web server,我还真没试过,忽然有点无从下手的感觉。Stack Overflow上倒是给了我灵感,别死盯着shell,考虑下别家呗:https://stackoverflow.com/questions/44443164/execute-a-shell-script-from-html
环境准备
先说明一下,我用的是Ubuntu系统。
leah@ubuntu:/var/www/html$ cat /proc/version
Linux version 4.4.0-31-generic (buildd@lgw01-43) (gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) ) #50~14.04.1-Ubuntu SMP Wed Jul 13 01:07:32 UTC 2016
1.安装PHP和apache
软件包安装
执行命令
sudo apt-get install php5-cli
sudo apt-get install apache2
运行php -v以及service apache2 status查看php和apache是否安装成功,
源码安装
我是因为在某个机器上没有root权限,才用了源码安装方式把东西装在自己的家目录里,如果只是为了使用不做别的,还是建议用软件包安装方式
apache需要下载下面几个依赖包:
apr:http://apr.apache.org/download.cgi
apr-util:http://apr.apache.org/download.cgi
pcre:https://sourceforge.net/projects/pcre/files/pcre/
httpd:http://httpd.apache.org/download.cgi
我下载的是tar.gz文件,然后解压到了家目录(tar -zxvf xxxxxx.tar.gz -C /home/leah/),接下来是依次安装:
- apr:
./configure --prefix=/home/leah/apr
make
make install
- apr-util(需要expat库,参考下方②):
./configure --prefix=/home/leah/apr-util --with-apr=/home/leah/apr/bin/apr-1-config --with-expat=/home/leah/expat
make
make install
- pcre:
./configure --prefix=/home/leah/pcre --with-apr=/home/leah/apr/bin/apr-1-config
make
make install
- httpd(需要先安装openssl,参考下方③,这里configure的参数,是根据各种报错才加那么长的参数的):
./configure --prefix=/home/leah/apache2 --with-apr=/home/leah/apr/bin/apr-1-config --with-apr-util=/home/leah/apr-util/bin/apu-1-config --with-pcre=/home/leah/pcre/bin/pcre-config --with-ssl=/home/leah/openssl --sysconfdir=/home/leah/httpd --enable-so --enable-rewrite --enable-ssl
make
make install
可能遇到的问题:
①安装apr过程中执行./configure -C xxxxx时遇到rm: cannot remove 'libtoolT': No such file or directory,可注释这一行
$RM "$cfgfile"
或者参考这篇博文,加个-f参数
②安装apr-util过程中执行make,报错:
xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory
#include <expat.h>
^
compilation terminated.
make[1]: *** [xml/apr_xml.lo] Error 1
make[1]: Leaving directory `/home/leah/apr-util-1.6.1'
make: *** [all-recursive] Error 1
解决方式为,安装expat库:https://libexpat.github.io/doc/packages/
./configure --prefix=/home/leah/expat
make
make install
但是有个问题,由于expat我也是自定义路径安装的,这导致了即使安装完expat后,在安装apr-util的时候,make仍然报上述错误。
make clean
./configure的时候加上--with-expat=/path-to-expat-installation-dir
问题完美解决。
make clean:清除上次的make命令所产生的object文件(后缀为“.o”的文件)及可执行文件。
注:若使用源码编译的expat,建议在后续编译apache的时候添加--with-expat=/path-to-expat-installation-dir参数
③安装httpd时,执行./configure xxxxxx,报错:
checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures
解决方式为安装openssl:https://www.openssl.org/source/
我安装的是openssl-1.1.1l.tar.gz,参考了https://linuxtect.com/how-to-install-openssl-libraries-on-ubuntu-debian-mint/ 进行安装,这里用的不是Configure而是config哦~
./config --prefix=/home/leah/openssl --openssldir=/home/leah/openssl
make
make install
注:上面的make install会有一个关于权限的报错:Cannot create directory /usr/local/openssl: Permission denied ,这个我没有理会。
④安装完成后执行/home/leah/apache2/bin/apachectl start,报错:
(13)Permission denied: AH00072: make_sock: could not bind to address [::]:80
(13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
AH00015: Unable to open logs
Action 'start' failed.
The Apache error log may have more information.
然而用root身份执行是可以正常启动apache的,果然,不用root身份,是真的寸步难行,先探索到这里吧。
2.测试apache能否解析php
执行命令
echo '<?php phpinfo(); ?>' > /var/www/html/infophp.php
p.s.:如果permission denied,那就chmod修改一下权限。
浏览器访问http://你的机器ip/infophp.php,可以看到php的版本信息
代码编写
在php中调用shell脚本可以使用shell_exec,由于实际代码涉及公司数据就不放出来了,下面是个简单的例子
<?php
if(isset($_POST['submit']))
{
$output=shell_exec('sh /somePATH/cgi-bin/script.sh');
echo $output;
}
?>
<form action="" method="post">
<input type="submit" name="submit" value="Call my Shell Script">
</form>
From:https://stackoverflow.com/questions/44443164/execute-a-shell-script-from-html
当然,除了shell_exec,还有exec()、passthru() 和 system(),见:https://www.jb51.net/article/28241.htm
转载请注明来源:https://www.cnblogs.com/Sherlock-L/p/15584456.html
最新文章
- Redola.Rpc 的一个小目标
- JSP学习笔记
- setInterval和clearInterval
- linux kernel的中断子系统 softirq
- linux下使用 Tomcat 的几个坑
- 【转】设计模式 ( 十七) 状态模式State(对象行为型)
- 2搭建Android开发环境
- ACCESS的System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误
- [codility]CountDiv
- string字符串转成16进制
- Unity调试中心
- kafka学习(二)-zookeeper集群搭建
- <;climits>;头文件使用方法
- linux下java窗口,正确显示中文
- Java-UrlRewrite中文api文档
- [模拟赛] T1 无线通讯网
- Python学习笔记 - 切片
- C# CreateParams的使用(解决闪屏问题)
- sourceTree 代码未同步合并
- KeepAlive--高可用解决方案