Check to see if NGINX is installed on UBUNTU

7

4

Is there any or command to check whether NGINX is already installed on UBUNTU Linux using a bash command/script?

I was trying something like this

echo "BEGINNING INSTALLATION OF NGINX WEB SERVER"
echo
echo
echo "CHECKING TO SEE IF NGINX IS ALREADY INSTALLED"
service nginx > temp.install 2> temperr.install
echo 111
grep -c unrecognized temperr.install > temp2.install
echo 222
status = `cat temp2.install`
echo "NGINX STATUS $status" 

Am new to bash scripting and hence not sure if this is teh best possible way to approach this. I need to write a script that checks if NGINX is already installed or not. If it is not installed it simply installs NGINX otherwise it first deletes NGINX and then re-installs it.

Vikram Chakrabarty

Posted 2013-09-18T08:30:11.107

Reputation:

5How about dpkg -l | grep nginx – None – 2013-09-18T08:32:20.577

I tried this but it just returns back to the bash prompt without giving me any output. – None – 2013-09-18T08:34:55.283

3@op Check return status with echo $? right after issuing the dpkg command. – None – 2013-09-18T08:35:48.417

4Well... did not check return status but I just modified your command a bit and it works for me. This is what I did dpkg -l | grep -c nginx . If nginx is installed it returns the count of the nginx packages and if it is not installed it simply returns 0 – None – 2013-09-18T08:42:56.500

Answers

10

if ! which nginx > /dev/null 2>&1; then
    echo "Nginx not installed"
fi

or

if [ ! -x /usr/sbin/nginx ]; then
    echo "Nginx not installed"
fi

or if you want to be Debian/Ubuntu specific:

if ! dpkg -l nginx | egrep 'îi.*nginx' > /dev/null 2>&1; then
    echo "Nginx not installed"
fi

if you're into the whole brevity thing:

! test -x /usr/sbin/nginx && echo "Nginx not installed"

Nanzikambe

Posted 2013-09-18T08:30:11.107

Reputation: 627

Just the little caveat that this only finds if nginx is installed the standard way. not if it is compiled from source and installed in for instance /usr/local/bin – Lenne – 2016-07-01T20:11:04.093

The last answer is awesome :) – Faris Rayhan – 2017-09-09T20:23:42.067

1

try this:

command -v nginx

install if not installed:

command -v nginx || sudo apt install nginx

dima.rus

Posted 2013-09-18T08:30:11.107

Reputation: 111

command is not found in cron job, I am not able to find full path for command, ie using which command – Ramratan Gupta – 2020-01-03T13:19:23.690

0

I build below solution, which run also in cron job

ISNGINX_INSTALLED=`/bin/ls /usr/sbin/nginx`
if [[ ! -z $ISNGINX_INSTALLED ]]; then
    echo "NOT installed"
else
    echo "installed"
fi

Ramratan Gupta

Posted 2013-09-18T08:30:11.107

Reputation: 101