繁体   English   中英

如何使用 bash 脚本检查当前的 Web 服务器是 NGINX 还是 Apache?

[英]How can I check if current web server is NGINX or Apache using bash script?

我在 Ubuntu VM 上部署了一个 Laravel 项目。 我有一个我现在正在处理的脚本,用于了解当前的 VM 是使用 nginx 还是 Apache 以编程方式部署的。

我知道我可以使用这些psgrep命令来检查我会发现

root@theawsbiz:~# ps -aux | grep apache                                                                                                                 
root      3446  0.0  1.8 544540 37144 ?        Ss   17:02   0:00 /usr/sbin/apache2 -k start                                                             
www-data  3449  0.1  1.9 550388 39796 ?        S    17:02   0:00 /usr/sbin/apache2 -k start                                                             
www-data  3454  0.0  1.0 547336 21532 ?        S    17:02   0:00 /usr/sbin/apache2 -k start                                                             
www-data  3457  0.0  1.8 548196 37864 ?        S    17:02   0:00 /usr/sbin/apache2 -k start                                                             
www-data  3458  0.0  1.0 547320 21428 ?        S    17:02   0:00 /usr/sbin/apache2 -k start                                                             
www-data  3462  0.0  1.7 550008 36264 ?        S    17:02   0:00 /usr/sbin/apache2 -k start                                                             
www-data  3465  0.0  1.8 550408 38160 ?        S    17:02   0:00 /usr/sbin/apache2 -k start                                                             
www-data  3466  0.0  1.9 550400 40512 ?        S    17:02   0:00 /usr/sbin/apache2 -k start                                                             
www-data  3467  0.0  1.0 547320 21416 ?        S    17:02   0:00 /usr/sbin/apache2 -k start                                                             
www-data  3468  0.0  1.7 548228 36236 ?        S    17:02   0:00 /usr/sbin/apache2 -k start                                                             
www-data  3520  0.0  0.9 546872 19964 ?        S    17:06   0:00 /usr/sbin/apache2 -k start                                                             
root      3526  0.0  0.0  14856  1036 pts/1    S+   17:06   0:00 grep --color=auto apache                                                               
root@theawsbiz:~# ps -aux | grep nginx                                                                                                                  
root      3529  0.0  0.0  14856  1092 pts/1    S+   17:06   0:00 grep --color=auto nginx                                                                
root@theawsbiz:~# 

有了这些结果,我知道这个 VM 正在使用 Apache。

但是,我不知道如何通过 Bash 脚本检查它。 一个人会怎么做呢? 我现在愿意接受任何建议。

在此处输入图片说明

由于您正在尝试使用grepps来实现这一点,因此您可以执行以下操作:

if [[ `ps -acx|grep apache|wc -l` > 0 ]]; then
    echo "VM Configured with Apache"
fi
if [[ `ps -acx|grep nginx|wc -l` > 0 ]]; then
    echo "VM Configured with Nginx"
fi

ss命令可以告诉您正在侦听端口的进程。

例如ss -tlnp | grep -E ":80\\b" ss -tlnp | grep -E ":80\\b"告诉你什么进程正在监听 tcp 端口 80。你可以看到它是 apache 或 nginx。

  1. 您可以针对 localhost 卷曲并 grep 标头
$ curl -v api.company.co.ke 2>&1 |grep -i server | awk -F: '{print $2}'

nginx/1.10.3

You can run the command in a subshell and get the output

 ❯ get_server_version=$(curl -v api.company.co.ke 2>&1 |grep -i server | awk -F: '{print $2}') 
 ❯ echo $get_server_version                                                                            
 nginx/1.10.3

或者直接运行 pgrep

 ❯ { pgrep nginx && server_version="nginx"; } || { pgrep apache  && server_version="apache"; } || server_version="unknown"

# On server running nginx
 ❯ echo $server_version
nginx


# On server with neither nginx nor apache
 ❯ echo $server_version
unknown

由于 curl 并不总是检索网络服务器的类型,您可以在服务器本地使用以下代码:

for webservice in gitlab apache nginx lighttpd; do 
    if `ps aux|grep -v grep|grep -q $webservice`; then 
        echo $webservice; 
        break; 
    fi;
done

如果您有很多值(gitlab 使用的是 nginx),请不要执行 break 语句。

如果您在服务器上有 netstat(我认为您可以使用 /proc 信息代替):

netstat -naptu |grep :80 |awk '{print $NF}'|cut -d"/" -f2|uniq

对于 https 的端口 80 或 443。

此致

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM