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

This article explains how to install a LAMP stack on a Cloud Server, migrated Cloud Server, VPS, VPS+, or a Dedicated Server running Debian 13, Ubuntu 24.04, or Ubuntu 26.04.

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

Requirements

Before installing the LAMP stack, make sure that:

  • Your server has sufficient hardware resources.
  • Your server meets the system requirements for MySQL and Apache

Install Apache

To install Apache, complete the following:

  • To check whether updates are available, enter the following commands:

    sudo apt update
    sudo apt upgrade

  • To install Apache, enter the following command:

    sudo apt install apache2

    The installation will begin. During installation, the following message is displayed:

    The following NEW packages will be installed:
    apache2 apache2-bin apache2-data apache2-utils bzip2 libapr1 libaprutil1
    libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0 mailcap mime-support
    ssl-cert
    0 upgraded, 13 newly installed, 0 to remove and 0 not upgraded.
    Need to get 2,135 kB of archives.
    After this operation, 8,486 kB of additional disk space will be used.
    Do you want to continue? [Y/n]

  • Type [y] and press Enter. Apache will be installed.

  • To verify that Apache was installed and started successfully, open your server’s public IP address in a web browser using the following format:

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

    If a test page is displayed, Apache was installed successfully 

Install MySQL

  • To install MySQL, enter the following command:

    sudo apt install mysql-server

    Once you have entered the command, the following message will appear:

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

  • Type [y] and press the Enter key. MySQL will be installed.

  • Log in to MySQL. To do this, enter the following command:

    sudo mysql

  • Enter the following command and replace MY_NEW_PASSWORD with the password you want to use:

    mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'MY_NEW_PASSWORD';

    If, after entering the command, you see the error message ERROR 1524 (HY000): Plugin 'mysql_native_password' is not loaded, modify the following command and then enter it:

    ALTER USER 'root'@'localhost' IDENTIFIED BY 'MY_NEW_PASSWORD';

    Then enter the following command:

    FLUSH PRIVILEGES;

  • To exit MySQL, enter the following command:

    mysql>exit

  • To run a security script that removes insecure default settings and restricts access to the database system, enter:

    mysql_secure_installation

    After entering the command, the following message will be displayed:

    Securing the MySQL server deployment.

    Connecting to MySQL using a blank password.

  • Enter the root password for the MySQL database that you have set. The following message will then be displayed:

    The VALIDATE PASSWORD PLUGIN can be used to test passwords
    and improve security. It checks the strength of password
    and allows users to set only those passwords that are
    secure enough. Would you like to set up the VALIDATE PASSWORD plugin?
    Press y|Y for Yes, any other key for No:

    If you enable the VALIDATE PASSWORD plugin, MySQL will reject passwords that do not meet the specified criteria. This can cause problems if you use a weak password together with software that automatically configures MySQL user credentials. For this reason, the article recommends not enabling this feature. You should still always use a secure password.

  • To skip setting up the Validate Password plugin, press the Enter key. The following message will then be displayed:

    Change the password for root? ((Press y|Y for Yes, any other key for No) :

  • To keep the password unchanged, press the Enter key. The following message will appear:

    By default, a MySQL installation has an anonymous user,
    allowing anyone to log into MySQL 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, type [y] and press Enter. The following message will then be displayed:

    Disallow root login remotely? [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 will then appear:

    Reload privilege tables now?

  • To reload the MySQL privilege tables, type [y]. To confirm your entry, press the Enter key.

Install PHP

To install the PHP scripting language, complete the following:

  • To install PHP, the Apache module for PHP, and the MySQL integration, enter the following command:

    apt install php libapache2-mod-php php-mysql

    The following message will be displayed:

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

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

 

Adjust settings in the dir.conf file

If a user does not specify a particular page in the URL, Apache searches by default for a start page named index.html first. To configure Apache so that it prefers index.php during this search, complete the following::

  • To open the dir.conf file using 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 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 [ESC] afterwards. 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 edit 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 appears immediately after DirectoryIndex, for example:

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

  • To return to command mode, press [ESC]. Then enter the command :wq to save the text and close the editor.

  • You must restart Apache for these changes to take effect. To restart Apache, enter the following command:

    sudo systemctl restart apache2

Install PHP modules

To extend PHP’s functionality, you can install additional modules.

To display the available PHP modules and libraries, pipe the results of apt search to less. Less is a pager that allows you to view text files from the command line. Furthermore, you can navigate freely through documents using less. To do this, enter the following command:

apt search php- | less

To scroll up or down, use the arrow keys. To exit less, press [Q]. To obtain detailed information about a PHP module, enter the following command:

apt show package_name

Example:

apt show php-xml

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

sudo apt install package1 package2

Example:

sudo apt install php-xml php-cli

Test PHP

To test whether PHP was installed correctly, create a script and save it 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.

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

    <?php
    phpinfo();
    ?>

  • To return to 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