Page 1 of 1

Installing Apache with PHP and Mysql/Mariadb

Posted: Mon Aug 31, 2026 3:45 am
by ianadmin
How to install an Apache server with PHP and Mariadb (mysql),
In order to host a website, you need to install a web server; Apache is a popular choice to run on Linux. If you want your site to be dynamic, you will also need to install a database such as Mariadb or Mysql and PHP. With this setup, Apache will serve the pages, data is stored in the database and PHP will process the dynamic content. Like my other posts, I use Ubuntu, so you may have to change your commands slightly if you are using other distros.
Like always, first make sure your system is up-to-date:
$sudo apt update
$sudo apt full-upgrade
$sudo apt clean
$sudo apt autoremove
Before we install everything, make sure you open ports in your firewall (I will assume you have UFW already installed):
$sudo ufw allow http
$sudo ufw allow https
Next, install Apache:
$sudo apt install apache2
Make sure it is started and enabled:
$sudo systemctl start apache2
$sudo systemctl enable apache2
Check it’s status:
$sudo systemctl status apache2
Now you should check if Apache is running by inputting you computers IP address into the address bar. To find your IP address:
$ip a
Then:
http://<your_ip_address>
The next step is to install either mariadb or mysql. Look into which one you want; there are some differences but the most common commands work the same on both. Do your research and decide which one you want.
Do either:
$sudo apt install mysql-server
Or
#sudo apt install mariadb-server
You can’t have both running at the same time. Which one you install won’t make a difference for most users; it WILL make a difference for more advanced usage.
Once installed (doesn’t matter which one from here on in), run:
$sudo mysl_secure_installation
Just follow the prompts and answer Yes (Y) or No (N)).
Check that you can log in:
$sudo mysql -u root (if you didn’t set a root password), or
$sudo mysql -u root -p (if you set a root password). Enter password after running this command
Once logged in, exit by typing
exit;
You have a decision here; either install PHP as a module or install it with FastCGI. Research which one you want to install it as. Both have their advantages. I will show you how to install either way.
Installing with FastCGI.
$sudo apt install php8.3 php8.3-fpm libapache2-mod-fcgid php-mysql (PHP 8.3 is the latest on Ubuntu; you can replace it with a different version if you want)
$sudo a2dismod php8.3
$sudo a2dismod mpm_prefork
$sudo a2enmod proxy_fcgi setenvif
$sudo a2enconf php8.3-fpm
$sudo systemctl enable php8.3-fpm
$sudo systemctl start php8.3-fpm
$sudo systemctl restart apache2
Restart Apache to enable PHP_FPM:
$sudo systemctl restart apache2
If you want to install PHP as a module these are the steps:
$sudo apt install php libapache2-mod-php php-mysql
Now that PHP is installed, we should change the way that Apache presents the files whenever a directory is asked for, By default, Apache looks for index.html first, however, we want Apache to look for index.php first.
To do this, we need to edit the dir.conf file:
$sudo vi /etc/apache2/mods-enabled/dir.conf
Move the index.php to just before index.html
Save and exit, then restart Apache
$sudo systemctl restart apache2
We can test that everything is working, we will create a file called info.php.
$sudo vi /var/www/html/info.php
Enter the following:
<?php
phpinfo();
?>
Go to http://<your_ip_address>/info.php and you will be shown information about PHP
Since this file shows sentsitive information about your setup, we will go ahead and delete the file:
$sudo rm /var/www/html/info.php
We can also test out that we can connect to a MariaDB database. To do this, we first need to create a database in Mariadb. We will also create a new user to access the new database. In the commandline, enter:
$sudo mariadb -u root
Once in MariaDB, enter:
CREATE DATABASE testingdb;
Now create a new user and grant them full privileges on the custom database you’ve created:
CREATE USER 'example_user'@'localhost’ IDENTIFIED BY 'password';
Now, we will give permissions over the testingdb to the new user:
GRANT ALL ON testingdb.* TO 'example_user'@'localhost';
This gives the user full privileges over the database while preventing them from modifying other databases.
Next, flush the privileges to ensure that they are saved and available in the current session:
FLUSH PRIVILEGES;
Following this, exit the MariaDB shell:
exit
You can test if the new user has the proper permissions by logging in to the MariaDB console again, this time using the custom user credentials:
mariadb -u example_user -p
Switch to the testingdb:
USE testingdb;
Now we can create a table:
CREATE TABLE todo_list (
item_id INT AUTO_INCREMENT,
content VARCHAR(255),
PRIMARY KEY(item_id)
);
Insert a few rows of content in the test table. Repeat the next command a few times, using different values, to populate your test table:
INSERT INTO todo_list (content) VALUES ("My first important item");
To confirm that the data was successfully saved to your table, run:
SELECT * FROM example_database.todo_list;
You will receive the following output:
Output
+---------+--------------------------+
| item_id | content |
+---------+--------------------------+
| 1 | My first important item |
| 2 | My second important item |
| 3 | My third important item |
| 4 | and this one more thing |
+---------+--------------------------+
4 rows in set (0.000 sec)
After confirming that you have valid data in your test table, you can exit the MariaDB console:
exit
Now you can create the PHP script that will connect to MariaDB and query for your content. Create a new PHP file in your custom web root directory using your preferred editor:
sudo vi /var/www/html/todo_list.php
The following PHP script connects to the MariaDB database and queries for the content of the todo_list table, exhibiting the results in a list. If there’s a problem with the database connection, it will throw an exception.
Add this content into your todo_list.php script, remembering to replace the and values with your own:
/var/www/your_domain/todo_list.php
<?php
$user = "example_user";
$password = "password";
$database = "example_database";
$table = "todo_list";
try {
$db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
echo "<h2>TODO</h2><ol>";
foreach($db->query("SELECT content FROM $table") as $row) {
echo "<li>" . $row['content'] . "</li>";
}
echo "</ol>";
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Save and close the file when you’re done editing.
You can now access this page in your web browser by visiting the domain name or public IP address for your website, followed by /todo_list.php:
http://your_domain/todo_list.php
This web page will reveal the content you’ve inserted in your test table to your visitor:
That means your PHP environment is ready to connect and interact with your MariaDB server.