简体   繁体   English

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

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

I have a Laravel project deployed on Ubuntu VM.我在 Ubuntu VM 上部署了一个 Laravel 项目。 I have a script that I am working on right now that to know if the current VM deployed using nginx or Apache programmatically.我有一个我现在正在处理的脚本,用于了解当前的 VM 是使用 nginx 还是 Apache 以编程方式部署的。

I know I can just check using these ps and grep command I will find that out我知道我可以使用这些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:~# 

With those result about, I know that this VM is using Apache.有了这些结果,我知道这个 VM 正在使用 Apache。

But, I have no idea how to check it via a Bash script.但是,我不知道如何通过 Bash 脚本检查它。 How would one go about and do that?一个人会怎么做呢? I'm open to any suggestions at this moment.我现在愿意接受任何建议。

在此处输入图片说明

Since you are trying to achieve this with grep and ps , you could do something like this:由于您正在尝试使用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 command can tell you what process is listening on a port. ss命令可以告诉您正在侦听端口的进程。

For example ss -tlnp | grep -E ":80\\b"例如ss -tlnp | grep -E ":80\\b" ss -tlnp | grep -E ":80\\b" tells you what process is listening on tcp port 80. You can see it's apache or nginx. ss -tlnp | grep -E ":80\\b"告诉你什么进程正在监听 tcp 端口 80。你可以看到它是 apache 或 nginx。

  1. You could curl against localhost and grep the headers您可以针对 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

Or just run pgrep或者直接运行 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

As curl does not always retrieve the type of webserver, you may use the following code locally on the server :由于 curl 并不总是检索网络服务器的类型,您可以在服务器本地使用以下代码:

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

Don't do the break statement if you have many values (gitlab is using nginx).如果您有很多值(gitlab 使用的是 nginx),请不要执行 break 语句。

If you have netstat on the server (I think you can use /proc informations instead) :如果您在服务器上有 netstat(我认为您可以使用 /proc 信息代替):

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

For the port 80 or 443 for https.对于 https 的端口 80 或 443。

Best regards此致

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

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