Linux apache версия php

How To Switch PHP Version on Ubuntu 20.04 LTS

This tutorial provides the steps required to switch among the multiple versions of PHP installed on Ubuntu 20.04 LTS for CLI, Apache Web Server, and NGINX.

The previous tutorial provided the steps required to install multiple PHP versions on Ubuntu 20.04 LTS. You can follow How To Install Multiple Versions Of PHP On Ubuntu 20.04 LTS.

Switch PHP versions — CLI

Verify the currently active PHP as shown below.

# Verify PHP
php --version

# Output
PHP 8.0.13 (cli) (built: Nov 22 2021 09:50:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.13, Copyright (c) Zend Technologies
with Zend OPcache v8.0.13, Copyright (c), by Zend Technologies

We can configure multiple versions of PHP installed on Ubuntu 20.04 LTS using the commands as shown below.

# PHP 7.0 
sudo update-alternatives --set php /usr/bin/php7.0
sudo update-alternatives --set phar /usr/bin/phar7.0
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.0

# PHP 8.0
sudo update-alternatives --set php /usr/bin/php8.0
sudo update-alternatives --set phar /usr/bin/phar8.0
sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.0

Now we can switch among PHP 7.0 or PHP 8.0 using the commands as shown below.

# Switch PHP
sudo update-alternatives --config php

# Output
There are 2 choices for the alternative php (providing /usr/bin/php).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/php8.0 80 auto mode
1 /usr/bin/php7.0 70 manual mode
* 2 /usr/bin/php8.0 80 manual mode

Press to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/php7.0 to provide /usr/bin/php (php) in manual mode

# Switch
sudo update-alternatives --config phar

# Output
There are 2 choices for the alternative phar (providing /usr/bin/phar).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/phar8.0 80 auto mode
1 /usr/bin/phar7.0 70 manual mode
* 2 /usr/bin/phar8.0 80 manual mode

Press to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/phar7.0 to provide /usr/bin/phar (phar) in manual mode

# Switch
sudo update-alternatives --config phar.phar

# Output
There are 2 choices for the alternative phar.phar (providing /usr/bin/phar.phar).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/phar.phar8.0 80 auto mode
1 /usr/bin/phar.phar7.0 70 manual mode
* 2 /usr/bin/phar.phar8.0 80 manual mode

Press to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/phar.phar7.0 to provide /usr/bin/phar.phar (phar.phar) in manual mode

Now, again verify the currently active PHP as shown below.

# Verify PHP
php --version

# Output
PHP 7.0.33-57+ubuntu20.04.1+deb.sury.org+1 (cli) (built: Nov 19 2021 06:39:53) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.33-57+ubuntu20.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

This is helpful for the applications reliable on the PHP in CLI mode to run console based programs. We can switch among the multiple versions of PHP installed on Ubuntu systems.

Читайте также:  Firefox esr linux deb

Switch PHP versions — Apache Web Server

Similar to CLI mode, we can also switch among multiple versions of PHP for the Apache Web Server. In the previous tutorial — How To Install Multiple Versions Of PHP On Ubuntu 20.04 LTS, we saw that the PHP version remains same i.e. PHP 7.0 even after installing the recent version of PHP i.e. PHP 8.0 as shown in Fig 1.

We can switch among the PHP versions for the Apache Web server using the commands as shown below.

# Disable PHP 7.0
sudo a2dismod php7.0

# Output
Module php7.0 disabled.
To activate the new configuration, you need to run:
systemctl restart apache2

# Enable PHP 8.0
sudo a2enmod php8.0

# Output
Considering dependency mpm_prefork for php8.0:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php8.0:
Enabling module php8.0.
To activate the new configuration, you need to run:
systemctl restart apache2

# Restart Apache Web Server
sudo service apache2 restart

Now, again refresh the info.php using the Web Browser. It should be similar to Fig 2.

We can also switch back to PHP 7 using the same commands.

Switch PHP versions — Apache Web Server — PHP FPM

Instead of switch PHP version for Apache Web Server which impacts all the Virtual Hosts, we can also configure the selected Virtual Host to use the PHP version specified by us. We can do so using PHP FPM installed by us as shown in How To Install Multiple Versions Of PHP On Ubuntu 20.04 LTS.

# Check Status - PHP 7.0 FPM
systemctl status php7.0-fpm

# Output
php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2021-12-04 20:35:28 PST; 1h 14min ago Docs: man:php-fpm7.0(8) Main PID: 42546 (php-fpm7.0) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 9432) Memory: 18.1M CGroup: /system.slice/php7.0-fpm.service ├─42546 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf) ├─42548 php-fpm: pool www └─42549 php-fpm: pool www Dec 04 20:35:28 ubuntu systemd[1]: Starting The PHP 7.0 FastCGI Process Manager. Dec 04 20:35:28 ubuntu systemd[1]: Started The PHP 7.0 FastCGI Process Manager.

# Check Status - PHP 8.0 FPM
systemctl status php8.0-fpm

# Output
php8.0-fpm.service - The PHP 8.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2021-12-04 21:15:28 PST; 35min ago Docs: man:php-fpm8.0(8) Process: 61492 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.0/fpm/pool.d/www.co> Main PID: 61489 (php-fpm8.0) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 9432) Memory: 10.7M CGroup: /system.slice/php8.0-fpm.service ├─61489 php-fpm: master process (/etc/php/8.0/fpm/php-fpm.conf) ├─61490 php-fpm: pool www └─61491 php-fpm: pool www Dec 04 21:15:28 ubuntu systemd[1]: Starting The PHP 8.0 FastCGI Process Manager. Dec 04 21:15:28 ubuntu systemd[1]: Started The PHP 8.0 FastCGI Process Manager.

