How to Install PHP 8.3 and MySQL on Ubuntu Server (Step-by-Step Guide)

How to Install PHP 8.3 and MySQL on Ubuntu Server (Step-by-Step Guide)

PHP and MySQL are core components of modern web applications. In this guide, you’ll learn how to install PHP 8.3 and MySQL on an Ubuntu server with Apache, following best practices for stability and performance.

New to AWS? If you want to create an Ubuntu server on AWS using the Free Tier, click here for a step-by-step guide.

Prerequisites

  • Ubuntu 20.04 / 22.04 server
  • Apache web server installed
  • Root or sudo access
  • SSH access to the server

Step 1: Update Ubuntu System

sudo apt update
sudo apt upgrade -y

Step 2: Add PHP 8.3 Repository

Ubuntu does not include PHP 8.3 by default. We’ll use the trusted Ondřej Surý PPA.

sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Step 3: Install PHP 8.3

sudo apt install php8.3 libapache2-mod-php8.3 -y

Step 4: Install Common PHP Extensions

These extensions are commonly required for Laravel, WordPress, and other PHP applications.

sudo apt install php8.3-cli php8.3-common php8.3-curl php8.3-mbstring \
php8.3-mysql php8.3-xml php8.3-zip php8.3-bcmath php8.3-gd -y

Step 5: Verify PHP Installation

php -v

You should see output similar to:

PHP 8.3.x (cli)

Step 6: Configure Apache to Use PHP 8.3

sudo a2enmod php8.3
sudo systemctl restart apache2

Step 7: Install MySQL Server

sudo apt install mysql-server -y

Step 8: Secure MySQL Installation

Run the security script to harden MySQL.

sudo mysql_secure_installation

Recommended answers:

  • Enable VALIDATE PASSWORD plugin: Yes
  • Set root password: Yes
  • Remove anonymous users: Yes
  • Disallow root login remotely: Yes
  • Remove test database: Yes
  • Reload privilege tables: Yes

Step 9: Verify MySQL Installation

sudo mysql

If MySQL opens successfully, the installation is complete. Exit with:

exit;

Step 10: Test PHP with Apache

Create a PHP info file:

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

Add the following content:

<?php
phpinfo();
?>

Save the file and open in browser:

http://your-server-ip/info.php

You should see the PHP 8.3 information page.

Important: Delete this file after testing.

sudo rm /var/www/html/info.php

Step 11: Restart Services

sudo systemctl restart apache2
sudo systemctl restart mysql

Common Issues and Fixes

  • PHP not working – Check Apache PHP module is enabled
  • MySQL access denied – Verify authentication method
  • Extensions missing – Install required PHP extensions

Best Practices

  • Do not expose phpinfo() publicly
  • Regularly update PHP and MySQL
  • Use environment variables for DB credentials
  • Backup MySQL databases regularly