Install Apache, PHP, MySQL & PhpMyAdmin on Ubuntu 22.04

In this tutorial, you will learn how to install LAMP stack on Ubuntu 22.04. LAMP stands for Linux (Operating System), Apache (Web Server), MySQL (Database Server) and PHP(Runtime Environment). It is assumed that you have a stable Ubuntu setup. You can try this in your local computer or production environment. Let’s jump into the tutorial.

If your Ubuntu version is 20.04, please check out my previous article.

sudo apt-get upgrade
sudo apt-get update

1. Install Apache

sudo apt install apache2

Make sure Firewall allows Apache to listen via port 80. Otherwise it is required to open this port so that your web server can handle incoming request from web clients. Let’s do some quick test by visiting public IP address.

http://YOUR_WEB_SERVER_IP_ADDRESS or http://localhost in local environment.

Everything is fine if you can see default Apache web page as below.

2. Install MySQL

sudo apt install mysql-server

sudo mysql_secure_installation

During MySQL secure installation, you might find following issue.
MySQL Failed! Error: SET PASSWORD has no significance for user root@localhost as the authentication method used doesn’t store authentication data in the MySQL server. Please consider using ALTER USER
If you didn’t encounter above issue, just skip following part and move to Install PHP section.

Open another terminal and terminate mysql_secure_installation process.

sudo killall -9 mysql_secure_installation

Start the MySQL client and login as root.

sudo mysql -u root -p

When prompted enter MySQL root password. After logged in, enter following SQL query.

> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MYSQL_ROOT_PASSWORD_HERE';
> exit

Then start mysql_secure_installation again.

sudo mysql_secure_installation

When promoted for the password enter the password you just set with above query. After that your MySQL setup will be secured and you’re good to go for the next section.

3. Install PHP

sudo apt install php libapache2-mod-php php-mysql

php -v

You would run following command to install other php extensions.

sudo apt install php-[extname]

4. Creating Virtual Hosts

cd /etc/apache2/sites-available/
sudo a2dissite 000-default.conf
sudo service apache2 restart
sudo nano app.conf

Sample virtual host file for Laravel application

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/
        <Directory /var/www/>
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        allow from all

Enable virtual host

sudo a2ensite app.conf
sudo apache2ctl configtest
sudo service apache2 restart

Create Apache user and set permissions for /var/www directory

sudo adduser ubuntu www-data
sudo chown -R www-data:www-data /var/www
sudo chmod -R g+rw /var/www
sudo service apache2 restart

Change DirectoryIndex settings so that .php files can get priority over .html files during rendering.

sudo nano /etc/apache2/mods-enabled/dir.conf

Update file as below.

<IfModule mod_dir.c>
        DirectoryIndex index.php index.html index.cgi index.xhtml index.htm
sudo service apache2 restart

Now, it’s better to do some quick test to verify virtual host is working properly. Create a directory named inside /var/www.

sudo -u www-data mkdir
sudo -u www-data mkdir public
cd public
sudo nano info.php

Go to and check that expected web page is rendered. Replace with your domain name / IP address.

5. Install PhpMyAdmin

sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl

As we’re using Apache as the web server, keep the default selection.

Select Yes on the following screen.

Next, provide MySQL application password for phpMyAdmin.

In Mysql Server secure installation step, we have enabled Validate Password plugin. With this selection, it will create error while attempting to set phpMyAdmin user password. Now we need to solve this. Select abort in the error screen.

We’re going to disable Validate Password plugin in order to complete phpMyAdmin installation and enable again.

sudo mysql

mysql> UNINSTALL COMPONENT "file://component_validate_password";

mysql> exit

sudo apt install phpmyadmin

sudo mysql
mysql> INSTALL COMPONENT "file://component_validate_password";
mysql> exit

Enable mbstring extension.

sudo phpenmod mbstring
sudo systemctl restart apache2

Now, it is possible to access phpMyAdmin using web browser. Go to Replace with your domain name / IP address. If this didn’t work, you may add following line to the bottom in apache configuration file.

sudo nano /etc/apache2/apache2.conf

Scroll down all the way and add this line.

Include /etc/phpmyadmin/apache.conf

Restart Apache server and you will find phpMyAdmin interface.

sudo service apache2 restart

7. Configure MySQL Root User

At this moment, MySQL root user is using auth_socket plugin for user login. This setup does not work when we try to login to PhpMyAdmin. For this, it needs to use mysql_native_password or caching_sha2_password. Therefore, Let’s fix this in MySQL console.

sudo mysql -u root -p
mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

You can see that root user is based on auth_socket. Now set your password using caching_sha2_password.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password';

Follow above steps and verify root user account is updated to use caching_sha2_password.

Now you may login to PhpMyAdmin and create additional users, databases…

To add some layer of security, it is worth to secure PhpMyAdmin in live environment.

8. Install Composer – [Extra]

php -r "copy('', 'composer-setup.php');"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer --version
php -r "unlink('composer-setup.php');"