6.4.10 Installation and configuration of LAMP
LAMP is a software bundle that represents a popular technology stack for developing and deploying web applications. LAMP is widely used due to its reliability, flexibility, and open-source nature. This acronym stands for:
- L — Linux family operating system.
- A — Apache web server.
- M — MySQL or MariaDB database management system.
- P — PHP programming language for creating web pages.
In this article, we’ll look at the process of installing and configuring LAMP on various Linux distributions, including Debian, Ubuntu, CentOS, AlmaLinux, and RockyLinux.
Important: Before starting the installation, make sure You have superuser (root) rights or the ability to use sudo. It’s also recommended to create a backup of important data before making changes to the system.
We recommend performing all the actions described below on a clean OS image, without pre-installed control panels or other functionality.
Note: instructions have been verified on the following OS versions: Ubuntu 18.04-22.04; Debian 8–12; CentOS 7, 8; AlmaLinux 8, 9. Functionality on later versions is not guaranteed.
Installation for Debian and Ubuntu
1. System update
Before starting the installation, update the package list and the system itself:
2. Installing Apache
Install the Apache web server:
3. Installing MySQL
Install the MySQL server:
After installation, run the MySQL secure installation script:
Answer the script’s questions, it’s recommended to answer Y to all questions to enhance security:
- Set root password? [Y/n] – Set a password for root if it wasn’t set before.
- Remove anonymous users? [Y/n] – Remove anonymous user.
- Disallow root login remotely? [Y/n] – Prohibit remote login as root.
- Remove test database and access to it? [Y/n] – Remove test database.
- Reload privilege tables now? [Y/n] – Apply changes.
4. Installing PHP
Install PHP and necessary modules. In this example, we’ll use PHP 8.1, but You can replace the version with the one relevant to Your case:
Ubuntu 18.04-22.04 and Debian 9–12: installing PHP 8.1.
To install PHP 8.1 or another needed version, use the PPA repository:
Then install PHP and necessary modules:
Debian 8 and older versions: installing PHP 8.1.
To install PHP 8.1 or another needed version, use a third-party repository:
Then install PHP and necessary modules:
Note: if You need a different PHP version (for example, PHP 7.4 or PHP 8.2), replace php8.1
with the needed version.
You can check the installed PHP version with the command:
The command output will display the installed PHP version:
5. Configuring Apache for PHP
Create a test PHP file:
5. Checking services status
After installing all LAMP components, check their status and make sure they are set to autostart:
When checking, the status should display with the text active (running) – this will mean that everything was installed correctly.
Make sure all services are added to autostart:
6. Configuring firewall for Debian and Ubuntu
If You’re using UFW (Uncomplicated Firewall), allow HTTP and HTTPS traffic:
Alternatively: if You plan to work with the IPTables utility.
Execute commands for automatic rule loading:
Then, allow access to HTTP and HTTPS by executing the following commands:
7. Testing after reboot
To ensure that all LAMP components start automatically after server reboot, perform the following actions:
After the server reboots, check the services status:
When checking, the status should display with the text active (running) – this will mean that everything was installed correctly.
8. Check the web server functionality
To check PHP operation, check the file created earlier, open it in the browser’s address bar.
http://255.255.255.255/phpinfo.php
, where instead of numbers 255 (255.255.255.255
) You need to specify the IP that was issued when creating the server.
You can view the IP address in the letter about opening the service.

We inform: the installation has been successfully completed. Now You can upload Your website files to the /var/www/html/
folder
Installation for CentOS, AlmaLinux, and RockyLinux
1. System update
Before starting the installation, update the system:
2. Installing Apache
Install the Apache web server:
3. Installing MySQL (MariaDB)
Install MariaDB:
After installation, run the MariaDB secure installation script:
Answer the script’s questions, it’s recommended to answer Y to all questions to enhance security:
- Set root password? [Y/n] – Set a password for root if it wasn’t set before.
- Remove anonymous users? [Y/n] – Remove anonymous user.
- Disallow root login remotely? [Y/n] – Prohibit remote login as root.
- Remove test database and access to it? [Y/n] – Remove test database.
- Reload privilege tables now? [Y/n] – Apply changes.
4. Installing PHP
Depending on Your operating system version, execute the appropriate commands to install PHP 8.1.
AlmaLinux 9 and RockyLinux 9: installing PHP 8.1.
For AlmaLinux 9 and RockyLinux 9, execute the following commands:
CentOS 8, AlmaLinux 8, RockyLinux 8: installing PHP 8.1.
For CentOS 8, AlmaLinux 8, and RockyLinux 8, use dnf
and install the repositories:
CentOS 7: installing PHP 8.1.
For CentOS 7, use yum
and install the EPEL and Remi repositories:
Note: if You need a different PHP version (for example, PHP 7.4 or PHP 8.2), replace php8.1
with the needed version.
You can check the installed PHP version with the command:
The command output will display the installed PHP version:
5. Configuring Apache for PHP
Create a test PHP file:
6. Checking services status
After installing all LAMP components, check their status and make sure they are set to autostart:
When checking, the status should display with the text active (running) – this will mean that everything was installed correctly.
Make sure all services are added to autostart:
7. Configuring firewall for CentOS, AlmaLinux, and RockyLinux
Allow HTTP and HTTPS traffic through the firewall:
Alternatively: if You plan to work with the IPTables utility.
- Install
iptables-services
to ensure automatic rule loading:
- Enable and start the
iptables
service:
- Allow access via HTTP and HTTPS using the following commands:
- Save the
iptables
rules:
- To ensure that the rules are applied upon reboot, execute:
8. Testing after reboot
To ensure that all LAMP components start automatically after server reboot, perform the following actions:
After the server reboots, check the services status:
When checking, the status should display with the text active (running) – this will mean that everything was installed correctly.
9. Check the web server functionality
To check PHP operation, check the file created earlier, open it in the browser’s address bar.
http://255.255.255.255/phpinfo.php
, where instead of numbers 255 (255.255.255.255
) You need to specify the IP that was issued when creating the server.
You can view the IP address in the letter about opening the service.

We inform: the installation has been successfully completed. Now You can upload Your website files to the /var/www/html/
folder
Security Recommendations
Now You have installed and configured the LAMP stack on Your system. This is a basic configuration that can be further customized for Your project’s specific needs. It is recommended to regularly update LAMP components to ensure security and stability.
We remind You:
after completing the testing, it is recommended to delete the phpinfo.php
file, as it may provide potential attackers with information about Your system:
Additionally:
-
Consider changing the standard SSH port (22) to a non-standard one to reduce the number of automated attacks.
-
Use key-based authentication instead of passwords for SSH.
-
Configure fail2ban to protect against brute-force attacks.
-
Regularly check server logs for suspicious activity.
-
Consider using a Web Application Firewall (WAF) for additional protection of web applications.