Logo

How to Install PHP 8.3 for Nginx on Ubuntu 24.04

Mar 16, 2024

To install and configure PHP 8.3 with Nginx on Ubuntu 24.04, proceed with the following steps.

Step 1 : Begin by updating your package index to ensure you have the latest version of available packages.

sudo apt update

Step 2 : You can search for available PHP versions using the following command:

apt-cache search php8.3

Step 3 : Install PHP 8.3 core packages including CLI and FPM:

sudo apt install php8.3 php8.3-cli php8.3-fpm

Step 4 : Install frequently used PHP extensions like MySQL, cURL, XSL, GD, etc., using:

sudo apt install php8.3-{mysql,curl,xsl,gd,common,xml,zip,xsl,soap,bcmath,mbstring,gettext,imagick}

Explanation of Extensions:

- mysql: Enables PHP to communicate with MySQL databases.

- curl: Allows making HTTP requests from within PHP scripts.

- xsl: Facilitates interaction with XML documents using XSLT.

- gd: Provides image manipulation capabilities to create, modify, and output image files.

- common: Includes shared files essential for various PHP extensions.

- xml: Enables parsing and generation of XML documents.

- zip: Allows creation, reading, and modification of zip archives.

- soap: Facilitates communication with SOAP-based web services.

- bcmath: Provides arbitrary-precision arithmetic functions.

- mbstring: Supports multibyte character encodings.

- gettext: Facilitates localization and translation of web applications.

- imagick: Allows manipulation of images using the ImageMagick library.

- Verify PHP version

Step 5 : Ensure PHP 8.3 is installed correctly by checking its version:

php -v

Step 6 : Edit your Nginx configuration file for your site (e.g., example.com) to include PHP processing directives.

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

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/html/example.com;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    error_log  /var/log/nginx/example.com_error.log;
    access_log /var/log/nginx/example.com_access.log;
}

Step 7 : Enable your Nginx site configuration file by creating a symbolic link from the sites-available directory to the sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Step 8 : Check the syntax of your Nginx configuration to ensure there are no errors:

sudo nginx -t

Step 9 : Restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 10 : Create a PHP file (e.g., info.php) in your web root directory

sudo nano /var/www/html/example.com/info.php

Add the following line:

<?php
phpinfo();
?>

Step 11 : Navigate to your domain (e.g., http://example.com/info.php) in a web browser to see the PHP information page.

Congratulations! You have successfully installed PHP 8.3 for Nginx on Ubuntu 24.04.

Recommended