What are the benefits of PHP 8.1 and the Commands to upgrade from an older version on Nginx?
Tags: php, ddev, drupal
Published: 2022-12-31 18:57:12

---
title: What are the benefits of PHP 8.1 and the Commands to upgrade from an older version on Nginx?
published: true
date: 2022-12-30 19:08:11 UTC
tags: PHP,DDEV,Drupal,php
canonical_url: https://davidloor.com/2022/12/30/benefits-php-8-1-commands-upgrade-nginx/
---

![php 8.1](https://i0.wp.com/davidloor.com/wp-content/uploads/2022/12/php_8_1_released.png?resize=800%2C500&ssl=1)

## PHP 8.1 new features and improvements

PHP 8.1 is the latest version of the popular server-side scripting language, and it brings a host of new features and improvements that make it a compelling choice for developers. In this article, we’ll highlight some of the key benefits of upgrading to PHP 8.1.

First and foremost, PHP 8.1 introduces a number of performance enhancements that make it faster and more efficient than previous versions. This is achieved through various optimizations, such as the JIT (Just-In-Time) compiler, which converts code into machine-readable instructions at runtime, and the union types feature, which allows for more efficient type checking. These and other improvements result in significant performance gains, which can be especially beneficial for high-traffic websites and applications.

Another benefit of PHP 8.1 is its improved error handling and debugging capabilities. The new throwable errors feature allows developers to catch and handle errors more effectively, while the improved type system makes it easier to catch and fix issues early on in the development process. Additionally, the new “match” expression feature allows for more concise and expressive code, which can make it easier to debug and maintain.

PHP 8.1 also introduces a number of new language features that make it more powerful and flexible. The union types feature mentioned earlier allows developers to specify multiple possible types for a variable, which can be especially useful when working with external libraries or APIs. The new “constructor property promotion” feature allows developers to simplify their code by eliminating the need to write redundant getters and setters, while the new “attributes” feature allows developers to attach metadata to class members, functions, and other language constructs.

## Commands to upgrade to PHP 8.1 on a Ngnix server on Ubuntu 

For purpose of this guide, we are using Ubuntu

```
sudo apt update

sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y

sudo add-apt-repository ppa:ondrej/php

sudo apt install php8.1
sudo apt install php8.1-{imagick,bz2,curl,intl,mysql,readline,xml,fpm,mbstring,zip,bcmath}
```

### Update the Nginx server blocks

Edit the server block for the domains that you want to start using the PHP 8.1 version. I use **nano** as my text editor, but you can use vim, vi, or any other text editor of your preference.

nano /etc/nginx/sites-available/yoursiteone.com

nano /etc/nginx/sites-available/yoursitetwo.com

You should find a line that looks like this one: `fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;` and replace the number of the current version of PHP, in my case 7.4, with 8.1.

![](https://i0.wp.com/davidloor.com/wp-content/uploads/2022/12/Screenshot-2022-12-30-at-12.47.50-PM.png?resize=800%2C328&ssl=1)
_Nginx block example to update to PHP 8.1_

After you restart the Nginx server with: `service nginx restart`, your site(s) will start using PHP 8.1

You can learn more about server blocks in Nginx at:

[https://www.nginx.com/resources/wiki/start/topics/examples/server\_blocks/](https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/)

[https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04](https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04)

If you are using DDEV for your local environment, you can use the following command to upgrade to PHP 8.1`: ddev config --php-version 8.1` You can find more information about DDEV in this [link](https://dev.to/davo20019/how-to-setup-a-wordpress-or-drupal-site-locally-with-ddev-4035-temp-slug-181423).

In summary, PHP 8.1 offers a number of benefits that make it an appealing choice for developers. Its performance enhancements and improved error-handling capabilities make it faster and more reliable, while its new language features make it more powerful and flexible. If you’re currently using an older version of PHP, upgrading to PHP 8.1 is highly recommended.