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仍然報上述錯誤。

參考:https://stackoverflow.com/questions/54412872/apache-httpd-build-from-source-fatal-error-expat-h-no-such-file-or-directory
忽然來了靈感:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM