Nginx set php version

Setting the CLI PHP Version

Out of the box, our default CLI PHP version is set to PHP 7.4. In the near future, this will be updated to be 8.0 out of the box.

This change will take place automatically, and so will future updates. For most GridPane users, you can leave our defaults and stop reading right now.

For a few of you though, you may come across a situation where you need to change our defaults to a higher or lower version of PHP, and this article will walk you through how to make this change and set it so that our systems don’t automatically update it in the future.

Table of Contents

CLI PHP vs Website PHP

Before we begin, it’s important to understand the CLI PHP version, and the version of PHP that your individual websites run on is not the same thing. Updating your website’s PHP version will not solve any issues that arise from scripts/software that does not support your current CLI version of PHP.

What is PHP CLI?

PHP CLI is PHP’s Command Line Interface, and it allows PHP to be executed from the server command line.

A Real World Example: Composer

Most programs have a minimum PHP requirement. Back in the days when 7.2 was our default CLI PHP version, Composer made an update that required a minimum of PHP 7.3.

This caused problems not just with Composer but other programs as well – here’s an example of an error from a site where SSO was failing to generate a login link:

Your Composer dependencies require a PHP version ">= 7.3.0". You are running 7.2.25-1+ubuntu18.04.1+deb.sury.org+1. Error Code: 100 | PHP Errors stopping SSO Link creation, please check logs.

Here Composer is using the CLI PHP version and not the FPM/LSPHP version that is used by your websites. Switching the website PHP version of your sites would, therefore, not fix this issue.

Updating the CLI PHP version to 7.3 fixed the above SSO errors and allowed Composer to once again function correctly.

Getting Started

To make this change, you will first need to SSH into your server. Once connected, the rest is simply a copy-and-paste exercise following our steps below.

Читайте также:  Php text align right

Please see the following guides to get started:

Checking Your CLI PHP Version

To check your CLI PHP version, you can run the following command:

sudo update-alternatives --display php

This will output the following, only with the correct PHP version should your setting be different:

root@servername:~# sudo update-alternatives --display php
php - manual mode
link best version is /usr/bin/php8.1
link currently points to /usr/bin/php7.4
link php is /usr/bin/php
slave php.1.gz is /usr/share/man/man1/php.1.gz
/usr/bin/php7.3 - priority 73
slave php.1.gz: /usr/share/man/man1/php7.3.1.gz
/usr/bin/php7.4 - priority 74
slave php.1.gz: /usr/share/man/man1/php7.4.1.gz
/usr/bin/php8.0 - priority 80
slave php.1.gz: /usr/share/man/man1/php8.0.1.gz
/usr/bin/php8.1 - priority 81
slave php.1.gz: /usr/share/man/man1/php8.1.1.gz
root@servername:~#

Changing Your CLI PHP Version on Nginx or OpenLiteSpeed

Nginx and OpenLiteSpeed Update

As of November 8th 2022, setting the PHP CLI version on both Nginx and OpenLiteSpeed has been brought to parity so that the same commands work on both server stacks.

Step 1. Set Your Preferred CLI PHP Version

sudo update-alternatives --set php /usr/bin/phpX.X
sudo update-alternatives --set php /usr/bin/php7.4

Step 2. Make the Change Persist

Our systems check for a token file for the CLI PHP version. If it’s not present, your changes will be reverted. Create the token file with:

/root/preferred_php_cli.version

Now save the file with CTRL+O followed by Enter. Exit nano with CTRL+X.

Search the Knowledge Base

New to GridPane?

Compare Us

Learn

To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Читайте также:  Python socket asyncio server

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

Обновление версии PHP на NGINX PHP-FPM

Версия PHP 7.4 содержит существенное количество обновлений и настоятельно рекомендуется к использованию. Для начала от имени суперпользователя добавляем репозитарий и обновляем списки пакетов:

$ apt-add-repository ppa:ondrej/php $ apt-add-repository ppa:ondrej/nginx $ apt update

Устанавливаем новые пакеты PHP и PHP-FPM:

$ apt install -y php7.4 php7.4-cli php7.4-common php7.4-fpm

Проверяем актуальную установленную версию PHP:

$ php -v PHP 7.4.22 (cli) (built: Jul 30 2021 13:07:59) ( NTS )

