This article explains how to install a LAMP stack on a Cloud Server, Virtual Private Server, or Dedicated Server running Ubuntu 18.04, Ubuntu 20.04, or Ubuntu 22.04.

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

Prerequisites
  • You have ensured that your server has sufficient hardware capacity before installing the LAMP stack.

  • You have installed Ubuntu 18.04, Ubuntu 20.04, or Ubuntu 22.04 on your server.

Installing Apache

Follow these steps to install Apache:

  • To check if an update is available, enter the following commands:

    root@localhost:~# apt update
    root@localhost:~# apt upgrade

  • To install Apache, enter the following command:

    root@localhost:~# apt install apache2

    The installation will start. The following message will be displayed during the installation:

    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 is now installed.

  • To verify that Apache was successfully installed and started, type the public IP address of your server in the following format in your web browser:

    http://123.123.123.123

    If you are presented with a test page, the installation of Apache was successful.

Installing MySQL

  • To install MySQL, enter the following command:

    apt install mysql-server

    After entering the command, the following message is displayed:

    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 [Enter].

    MySQL is installed.

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

    sudo mysql

  • Enter the following command and replace the placeholder MY_NEW_PASSWORD with the password you want.

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

  • To exit MySQL, enter the following command:

    mysql>exit

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

    mysql_secure_installation

    After you type the command, the following message is displayed:

    Securing the MySQL server deployment.

    Enter password for user root:

  • Enter the root password for the MySQL database that you have set.

    The following message is then displayed:

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

    If you enable the VALIDATE PASSWORD PLUGIN, passwords that do not match the specified criteria will be rejected by MySQL with an error. This can cause problems if you use a weak password in conjunction with software that automatically configures MySQL user data. For this reason, we recommend that you do not set up this feature. However, always use a strong password.

  • To skip setting up the Validate Password plugin, press [Enter].

    The following message is then displayed:

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

  • To not change the password, press [Enter].

    The following message is displayed:

    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 is then displayed:

    Disallow root login remotely? [Y/n]

  • Type [y] and press [Enter].

    The following message is then displayed:

    Remove test database and access to it?

  • Type [ y] and press [Enter].

    The following message is then displayed:

    Reload privilege tables now?

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

Installing PHP

To install the PHP scripting language, do the following:

  • To install the PHP-MySQL package, enter the following command:

    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, type [y]. Then press [Enter].

Adjust settings in the dir.conf file

By default, if a user does not specify a specific page in the URL, Apache first searches for the home page named index.html. To configure Apache to prefer the index.php file in this search, do 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 enter the insert mode with the key [i] key. In this mode, the characters entered are immediately inserted into the text. To enter the command mode, press [ESC] afterwards. When you use the command mode, your keyboard inputs are 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:

    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

    Move the index.php entry so that it is after the DirectoryIndex entry.

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

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

    systemctl restart apache2

Installing PHP Modules

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

To view 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 on the command line. In addition, you can use less to move around in documents at will. To do this, type the following command:

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, type the following command:

apt show package_name

Example:

apt show php-codesniffer

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

apt install package1 package2

Example:

apt install php-codesniffer php-cli

Testing PHP

To test if PHP was installed properly, create a script with the editor. This must be saved in the /var/www/html directory. Follow these steps 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 enter the command mode, press [ESC]. Then enter the :wq command to save the text and close the editor.

  • To test whether the contents of the PHP script are displayed, call the corresponding URL in your web browser in the following format:

    http://123.123.123.123/info.php

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

    rm /var/www/html/info