简体   繁体   中英

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

I have a Laravel project deployed on Ubuntu VM. I have a script that I am working on right now that to know if the current VM deployed using nginx or Apache programmatically.

I know I can just check using these ps and grep command I will find that out

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.

But, I have no idea how to check it via a Bash script. 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:

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.

For example 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.

  1. You could curl against localhost and grep the headers
$ 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 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 :

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).

If you have netstat on the server (I think you can use /proc informations instead) :

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

For the port 80 or 443 for https.

Best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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