Step-by-Step Guide to Installing PHP 8.4 on Ubuntu 24.04 with Nginx & MySQL

by Admin
11 minutos
Step-by-Step Guide to Installing PHP 8.4 on Ubuntu 24.04 with Nginx & MySQL

To install PHP 8.4 on a Linux, Nginx, and MySQL setup on an Ubuntu 24.04 Server, follow these steps:


1. Update and Upgrade Your System

Ensure your system is up to date before proceeding:

sudo apt update && sudo apt upgrade -y

2. Add the Ondřej PHP Repository

Ubuntu typically does not include the latest PHP versions in its default repositories. Use the Ondřej Surý repository, which provides the latest PHP packages.

  1. Install required dependencies:

    sudo apt install software-properties-common -y
    
  2. Add the Ondřej PHP PPA:

    sudo add-apt-repository ppa:ondrej/php
    
  3. Update your package list:

    sudo apt update
    

3. Install PHP 8.4

  1. Install PHP 8.4 along with commonly used extensions:

    sudo apt install php8.4 php8.4-fpm php8.4-cli php8.4-mysql php8.4-curl php8.4-xml php8.4-mbstring php8.4-zip php8.4-bcmath php8.4-soap php8.4-intl php8.4-readline -y
    
  2. Verify the installation:

    php -v
    

    You should see output showing PHP 8.4.x as the installed version.


4. Configure PHP-FPM for Nginx

  1. Ensure PHP-FPM is running:

    sudo systemctl status php8.4-fpm
    

    If not active, start it:

    sudo systemctl start php8.4-fpm
    
  2. Configure Nginx to use PHP-FPM. Open your Nginx site configuration file:

    sudo nano /etc/nginx/sites-available/your-site.conf
    
  3. Add the following block (or modify if already present):

    server {
        listen 80;
        server_name your-domain.com;
        root /var/www/your-site;
    
        index index.php index.html;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    
  4. Test the Nginx configuration:

    sudo nginx -t
    
  5. Reload Nginx to apply changes:

    sudo systemctl reload nginx
    

5. Test PHP Functionality

  1. Create a PHP info file to test:

    echo "<?php phpinfo();" | sudo tee /var/www/your-site/info.php
    
  2. Access http://your-domain.com/info.php in your browser. You should see the PHP information page showing PHP 8.4.


6. Secure Your Installation

  1. Remove the PHP info file after testing:

    sudo rm /var/www/your-site/info.php
    
  2. Adjust PHP configuration if needed:

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

    Common adjustments:

    • memory_limit = 256M
    • upload_max_filesize = 50M
    • post_max_size = 50M
    • max_execution_time = 300
  3. Restart PHP-FPM:

    sudo systemctl restart php8.4-fpm
    

7. Install and Secure MySQL

If MySQL isn't already installed:

sudo apt install mysql-server -y

Secure the installation:

sudo mysql_secure_installation

8. Connect PHP to MySQL

Test your PHP-MySQL connection:

  1. Create a test PHP file:

    <?php
    $conn = new mysqli("localhost", "username", "password", "database");
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    ?>
    
  2. Save it in /var/www/your-site/test-db.php and load it in your browser.


9. Install Additional Tools (Optional)

For development or debugging:

sudo apt install php8.4-xdebug php8.4-dev

Your Ubuntu 24.04 server is now ready with PHP 8.4, Nginx, and MySQL! 🎉