Chris Mellor
Chris Mellor

Reputation: 377

Getting PHP Version for Currently Running Version

I have just built 3 different versions of PHP from source on an Ubuntu server (alongside NGINX and MySQL 5.7). I am looking for a way to run php --ini for the currently running version. I know I have to add the location to the file PATH in .bashrc so I don't have to add the full path.

I have added this to my .bashrc which allows me to get the currently running PHP version, which then allows me to run the command:

# parallels@ubuntu:~$ ps aux | grep php
# root      6948  0.0  0.2 153724  4620 ?        Ss   16:48   0:00 php-fpm: master process (/opt/php-7.0.0/etc/php-fpm.conf)

PHP_VERSION=$(ps aux | grep -o php-[[:digit:]].[[:digit:]].[[:digit:]])
export PATH="/bin:/usr/bin:/opt/$PHP_VERSION/bin:/sbin"

It works, but I am a bash novice and I'm thinking their might be a different way to do it. Would I be correct?

Upvotes: 4

Views: 8686

Answers (4)

a55
a55

Reputation: 565

This command works while running in PHP

<?php
    echo PHP_VERSION;

You can get it in bash, like

PHP_VERSION=$(php -r "echo PHP_VERSION;")

Here is all of PHP Predefined Constants

Upvotes: 9

rlemon
rlemon

Reputation: 17667

should be able to get it done with awk.

php -v | awk 'NR<=1{ print $2 }'

print the second column from the first row of input.

Upvotes: 2

Nicolas Beauvais
Nicolas Beauvais

Reputation: 1052

I got it to work with the following commands:

# Full version
php -v | head -n 1 | cut -d " " -f 2

# Major.Minor version
php -v | head -n 1 | cut -d " " -f 2 | cut -f1-2 -d"."

Upvotes: 8

davidcondrey
davidcondrey

Reputation: 36043

PHP_VERSION=$(php -v | tail -r | tail -n 1 | cut -d " " -f 2 | cut -c 1-3)
cd /usr/local/etc/php/$PHP_VERSION/
# cd /usr/local/etc/php/7.1/

Upvotes: 8

Related Questions