What are the benefits of PHP 8.1 and the Commands to upgrade from an older version on Nginx?
David Loor
Posted on December 31, 2022
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.
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/
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.
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.
Posted on December 31, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.