Installing the LAMP Stack on a Cloud Server with a Ubuntu 18.04 Minimal Installation

This article explains how to install a LAMP stack on a Cloud Server with a Ubuntu 18.04 minimal installation.

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 one server.

How to install a LAMP stack:

Requirements
  • Before installing the LAMP stack, make sure that your server has sufficient hardware capacity.

  • You have installed the minimal version of the Ubuntu 18.04 operating system on your server.

Installing Apache

Proceed as follows to install Apache:

  • To check if an update is available for Ubuntu 18.04, type the following command:
    apt update

  • To install Apache, enter the following command:
    apt install apache2
    The installation will start. The following message is displayed during installation:
    The following NEW packages will be installed: apache2
    apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3
    libaprutil1-ldap ssl-cert 0
    upgraded, 9 newly installed, 0 to remove and 3 not upgraded.
    Need to get 1,622 kB of archives. After
    this operation, 6,561 kB of additional disk space will be used. Do
    you want to continue? [Y/n] y

  • Enter [y] and press [Enter].

    Apache will be installed.

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

    http://YOUR-SERVER-IP-ADDRESS 

    If you see a test page, the Apache installation 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 21.0 MB of archives. After
    this operation, 162 MB of additional disk space will be used. Do
    you want to continue? [Y/N]

  • Enter [y] and press [Enter].

    MySQL will be installed.

  • To execute a security script that removes some dangerous default settings and restricts access to the database system, enter the following command: mysql_secure_installation
    After entering the command, the following message is displayed:
    Securing the MySQL server deployment.

    Connecting to MySQL using a blank password.

    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, MySQL will reject passwords that do not match the specified criteria with an error. This can cause problems if you use a weak password in combination with software that automatically configures MySQL user data. For this reason, we recommend that you do not set up this function. However, always use a secure password.

  • To skip the Validate Password Plugin setup, press [Enter].

  • Enter a new root password, repeat it and then 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, enter [y] and press [Enter], and

    The following message is displayed:
    Disallow root login remotely? [Y/N]

  • Enter [y] and press [Enter].

    The following message is 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 MySQL privilege tables, type [y]. To confirm the entry, press [Enter].

Installing PHP

To install the PHP scripting language, proceed as follows:

  • 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, 18.0 MB of additional disk space will be used.
    Do you want to continue? [Y/N]

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

 

Customizing settings in the dir.conf file

If a user does not specify a specific page in the URL, Apache first searches for the home page named index.html by default. To configure Apache to give preference to the index.php file in this search, proceed as follows:

  • To open the dir.conf file with the vi editor, type the following command:
    vi /etc/apache2/mods-enabled/dir.conf

Note

The vi editor has an insert mode and a command mode. You can open the insert mode using the [I] key. In this mode, the characters entered will immediately be inserted in the text in this mode. To switch to command mode, press the [ESC] key. When you are in command mode, key presses will be understood as commands.

Note

vi cannot be exited 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 entry index.php so that it stands behind the entry DirectoryIndex.

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

  • To apply these changes, you must restart Apache. 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, filter the results from apt search through the less command. Less is an option that you can use to display text files in the command line. In addition, you can move around freely in documents with 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 get detailed information about a PHP module, type the following command:

apt show package_name
Example:

apt show php-codesniffer
To install the PHP modules you want, enter the command below:

apt install package1 package2
Example:

apt install php-codesniffer php-cli

Test PHP

To test if PHP has been installed correctly, create a script with the editor. This must be stored in the directory /var/www/html. Proceed as follows to create the script and test PHP:

  • To create the script in the /var/www/html directory, type 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 the [ESC] key. Then type 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 the following format in your web browser:

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

  • To remove the displayed page, type the following command:
    rm /var/www/html/info