Install Apache PHP MySQL on Ubuntu 22.04

Ubuntu Server 7 Comments

Install Apache PHP MySQL on Ubuntu 22.04

Introduction – Install Apache PHP MySQL on Ubuntu 22.04

“LAMP” stack is a very popular word nowadays and it just means – Linux (Ubuntu comes under this), Apache, MySQL, and PHP.  All of these are open-source software and are installed together in order to enable a web server to host PHP-based web applications and dynamic websites (based on WordPress CMS etc).

By the end of this tutorial, you’ll be able to set up a LAMP stack on an Ubuntu 22.04 machine.

If you don’t have an Ubuntu machine, you can try DigitalOcean to get a machine (or VPS), popularly known as DROPLET in the DigitalOcean community. Signup DigitalOcean using this link and get $100 worth of credits for FREE.


In order to complete this tutorial, you have to have an Ubuntu 22.04 server with a  sudo-enabled user account.

Step 1 — Installing Apache and Updating the Firewall

The Apache web server is popularly used, well documented, and has an active community of users.
We will start by updating the package manager cache of our Ubuntu system.

sudo apt-get update

After this, we’ll install Apache by running the following command in our terminal:

sudo apt-get install apache2

Please confirm Apache’s installation by pressing Y, then ENTER.

After this is done, we need to adjust the firewall settings by running the following commands:

sudo ufw app list
sudo ufw allow in "Apache"

Explaining these commands in detail doesn’t make much sense here. If you’re interested, please use Google search. 🙂

Now Apache is installed, you can confirm this by visiting:


You should see Apache’s default page like the below screenshot:

Install Apache PHP MySQL on Ubuntu 22.04

Apache’s Default Page

Step 2 — Installing MySQL
sudo apt-get install mysql-server mysql-client

When you’re finished, test whether you’re able to log in to the MySQL console by typing:

sudo mysql

In order to exit the MySQL console, type exit:

Step 3 — Installing PHP

In order to install the latest PHP and all the related packages, please run the following command:

sudo apt install php libapache2-mod-php php-mysql php-curl php-xml php-gd mysql-client zip unzip php-zip php-json php-mbstring

In order to install “mcrypt” extension, we will have to run following commands to install its pre-requisites first:

sudo apt-get install php-dev php-pear libmcrypt-dev gcc make autoconf libc-dev pkg-config

After this, we’ll have to run the following commands to compile the mcrypt extension:

sudo pecl install mcrypt

Just press enter when it asks about the libmcrypt prefix.

To enable the extension for apache, run the following command:

echo "" | sudo tee -a /etc/php/8.1/apache2/conf.d/mcrypt.ini

After this, restart apache

sudo service apache2 restart

Now you’ve installed Latest PHP and its most useful and needed extensions on your Ubuntu 22.04 machine. You can confirm your PHP version by running the following command:

php -v
Step 4 (Optional) — Setting up SWAP memory

We’ll run the following commands to setup SWAP memory of 2GB:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo ‘/swapfile none swap sw 0 0’ | sudo tee -a /etc/fstab

Now we need to add 2 parameters in sysctl.conf file.
In order to do this, run the following commands:

sudo nano /etc/sysctl.conf

And add the following 2 lines at the end of the sysctl.conf file:


Press control+x and they Y to save and exit.
Run the following command to confirm the successful SWAP file creation:

free -h

That’s it.

Step 5 (Optional) — Setting up proper permissions

In order to set up proper permissions for your /var/www/html folder, just run the following 2 commands:

sudo usermod -aG www-data $USER
sudo chown -R www-data:www-data /var/www
Step 6 (Optional) — Basic commands for creating a database, a user, and giving it permissions to access it.

Let’s login to MySQL first:

mysql -u root -p

Enter your password and then in order to create a database “testdb”, run the following command:

create database testdb;

Now we’ll create a user “dbuser” with password “dbuserpass” which will access this database from “” server to access testdb which we created above :

CREATE USER 'dbuser'@'' IDENTIFIED BY 'dbuserpass';
GRANT ALL PRIVILEGES ON testdb.* TO 'dbuser' @'';

That’s it.

Step 7 (Optional) — Setup Memcached cache software

Just run the following 2 commands:

sudo apt install memcached libmemcached-tools -y
sudo apt install php-memcached

This completes the tutorial to set up your Ubuntu 22.04 server with Apache, MySQL, and PHP. If you need any help (paid) in setting up WordPress or with Nginx then feel free to drop an email to and our team will be happy to help!

5 1 vote
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
Manuel Cano

la ultima linea del paso 4 debería ser:

echo ‘/swapfile none swap sw 0 0’ | sudo tee -a /etc/fstab

Last edited 1 year ago by Manuel Cano