(Опционально) Устанавливаем дополнительные библиотеки и модули для WordPress:

$ apt install -y php7.4-mysql php7.4-mysqli php7.4-dom php7.4-simplexml php7.4-ssh2 php7.4-xml php7.4-xmlreader php7.4-curl php7.4-exif php7.4-ftp php7.4-gd php7.4-iconv php7.4-imagick php7.4-json php7.4-mbstring php7.4-posix php7.4-sockets php7.4-tokenize php7.4-pdo php7.4-sqlite3 php7.4-ctype php7.4-fileinfo php7.4-zip php7.4-exif

(Опционально) Вносим изменения в /etc/php/7.4/fpm/php.ini — раскомментируем параметр cgi.fix_pathinfo и устанавливаем его равным 0.

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's ; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok ; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting ; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting ; of zero causes PHP to behave as before. Default is 1. You should fix your scripts ; to use SCRIPT_FILENAME rather than PATH_TRANSLATED. ; http://php.net/cgi.fix-pathinfo cgi.fix_pathinfo=0

Перезапускаем сервис PHP-FPM:

$ systemctl restart php7.4-fpm.service

Проверяем, что появился новый сокет:

$ ls -1 /run/php/ php-fpm.sock php7.2-fpm.pid php7.2-fpm.sock php7.4-fpm.pid php7.4-fpm.sock

Теперь осталось только в конфиге NGINX поменять версию PHP-FPM (указываем новый сокет):

$ nano /etc/nginx/sites-available/default

Тестируем конфиг NGINX и перезапускаем его:

$ nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful $ systemctl reload nginx.service

Источник

How to Change PHP Version

Sometimes it is necessary to change the PHP version used by one or many web applications on a server. This could be due to a whole myriad of reasons, though it is likely because either the code in the app is too old for newer versions of PHP, or it features utilities not available in older PHP versions.

Читайте также:  Ползунок диапазон на javascript

In this tutorial, we will learn how to change PHP versions and configure them to be used on different web applications.

Check Which Version Your Site is Using

You are going to need to know what version of PHP your site is currently using and validate it has changed. Please read about how to do this here.

List PHP Versions Available

Now let’s list the versions of PHP available on your system. We can do this using the update-alternatives command-line utility with the —list flag. It will return a list of paths to the PHP versions on your system.

sudo update-alternatives --list php 
/usr/bin/php5.6 /usr/bin/php7.0 /usr/bin/php7.1 /usr/bin/php7.2 /usr/bin/php7.3 /usr/bin/php7.4 /usr/bin/php8.0 

Set Default PHP Version

To set the default PHP version your system use the following update-alternatives command, passing the PHP version you wish to use at the end.

sudo update-alternatives --set php /usr/bin/php7.2 

If it works, the above command should return a message stating the new default PHP version:

update-alternatives: using /usr/bin/php7.2 to provide /usr/bin/php (php) in manual mode 

To confirm this run php -v :

PHP 7.2.34-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Oct 6 2020 12:01:28) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.34-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies 

Enable a PHP Version in Apache

To enable a new version of PHP in Apache, use the a2enmod utility in sudo mode, passing the name of the PHP version as the first argument.

Then restart the server to complete:

sudo service apache2 restart 

Setting PHP Version For a Single Site in Apache

To set a PHP version for a specific website in Apache, open the .htaccess file in its web root and paste the following line, changing php72 to the version you wish to use:

AddHandler application/x-httpd-php72 .php 

Change the PHP Version for a Site in Nginx

The PHP version for a site in Nginx can be changed in its configuration file. If you don’t know the name of the applications configuration file list them using the following command:

ls /etc/nginx/sites-available 

Now open the configuration file using nano in sudo mode, replacing skillsugar.com with the name of your configuration file:

sudo nano /etc/nginx/sites-available/skillsugar.com 

In the file, there should be a block of code that looks like this:

location ~ \.php$  include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; > 

Change PHP version number to the desired one and save the file.

Now test Nginx for an configuration errors:

If no errors are reported, restart Nginx to apply the changes using the following command:

sudo service nginx restart 

Conclusion

You now know how to change the default PHP version for your system and how to apply a particular version to specific sites on both Nginx and Apache HTTP servers.

Источник

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