How to Install PHP and MySQL on Ubuntu: Beginner’s Tutorial

How to Install PHP and MySQL on Ubuntu: Beginner’s Tutorial
Share

Setting up a development environment is one of the first steps for anyone diving into web development. If you’re new to coding and want to learn how to install PHP and MySQL on Ubuntu, this beginner-friendly tutorial is for you. By following these step-by-step instructions, you’ll have a fully functional PHP and MySQL setup on your Ubuntu system, ready for building dynamic websites or applications.

In this guide, you’ll learn:

  • How to install PHP on Ubuntu and verify its installation.
  • How to set up MySQL for database management.
  • How to configure PHP and MySQL to work together.
  • Troubleshooting tips for common installation issues.
  • Best practices for securing your setup.

Whether you’re building a personal project or setting up a local server, this tutorial will guide you through every step. Let’s get started!

Why Use PHP and MySQL on Ubuntu?

PHP is a powerful server-side scripting language used to create dynamic web applications, while MySQL is a robust database management system perfect for storing and retrieving data. Ubuntu, a popular Linux distribution, is a stable and secure choice for hosting these tools. Together, they form the backbone of many web applications, including platforms like WordPress.

By setting up PHP and MySQL on Ubuntu, you create a local development environment that mirrors production servers, making it easier to test and deploy your projects. This tutorial assumes you’re using Ubuntu 20.04 or later, but the steps are similar for other versions.

Prerequisites

Before we begin, ensure you have:

  • An Ubuntu system (desktop or server, version 20.04 or later).
  • Access to a terminal with sudo privileges.
  • A stable internet connection.
  • Basic familiarity with the Linux command line (don’t worry, we’ll keep it simple!).

Step 1: Update Your Ubuntu System

Before installing any software, it’s a good practice to update your system’s package index to ensure you’re installing the latest versions of PHP and MySQL. Open your terminal and run:

sudo apt update
sudo apt upgrade -y

The sudo apt update command refreshes the package list, and sudo apt upgrade installs any available updates. This step ensures compatibility and security.

Step 2: Install PHP on Ubuntu

PHP is easy to install on Ubuntu using the apt package manager. To install PHP and common extensions, run:

sudo apt install php php-common php-mysql php-cli

Here’s what each package does:

  • php: The core PHP package.
  • php-common: Common PHP libraries.
  • php-mysql: Extension for connecting PHP to MySQL.
  • php-cli: Command-line interface for running PHP scripts.

After installation, verify PHP is installed by checking its version:

php -v

You should see output like:

PHP 8.1.2 (cli) (built: Jan 17 2023 12:34:56) ( NTS )

If you see this, PHP is installed correctly. If not, double-check the installation command or consult the troubleshooting section below.

Step 3: Install MySQL on Ubuntu

Next, let’s install MySQL, a popular database system. Run the following command:

sudo apt install mysql-server -y

This installs the MySQL server and client. Once installed, start the MySQL service and enable it to run on boot:

sudo systemctl start mysql
sudo systemctl enable mysql

Verify MySQL is running:

sudo systemctl status mysql

Look for “active (running)” in the output. If MySQL isn’t running, try restarting it with sudo systemctl restart mysql.

Step 4: Secure Your MySQL Installation

MySQL includes a security script to lock down your database. Run:

sudo mysql_secure_installation

This script prompts you to:

  • Set a root password for MySQL.
  • Remove anonymous users.
  • Disallow remote root login.
  • Remove the test database.
  • Reload privilege tables.

Follow the prompts and choose “Y” (yes) for most options to secure your setup. For a beginner, setting a strong root password and removing anonymous users are critical steps.

Step 5: Test PHP and MySQL Integration

To ensure PHP and MySQL work together, create a simple PHP script to connect to your database. First, log in to MySQL to create a test database:

sudo mysql -u root -p

Enter your root password when prompted. Then, create a database:

CREATE DATABASE test_db;

Next, create a PHP file to test the connection. If you’re using a web server like Apache (covered in the next section), create a file in /var/www/html. Otherwise, for a local test, create a file anywhere, such as ~/test.php:

<?php

$servername = "localhost";
$username = "root";
$password = "your_root_password";
$dbname = "test_db";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully to MySQL!";
$conn->close();

Run the script from the terminal:

php ~/test.php

If you see “Connected successfully to MySQL!”, your PHP and MySQL setup is working.

Step 6: Optional – Install and Configure Apache

To serve PHP files over the web, you’ll need a web server like Apache. Install it with:

sudo apt install apache2 -y

Start and enable Apache:

sudo systemctl start apache2
sudo systemctl enable apache2

Place your test.php file in /var/www/html, then visit http://localhost/test.php in your browser. You should see the “Connected successfully to MySQL!” message.

For more information on setting up Apache, check out the official Apache documentation.

Step 7: Troubleshooting Common Issues

Here are solutions to common problems:

  • PHP not found: Ensure PHP is installed (php -v). If not, reinstall with sudo apt install php.
  • MySQL connection errors: Verify your root password and ensure the MySQL service is running (sudo systemctl status mysql).
  • Apache not serving PHP: Install the PHP module for Apache (sudo apt install libapache2-mod-php) and restart Apache (sudo systemctl restart apache2).

Best Practices for Your PHP and MySQL Setup

To keep your environment secure and efficient:

  • Regularly update PHP and MySQL with sudo apt update && sudo apt upgrade.
  • Use strong passwords for MySQL users.
  • Avoid using the root MySQL account for application connections; create a dedicated user instead.
  • Back up your databases regularly.

Conclusion

Congratulations! You’ve successfully learned how to install PHP and MySQL on Ubuntu and set up a basic development environment. With PHP and MySQL configured, you’re ready to start building dynamic web applications or explore frameworks like Laravel or WordPress.

Want to dive deeper? Check out our Ubuntu tutorials for more guides on web development, Linux, and programming!

Table of Contents
Scroll to Top