LAMP (Linux, Apache, MySQL, and PHP) stack is a widely used hosting stack made up of open-source software used for hosting both static and dynamic web pages. It comprises the Apache web server, MariaDB or MySQL database engine, and PHP which is a backend scripting language. In this guide, you will learn how to install LAMP stack on Ubuntu 22.04.
Step 1: Install Apache
The apache web server is available in the Ubuntu repository. Therefore to install it, simply run the following command:
$ sudo apt install apache2
Upon installation, The Apache web servers starts automatically. You can verify this by checking the status as shown.
$ sudo systemctl status apache2
From the output, we can see that the webserver is up and running.
Next, enable Apache to run automatically on system boot as shown:
$ sudo systemctl enable --now apache2
If you are running behind a firewall allow the webserver service using the following command:
$ sudo ufw allow 'Apache'
To verify that apache is correctly installed, head over to your server’s ip address.
The following page will be displayed.
Step 2: Install MariaDB database server
MariaDB is an opensource relational database server that is a fork and a drop-in replacement of MySQL. It provides better performance, speed, and replication when compared to MySQL thanks to its advanced features such as storage engines, Galera clustering, and many more.
To install mariaDB on Ubuntu 22.04 run the following command:
$ sudo apt install mariadb-server
Like Apache, MariaDB also starts automatically once the installation is done. To confirm that MariaDB is running check the status of MariaDB on the system:
$ sudo systemctl status mariadb
Also, be sure to enable the database service to start on system startup.
$ sudo systemctl enable mariadb
Next, secure your database server from common threats by running the following script:
$ sudo mysql_secure_installation
First, create a password for the root account.
To secure your database server, type ‘Y‘ for the remaining prompts to achieve the following:
- Remove anonymous users
- Disallow remote root login
- Remove test database
- Reload privilege tables
Step 3: Install PHP and the Required Extensions
By default, Ubuntu 22.04 provides PHP 8.1 from its repositories. As such, you can install it using the following command.
$ sudo apt install php8.1 libapache2-mod-php8.1
Once PHP is installed, you can verify its installation on the command line as follows:
$ php -v
Next, you might also consider installing some PHP extensions. Here, we have installed a few PHP extensions
$ sudo apt install php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-intl -y
Next, let’s create a PHP script to test out PHP integration with Apache.Run the following command:
$ echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Open your browser and go to the following address:
And that’s it. We have demonstrated how to install LAMP stack on Ubuntu 22.04. From here, you can proceed and host your website(s) on the LAMP stack.