Valid for Cloud Servers, migrated Cloud Servers, VPS, and Dedicated Servers, which are managed in the Cloud Panel.

This article explains how to install a LAMP stack on a ###VPS+###, Cloud Server, migrated Cloud Server, ###VPS+###, or a Dedicated Server with Debian 11, Debian 12, Ubuntu 18.04, Ubuntu 20.04, Ubuntu 22.04, or Ubuntu 24.04. The LAMP stack for Debian consists of the Linux operating system and the Apache, MariaDB, and PHP software. These are installed together to host dynamic websites and web applications on a server.

How to install a LAMP stack:

Requirements

Install Apache

To install Apache, complete the following:

  • Check if a repo and software update is available with the following command:

    root@ubuntu:~# sudo apt update && sudo apt upgrade -y

  • Enter the following command to install Apache:

    root@ubuntu:~# apt install apache2

    The installation is started. The following message is displayed during the installation:

    Installing:
    apache2
    Installing dependencies:
    apache2-bin libaprutil1-dbd-sqlite3 liblua5.4-0 ssl-cert
    apache2-data libaprutil1-ldap libperl5.40
    apache2-utils libaprutil1t64 perl
    libapr1t64 libgdbm-compat4t64 perl-modules-5.40

    Suggested packages:
    apache2-doc perl-doc
    apache2-suexec-pristine libterm-readline-gnu-perl
    | apache2-suexec-custom | libterm-readline-perl-perl
    ufw make
    www-browser libtap-harness-archive-perl

    Summary:
    upgrading: 0, Installing: 14, Removing: 0, Not Upgrading: 0
    Download size: 10.1 MB
    Space needed: 60.0 MB / 242 GB available

    Continue? [Y/n]

Note

Under Ubuntu 24.04, the exact version numbers and package names (e.g. t64 suffixes) differ slightly.

  • Enter [y] and press [Enter]. Apache will be installed.
  • To check whether Apache has been successfully installed and started, enter the public IP address of your server in the following format in your web browser:

    http://YOUR-SERVER-IP-ADDRESS/

    If a test page is displayed, Apache has been successfully installed.

Install MariaDB

  • Enter the following command to install MariaDB:

    root@localhost:~# apt install mariadb-server -y

    MariaDB will be installed.

  • Enter the following command to start MariaDB:

    root@localhost:~# systemctl start mariadb

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

    root@localhost:~# mariadb-secure-installation

    After entering the command, you will be asked for a password. As you have not yet defined a password for MariaDB, you can skip this point. To do this, press Enter. The following message will then be displayed:

    Switch to unix_socket authentication [Y/n]

  • Enter [n] and press [Enter]. You will then be asked whether you want to change the root password.
  • Enter [y] and press [Enter].
  • Enter a new root password, repeat it and then press [Enter]. The following message is displayed:

    By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. you should remove them before moving into a production environment.
    Remove anonymous users? [Y/n]

  • To remove anonymous users, enter [y] and press [Enter].The following message is then displayed:

    Disallow root login remotely? [Y/n]

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

    Remove test database and access to it?

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

    Reload privilege tables now?

  • To reload the privilege tables, enter [y]. To confirm the entry, press [Enter].
  • To activate the automatic startup of MariaDB when the server is started, enter the following command:

    root@localhost:~# systemctl enable mariadb.service

Install PHP

To install the PHP scripting language, complete the following:

  • Enter the following command to install PHP, the Apache module for PHP, and the MySQL connector:

    root@localhost:~# apt install php libapache2-mod-php php-mysql

    The following message is displayed:

    After this operation, 21.8 MB of additional disk space will be used.
    Do you want to continue? [Y/n]

  • To continue the installation, enter [y]. Then press [Enter].

Adjust the settings in the dir.conf file

If a user does not enter a specific page in the URL, Apache first searches for the start page with the name index.html by default. To configure Apache so that the index.php file is preferred in this search, complete the following:

  • To open the dir.conf file with the vi editor, enter the following command:

    vi /etc/apache2/mods-enabled/dir.conf

Notes

  • The vi editor has an insert mode and a command mode. You can call up insert mode with the [i] key. In this mode, the characters entered are immediately inserted into the text. To call up command mode, press [ESC]. If you use command mode, your keyboard input is interpreted as a command.
  • vi cannot be terminated in insert mode. Therefore, always enter command mode to exit vi.
  • Press [i] and adjust the following entry:

    <IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
    </IfModule>

  • Move the index.php entry so that it is behind the DirectoryIndex entry.
  • Press [ESC] to enter the command mode. Then enter the command :wq to save the text and close the editor.
  • You must restart Apache for these changes to be applied. To restart Apache, enter the following command:

    root@ubuntu:~# systemctl restart apache2

Install PHP modules

To extend the functionality of PHP, you can install additional modules.

To display the available options for PHP modules and libraries, pass the results of apt search to less. Less is a pager that allows you to display text files in the command line. You can also use less to move around in documents. To do this, enter the following command:

root@ubuntu:~# apt search php- | less

To scroll up or down, use the arrow keys. To exit less, press [q].


To get detailed information about a PHP module, enter the following command:

root@ubuntu:~# apt show package_name

Example:

root@localhost:~# apt show php-cli


To install the desired PHP modules, enter the command below:

root@ubuntu:~# apt install package1 package2

Example:

root@localhost:~# apt install php-cli php-composer-ca-bundle

Test PHP

To test whether PHP has been installed correctly, create a script with the editor. This must be saved in the /var/www/html directory. Complete the following to create the script and test PHP:

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

    vi /var/www/html/info.php

    The vi editor opens.

  • Press the [i] key and then enter the following PHP code:

    <?php
    phpinfo();
    ?>

  • To call up the command mode, press [ESC]. Then enter the command :wq to save the text and close the editor.
  • To test whether the contents of the PHP script are displayed, open the corresponding URL in the following format in your web browser:

    http://YOUR-SERVER-IP-ADDRESS/info.php

  • To remove the displayed page again, enter the following command:

    rm /var/www/html/info.php

Your LAMP stack is now ready for use. Next, you could install a web application such as WordPress, or create your first PHP site.