Ubuntu

How to Set up LAMP(Linux, Apache, MySQL, PHP) Stack on Ubuntu 22.04

For installing PHP-based applications, most of the developers use the LAMP (Linux, Apache, MySQL, and PHP) stack on Linux-based systems such as Ubuntu. It can be set up on both virtual and physical servers. A LAMP stack offers both static and dynamic web content. Common use cases of the LAMP stack are online shops or websites developed by utilizing the LAMP server’s Content Management System(CMS).

This write-up will discuss the procedure to set up LAMP Stack on Ubuntu 22.04. So, let’s start!

How to Set up LAMP(Linux, Apache, MySQL, PHP) Stack on Ubuntu 22.04

To set up LAMP Stack on Ubuntu 22.04, you must follow the below-given step-by-step instructions.

Step 1: Update system repositories

Press “CTRL+ALT+T” to open the terminal of your Ubuntu 22.04 and run the below-given command to update system repositories:

$ sudo apt update

Step 2: Set up PHP on Ubuntu 22.04

Decide the PHP version which you want to install on Ubuntu 22.04 system. For instance, we will install “PHP 8.1”. To do the same, firstly, add the “ondrej/php” repository:

$ sudo add-apt-repository ppa:ondrej/php

After adding the repository, execute the following command to install PHP version “8.1”:

$ sudo apt install php8.1

Wait for a few minutes as the PHP installation will take some time to complete:

Step 3: Set up Apache on Ubuntu 22.04

PHP also installs Apache and its related modules on Ubuntu 22.04. However, you can still run the below-given command for confirmation:

$ sudo apt install apache2 libapache2-mod-php8.1 -y

Next, open port numbers “80” and “43” in the UFW firewall:

$ sudo ufw allow 80/tcp

$ sudo ufw allow 43/tcp

After performing the specified operation, open up your favorite web browser and navigate to the server address. Upon doing so, the following default page of the Apache Server will be displayed:

Step 4: Set up MySQL server on Ubuntu 22.04

In the next step, install the MySQL server on your system with the help of the following command:

$ sudo apt-get install mysql-server

Now, write out the below-given command for performing an initial and interactive configuration of the MySQL server:

$ sudo mysql_secure_installation

In the configuration manual, you will be first asked to set the level for the password validation policy. Input a number from the given menu according to your password validation requirements:

After doing so, type out the new password for “root” and enter “y” to continue with the provided password:

Also, set the other configuration options accordingly:

Next, log in to the MySQL server to check if the connection with the server is established or not:

$ sudo mysql

Step 5: Install other required packages

You may also need to install modules such as PHP extensions based on application requirements. Execute the following command in Ubuntu 22.04 terminal to check the available PHP extensions:

$ sudo apt search php8.1-*

As you can see, the given command will print out the list of available PHP modules:

Lastly, install the required PHP modules with the help of the following command:

$ sudo apt install php8.1-mysql php8.1-curl php8.1-xml

Step 6: Verify set up of LAMP(Linux, Apache, MySQL, PHP) Stack on Ubuntu 22.04

By carefully following the previous steps, you must successfully set up LAMP(Linux, Apache, MySQL, PHP) Stack on Ubuntu 22.04. To validate the integration between Apache and PHP, create a PHP script named “phpinfo.php” in the document root website directory:

$ sudo nano /var/www/html/phpinfo.php

Add the below-given content in the created “phpinfo.php” test script and press “CTRL+O” to save it:

<?php

phpinfo();

?>

Lastly, utilize the web browser to access the created PHP test script with the server IP address:

The content of the above web page signifies that we have successfully set up LAMP(Linux, Apache, MySQL, PHP) Stack on Ubuntu.

Conclusion

To set up LAMP Stack on Ubuntu 22.04, firstly, execute the “$ sudo add-apt-repository ppa:ondrej/php” command to add “ondrej/php” repository, then run “$ sudo apt install php8.1” to install PHP and “$ sudo apt-get install mysql-server” to set up MySQL. The PHP package will also comprise Apache installation for Ubuntu 22.04. This write-up discussed the method to set up LAMP Stack on Ubuntu 22.04.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.