Valid for Cloud Servers, VPS+, migrated Cloud Servers, VPS, Dedicated Servers, Bare Metal Servers, and Server Power Deals managed in the Cloud Panel.

This article explains how to install a LAMP stack on a VPS+, Cloud Server, migrated Cloud Server, VPS, Dedicated Server, or a Bare Metal Server running AlmaLinux 9, AlmaLinux 10, Rocky Linux 9, or Rocky Linux 10.

A LAMP stack consists of the Linux operating system and the software applications Apache, MySQL/MariaDB, and PHP. These are installed together to host dynamic websites and web applications on a server. To install a LAMP stack:

Requirements

  • Before installing the LAMP stack, you have ensured that your server has sufficient hardware capacity.

  • You have opened port 80 in the firewall policy assigned to the server in the Cloud Panel.

  • You have opened port 80 in the internal firewall of AlmaLinux or Rocky Linux.

  • You have logged in to the server as root.

Note

To allow HTTP and HTTPS traffic through the internal firewall, enter the following commands:

Allow HTTP service

firewall-cmd --permanent --zone=public --add-service=http

Allow HTTPS service

firewall-cmd --permanent --zone=public --add-service=https

Apply changes

firewall-cmd --reload

To check whether the service has been successfully enabled, enter the following command:

firewall-cmd --zone=public --list-all

Install Apache

To install Apache, follow these steps:

  • To check whether an update is available, enter the command below:

    dnf update

  • To install Apache, enter the following command:

    dnf install httpd

    The following message will be displayed:

    Total download size: 2.3 M
    Installed size: 6.5 M
    Is this OK [y/N]:

  • Type [y] and press [Enter]. Apache will be installed.
  • To start Apache, enter the command below:

    systemctl start httpd.service

  • To check whether Apache has been successfully installed and started, enter the following command:

    systemctl status httpd

  • To exit the status display, press the [q] key.
  • To generate a test page, enter the following command:

    echo "Welcome to this site!" > /var/www/html/index.html

  • To check whether Apache has been successfully installed and started, enter your server’s public IP address in your web browser in the following format:
    http://YOUR.SERVERS.IP.ADDRESS
    If a test page is displayed, the installation of Apache was successful.
  • To ensure that Apache also restarts automatically when the server is rebooted, enter the following command:

    systemctl enable httpd.service

Install MariaDB

  • To install MariaDB, enter the following command:

    dnf install mariadb-server mariadb

    The following message will be displayed:

    Total download size: 26 M
    . Installed size: 135 M
    . Is this OK [y/N]:

  • Type [y] and press [Enter]. MariaDB will be installed.
  • To start MariaDB, enter the following command:

    systemctl start mariadb

  • To run a security script that removes some potentially dangerous default settings and restricts access to the database system, enter the following command:

    mysql_secure_installation

  • After entering the command, you will be prompted for a password. As you have not yet defined a password for MariaDB, you can skip this step. To do so, press [Enter]. The following message will then be displayed:

    Setting the root password or using the unix_socket ensures that nobody can log into the MariaDB root user without the proper authorization.
    You already have your root account protected, so you can safely answer 'n'.
    Switch to unix_socket authentication [Y/n]

  • Enter [n] and press [Enter]. The following prompt will then appear:

    Change the root password? [Y/n]

  • Type [y] and press [Enter].
  • Enter a new root password, repeat it, and then press [Enter]. The following message will appear:

    By default, a MariaDB installation includes an anonymous user, allowing anyone to log into MariaDB without needing a user account created for them. This is intended solely for testing purposes and to ensure the installation proceeds more smoothly. You should remove this user before moving to a production environment.

    Remove anonymous users? [Y/n]

  • To remove anonymous users, type [y] and press [Enter]. The following message will then appear:

    Disallow remote root login? [Y/n]

  • Type [y] and press [Enter]. The following message will then be displayed:

    Remove the test database and access to it?

  • Type [y] and press [Enter]. The following message is displayed:

    Reload privilege tables now?

  • To reload the privilege tables, type [y]. Then press [Enter] to confirm your entry.
  • To enable MariaDB at boot time, enter the following command:

    systemctl enable mariadb.service

Install PHP

To install the PHP scripting language, complete the following:

  • To install PHP, the MySQL driver, the PDO_MySQL driver, the GD library, and the PHP module for multibyte strings, enter the following command:

    dnf install php php-mysqlnd php-pdo php-gd php-mbstring

    The following message will be displayed:

    Total download size: 9.3 M
    Installed size: 44 M
    Is this OK [y/N]:

  • To continue with the installation, enter [y]. Then press [Enter].
  • To restart the Apache web server, enter the following command:

    systemctl restart httpd.service

Install PHP modules

  • To extend PHP’s functionality, you can install additional modules.
  • To view the available options for PHP modules and libraries, enter the following command:

    dnf search php-

  • To obtain detailed information about a PHP module, enter the command below:

    dnf info package_name

    Example:

    dnf info php-embedded.x86_64

  • To install the required PHP modules, enter the following command:

    dnf install package1 package2

    Example:

    dnf install php-cli.x86_64 php-devel.x86_64 php

  • To proceed with the installation, type [y]. Then press [Enter].

Test PHP

To check whether PHP has been installed correctly, create a script using the vi editor. This must be saved in the /var/www/html directory.

  • To create the script in the /var/www/html directory, enter the following command:

    vi /var/www/html/info.php

    The vi editor will open.

Notes

  • The vi editor has an insert mode and a command mode. You can enter insert mode by pressing the [i] key. In this mode, the characters you type are inserted into the text immediately. To switch to command mode, press the Esc key. When you are in command mode, your keystrokes are interpreted as commands.
  • vi cannot be exited whilst in insert mode. You should therefore always switch to command mode to exit vi.
  • Press [i] and enter the following PHP code:

    <?php phpinfo(); ?>

  • To enter command mode, press [ESC]. Then enter the command :wq to save the text and close the editor.
  • To check whether the contents of the PHP script are displayed, open the relevant URL in your web browser using the following format:
    http://YOUR.SERVER.IP.ADDRESS/info.php
  • To remove the displayed page, enter the following command:

    rm /var/www/html/info.php

    The following message will be displayed:

    rm: remove regular file '/var/www/html/info.php'?

  • Type y and press [Enter].