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

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


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"fiif [[ `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" 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.3You 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 nginxecho $server_versionnginx# On server with neither nginx nor apacheecho $server_versionunknown