Skip to main content

Featured

Install Apache 2 on Ubuntu 24.04

Apache2 is one of the most popular and reliable open-source web servers. If you’re running Ubuntu 24.04 and want to host websites or applications, Apache2 is an excellent choice. In this guide, you’ll learn how to install and configure Apache2 step by step.

Install PHP-FPM (PHP 8.1) with Nginx on Ubuntu 24.04

When using Nginx as your web server, PHP is usually executed through PHP-FPM (FastCGI Process Manager) instead of being loaded directly into the server like with Apache. This provides better performance, scalability, and security for modern web applications. In this tutorial, you’ll learn how to install and configure PHP-FPM 8.1 with Nginx on Ubuntu 24.04.

Prerequisite: Nginx Installation

Before installing PHP-FPM, make sure you have already installed and configured Nginx on Ubuntu 24.04. You can follow the installation guide here: Install Nginx on Ubuntu 24.04.

Add the PHP Repository

To install PHP 8.1, you need to add the ondrej/php repository and update your package list:

sudo add-apt-repository ppa:ondrej/php
sudo apt update

Install PHP-FPM 8.1 and Extensions

Next, install PHP-FPM 8.1 along with the most commonly used extensions:

sudo apt install -y php8.1-fpm php8.1-mcrypt php8.1-gd php8.1-curl php8.1-mysql php8.1-zip php8.1-xml php8.1-soap php8.1-intl php8.1-mbstring php8.1-bcmath

This will install PHP-FPM and essential modules for database support, encryption, XML, SOAP, string handling, and more.

Check Your Linux User

Before editing the PHP-FPM configuration, make sure you know your Linux username. You can check it by running the following command in your terminal:

echo $USER

This will print your current Linux user, which you can then use in the www.conf configuration file.

Configure PHP-FPM Pool

Each PHP-FPM process pool has its own configuration file. Open the www.conf file:

sudo nano /etc/php/8.1/fpm/pool.d/www.conf

Change the user and group to match your Linux username (example: gerson):

user = gerson
group = gerson
listen.owner = gerson
listen.group = gerson

This ensures PHP-FPM processes run under your user account instead of the default www-data.

Configure Nginx for PHP-FPM

To make Nginx work with PHP-FPM, you need to edit the default server block configuration. Open the following file:

sudo nano /etc/nginx/sites-enabled/default

Inside the file, locate the PHP section and make sure it looks like this (uncomment the required lines):

location ~ \.php$ {
        include snippets/fastcgi-php.conf; # uncomment this line
#
#       # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php8.1-fpm.sock; # uncomment this line
#       # With php-cgi (or other tcp sockets):
#       fastcgi_pass 127.0.0.1:9000;
}

This configuration tells Nginx to pass PHP requests to PHP-FPM through the Unix socket at /run/php/php8.1-fpm.sock.

Adjust PHP Settings

Edit the PHP configuration file to optimize performance and error reporting:

sudo nano /etc/php/8.1/fpm/php.ini

Add or modify the following lines:

memory_limit = 2048M
max_input_vars = 10000
max_execution_time = 3600
max_input_time = 3600
display_errors = On
error_reporting = E_ALL

Restart PHP-FPM

After making these changes, restart PHP-FPM so the new configuration is applied:

sudo service php8.1-fpm restart

Conclusion

You have successfully installed and configured PHP-FPM 8.1 with Nginx on Ubuntu 24.04. With this setup, your server is ready to run dynamic web applications and frameworks like WordPress, Laravel, or Symfony with improved performance and security. By customizing the www.conf and php.ini files, you ensured that PHP-FPM runs under your user and is optimized for development and production environments.

Comments

Popular Posts