We can see that both PHP 7.0 FPM and PHP 8.0 FPM are in running state. Now, enable Apache2 to use multiple versions of PHP using the command as shown below.

# Install FCGID
sudo apt install libapache2-mod-fcgid

# Enable FCGID
sudo a2enmod actions fcgid alias proxy_fcgi

# Restart Apache
sudo service apache2 restart

Also, update the Virtual Host as shown below.


----
----


# For Apache version 2.4.10 and above
SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost"


----
----

Also, reload Apache as shown below.

# Reload Apache
sudo service apache2 reload

Now, check the output of info.php using the Browser. It should show the configurations specific to PHP 7. Similarly, we can change PHP 7 to PHP 8 for the selected virtual host, without impacting the other virtual hosts.

Читайте также:  Swap файловая система linux

Switch PHP versions — NGINX

Switching PHP version for NGINX is straight-forward, since it uses PHP FPM to execute the PHP scripts. We can simply specify the PHP version while configuring the Server Block as shown below. You can also follow How To Install PHP For Nginx On Ubuntu 20.04 LTS for more details.

# Server Block with PHP FPM
sudo nano /etc/nginx/sites-available/example.com

server <

.
.

# pass the PHP scripts to FastCGI
location ~ \.php$ root /var/www/example.com/html;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
>

.
.
>

Summary

This tutorial provided the steps required to switch among the multiple PHP versions installed on Ubuntu 20.04 LTS for CLI, Apache Web Server, and NGINX.

Источник

Как указать apache какую версию php использовать?

ОС Freebsd
Установил второй версией php 70 на сервер, но не могу найти теперь где указать apache какую версию PHP использовать.
И еще вопрос, как можно для отдельного хоста указать определенную версию php?

Оценить 1 комментарий

Просто удаляете не нужную версию php и ставите ту что надо, если конечно она есть в портах или репозиториях.

kpa6uu

sudo a2dismod php5.6 // отключаем старую версию пыхи в апаче sudo a2enmod php7.1 // включаем новую версию пыхи в апаче sudo update-alternatives --set php /usr/bin/php7.1 // выставляем версию cli sudo service apache2 restart

Vincent1

kpa6uu

Vincent1: вот же ёкарный бабай. Ну тогда я бы посмотрел что эти команды выполняют и накалякал баш скрипты для их замены. Сочная у вас ава, даже приятно советы давать))

kpa6uu

Vincent1

Jakim

cd /etc/nginx/sites-available/ sudo nano your_domain.com location ~ \.php$ < fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; # какую версию читать для сайта your_domain.com выбрать можно любую с установленых на серваке >
sudo apt install php7.2 php7.2-fpm # установили php версию нужную для определенного домена ### sudo a2enmod actions fastcgi alias proxy_fcgi # Ubuntu старее 18.04 sudo a2enmod actions fcgid alias proxy_fcgi # Ubuntu 18.04 ### sudo nano /etc/apache2/sites-available/your_domain.com ServerName your_domain.com DocumentRoot /var/www/your_domain_folder Options -Indexes +FollowSymLinks +MultiViews AllowOverride All Require all granted SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/" # версия php которая нужна на этом домене 

Годная инструкция. Для PHP5.6 потребовалось установить несколько модулей. В эпоху PHP7.x некоторые модули для старого PHP не поставляются по умолчанию. Также fcgid в десктопной ubuntu 18.04 не поставляется из коробки.

Читайте также:  Set oracle home on linux

Источник

How to Check PHP Version (Apache/Nginx/CLI)

PHP (Hypertext Preprocessor) is a popular programming language generally used for web development. It was originally developed by the Danish-Canadian programmer Rasmus Lerdorf in the year 1994. It can be used either via the command line or used with popular web servers like Apache or Nginx. Apache has a PHP module to directly run PHP files but Nginx required a PHP-FPM package to work with PHP files.

This tutorial will help you to check the PHP version installed on your system. Also, what is the active version of PHP on the command line as well as the check PHP version used by Apache or Nginx? As we can install multiple PHP versions on a single system, it can be the there are different version’s active on CLI and Web servers.

Check PHP Version (CLI)

By default, there can be one default PHP version that can use with standard binary. To check the PHP version currently active on the command line, execute the following command on the terminal:

Output
PHP 7.4.8 (cli) (built: Jul 13 2020 16:45:28) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.8, Copyright (c), by Zend Technologies

On Ubuntu and Debian systems, you can also access another version by pointing their versions like: php7.2 or php7.3 etc.

Check PHP Version (Apache/Nginx)

This is fine that, the current active PHP version on the command line is different than the version used by the Apache or Nginx web servers. It’s also possible that two virtual hosts can use different-2 PHP versions by using PHP-FPM.

So, to check the currently active version for Apache or Nginx, navigate to the document root of the website to which you want to check the PHP version. For example, my document root is “/var/www/html”:

Then create and edit a PHP script file in your favorite text editor:

And, add the following content to the files. Here we used built-in phpinfo() function, which prints the active PHP version details:

Источник

Оцените статью
Adblock
detector