Introduction
LAMP is an acronym for Linux, Apache, MariaDB/MySQL & PHP. A LAMP Stack hosts dynamic websites and web apps. Its Windows counterpart is WAMP, the only difference being the Operating System. Functions of services making up a LAMP Stack are as follows:
- Linux – Servers as the Base OS for the LAMP Stack.
- Apache – Serves Web Content.
- MariaDB / MySQL – MariaDB is a community-developed fork of MySQL Database Server.
- PHP – PHP is one of the most commonly used programming languages for developing web content.
Prerequisites
- Ubuntu 22.04 Server Install
- Root user / Non-Root user with SUDO privileges.
- A Public / Static IP Address is attached to your server.
- A domain is pointing to your webserver’s public/static IP Address. We can do this by adding an “A” Record pointing to your IP Address in your domain’s DNS records.
1. Update your system packages
Suppose you are installing Apache on a fresh Ubuntu 22.04 Server. You need to update the software repository and packages on the system.
apt update -y
2. Installation of Apache Web Server
Now we will install Apache on our Ubuntu Server using the following command.
apt install apache2 -y
Select OK on the following screen.
After the installation is complete, start and enable the Apache Service.
systemctl enable apache2 systemctl start apache2
Now you need to enable HTTP / HTTPS via the ubuntu firewall using the following command
ufw allow "Apache Full"
Open your browser and type http://YOUR-IP-ADDRESS or http://YOUR-DOMAIN-NAME to verify that Apache has been installed successfully.
3. Installation / Configuration of MariaDB Database Server
Once you have completed the installation of Apache, you can go ahead with the installation of MariaDB. Install MariaDB using the default APT repository in Ubuntu 22.04.
apt install mariadb-server -y
Start and enable the MariaDB Service.
systemctl start mariadb systemctl enable mariadb
3.1 Secure your MariaDB Service
The default installation of MariaDB is insecure. You can secure MariaDB with a script known as mysql_secure_installation.
mysql_secure_installation
On running the script you will be asked a series of interactive questions. Respond as below:
- Enter current password for root (enter for none) – Press Enter (Note: This is not the system root user / MariaDB Administrator is also known as root)
- Set root password? [Y/n] – Y
- Remove anonymous users? [Y/n] – Y
- Disallow root login remotely? [Y/n] – Y
- Remove test database and access to it? [Y/n] – Y
- Reload privilege tables now? [Y/n] – Y
3.2 Creation of Database
After securing MariaDB, you may create your first database. Login to MariaDB using the password you assigned earlier.
mysql -u root -p
You may now create the database using the following command.
create database [DATABASE NAME];
3.3 Creation of MariaDB User / Assign Database Permissions
Now you can create a user and assign it permissions over the databases you just created.
GRANT ALL ON tuxpedia.* TO 'tuxpedia'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION; FLUSH PRIVILEGES;
After installation of PHP, you may start using the database created above for your application.
4. Installation of PHP
Now, you may install PHP using the default APT repository. Furthermore, you will also need to install an extension php-mysql which is required for PHP to interact with MariaDB / MySQL.
apt install php php-mysql -y
Now, restart Apache Server to enable PHP Processing.
systemctl restart apache2
4.1 Testing PHP on Apache Server (Optional)
Next, you can test if your Apache Server is processing PHP properly.
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
The above command creates a file which contains a function to display information regarding the PHP installed on your system. To access it, you can open the browser and type http://YOUR-IP-ADDRESS/info.php . Furthermore, if you have an A record configured, you can open it via http://YOUR-DOMAIN-NAME/info.php.
5. Conclusion
You have successfully completed the installation of LAMP Stack on your Ubuntu 22.04 Server. Now you can write your own PHP content OR you may install any PHP application and connect it with your Database Server.