To install the Apache, MySQL, and PHP first of all you would need the root user OR the user with sudo privilege. To know more about creating users and granting privileges setup on Linux, see the amendment to this post.

In this post, we will see the step-by-step guide to the installation of LAMP, which means Apache MySQL and PHP on a Linux server. To understand this article you must be aware to Log into the SSH tool (putty).

Step 1: Install Apache with SSH commands

Apache is a widely used web server. For installing apache on Ubuntu(Linux) you need to first update the package manager cache. For this type in the following command. This will update the package cache and you can get updated versions of the package.

$ sudo apt update

Now, you type the below command to install the apache with the latest available version in the package manager. If you want to install the specific version you need to specify that separately in the command.

$ sudo apt install apache2

Press enter, and it will ask for confirmation. Press the y and enter again. This will start to install apache.

After your installation is finished, you need to allow the HTTP traffic in firewall settings. To view all available UFW application profiles, type the below command

$ sudo ufw app list

This will Output as:

Available applications:
  Apache Full
  Apache Secure
  • Apache: opens only port 80 (non-encrypted/ non-HTTPS).
  • Apache Full: opens both port 80(non-encrypted / non-HTTPS) and port 443 (HTTPS/TLS/SSL /encrypted ).
  • Apache Secure: opens only port 443 (HTTPS/TLS/SSL /encrypted).

You can type in the command below, for this tutorial we will allow only traffic on port 80 ie HTTP / non-encrypted traffic

$ sudo ufw allow in "Apache"

Check the status to verify the changes:

$ sudo ufw status

This will output as:

Status: active
To                         Action      From
OpenSSH                    ALLOW       Anywhere                                
Apache                     ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)                    
Apache (v6)                ALLOW       Anywhere (v6)    

Now check the status of the installed apache visit the default apache page by typing the server IP in the browser


You will see the default apache page in the browser, if not then you need to debug your installation. Try to reinstall the apache from start.

Step 2: Install MySQL on Linux (Ubuntu)

To install MySQL, type the command

$ sudo apt install mysql-server

It will prompt you to confirm, type the Y, and press Enter, it will start to install the MySQL server on your machine. When finished, test whether you’re able to log in to the MySQL console by

$ sudo mysql

This will output as:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.28-0ubuntu4 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

This means your MySQL installation is done successfully

Type exit command to exit MySQL

mysql> exit

Step 3: Install PHP on Linux (Ubuntu)

In addition to the php package, you’ll need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. You’ll also need libapache2-mod-php to enable Apache to handle PHP files. Core PHP packages will automatically be installed as dependencies.

To install these packages, type the command:

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

Press enter, this will start installing PHP on your machine. After finishing installation type the command below to verify if the PHP is installed successfully

$ php -v

This will output as:

PHP 8.1.2 (cli) (built: Mar  4 2022 18:13:46) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

If you see the output like above, then congratulations your PHP installation is successful.

You are ready to start using your Apache, PHP, and MySQL for development. Cheers!

Your feedback is highly appreciated!