Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

like image 899
code-8 Avatar asked Mar 11 '19 17:03

code-8


People also ask

How do I know if my server is Nginx or Apache?

How to Check If You're Running Nginx or Apache. On most websites, you can simply check the server HTTP header to see if it says Nginx or Apache. You can see HTTP headers by launching the network tab in Chrome Devtools. Or you can check headers in a tool like Pingdom or GTmetrix.

How do I know if my server is Apache?

#1 Checking the Apache Version Using WebHost ManagerFind the Server Status section and click Apache Status. You can start typing “apache” in the search menu to quickly narrow your selection. The current version of Apache appears next to the server version on the Apache status page. In this case, it is version 2.4.

How can I tell what webserver is on Linux?

If your webserver runs on standard port see "netstat -tulpen |grep 80". It should tell you which service is running. Now you can check the configs, you'll find them normally in /etc/servicename, for example: apache configs are likely to find in /etc/apache2/. There you'll get hints where the files are located.

How do I know if Nginx is running on Linux?

Through a simple command you can verify the status of the Nginx configuration file: $ sudo systemctl config nginx The output will show if the configuration file is correct or, if it is not, it will show the file and the line where the problem is.


2 Answers

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
like image 94
Sonny Avatar answered Oct 14 '22 09:10

Sonny


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.

like image 33
Bob Johnson Avatar answered Oct 14 '22 09:10

Bob Johnson