The LAMP stack, consisting of Linux, Apache, MySQL, and PHP, is a powerful combination that forms the foundation for many web applications. In this tutorial, we’ll guide you through the process of installing and configuring a LAMP stack on Ubuntu 22.04, the latest LTS release.
Update Package Lists
Before we begin, it’s a good practice to ensure that your package lists are up to date. Open a terminal and run:
sudo apt update
Install Apache
Apache is a widely used web server. Install it using the following command:
ubuntu@linews:~$ sudo apt install apache2
Check your installation on http://<your_ip_address>
Install MariaDB
Next, let’s install MariaDB, a robust relational database management system. Run the following command and after installation start MariaDB setup.
ubuntu@linews:~$ sudo apt install mariadb-server
ubuntu@linews:~$ sudo mysql_secure_installation
Enter for none
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none):
Enter ‘n’
Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.
You already have your root account protected, so you can safely answer 'n'.
Switch to unix_socket authentication [Y/n] n
and then…
You already have your root account protected, so you can safely answer 'n'.
Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
Thanks for using MariaDB!
ubuntu@linews:~$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 39
Server version: 10.6.16-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]> exit;
Install PHP and Required Modules
PHP is a server-side scripting language that is integral to many web applications. Install PHP and the necessary Apache module with:
ubuntu@linews:~$ sudo apt install php libapache2-mod-php php-mysql
ubuntu@linews:~$ php -v
PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies
ubuntu@linews:~$ sudo mkdir /var/www/html/your_domain
ubuntu@linews:~$ sudo chown -R $USER:$USER /var/www/html/your_domain
ubuntu@linews:~$ cd /var/www/html/your_domain/
ubuntu@linews:/var/www/html/your_domain$ nano index.php
<?php
echo date('Y-m-d');
?>
Check that PHP is working. Go to your website http://<your_ip_address>/your_domain
Install phpMyAdmin
ubuntu@linews:~$ sudo apt install phpmyadmin
That’s all. Go to http://<your_ip_address>/phpmyadmin
Congratulations! You’ve now set up a LAMP stack on Ubuntu 22.04, ready to host dynamic web applications.