Installing the LAMP Stack on a Cloud Server with CentOS 7 or CentOS Stream 8

This article explains how to install a LAMP stack on a Cloud Server, Virtual Private Server, or Dedicated Server running CentOS 7 or CentOS Stream 8.

A LAMP stack consists of the Linux operating system and the Apache, MySQL/MariaDB, 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 CentOS 7 or CentOS Stream 8 on your server.

Installing Apache

Follow these steps to install Apache:

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

  • To install Apache, type the following command:
    yum install httpd The following message is displayed:

    CentOS Stream 8Total download size: 2.1 M
    Installed size: 5.6 M
    Is this ok [y/N]:

    CentOS 7
    Total download size: 3.0 M
    Installed size: 10 M
    Is this ok [y/d/N]:

  • Type [y] and press [Enter].

    Apache is now installed.

  • To start Apache, type the command below:
    systemctl start httpd.service

  • To check if Apache was successfully installed and started, type the following command.
    systemctl status httpd

  • To exit the status display, press the q key.

  • To create a test page, enter the following command:
    echo "Welcome" > /var/www/html/index.html

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

    http://123.123.123.123

    If you see a test page, the installation of Apache was successful.

  • To ensure that Apache also restarts automatically when you restart the server, enter the following command:
    systemctl enable httpd.service

Installing MariaDB

  • To install MariaDB, type the following command:
    yum install mariadb-server mariadbThe following message will then be displayed:Total download size: 31 M
    Installed size: 156 M
    Is this ok [y/N]:

  • Type [y] and press [Enter].

    MariaDB is now installed.

  • To start MariaDB, type the following command:
    systemctl start mariadb

  • To run a security script that removes some dangerous defaults 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. Since you have not yet defined a password for MariaDB, you can skip this point. To do this, press Enter. You will then be asked if you want to set the root password.

  • Type [y] and press [Enter].

  • Type 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, 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 displayed:
    Reload privilege tables now?

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

  • To enable MariaDB at boot time, type the following command:
    systemctl enable mariadb.service

Installing PHP

To install the PHP scripting language, do the following:

  • To install PHP, the MySQL driver, the POD_MySQL driver, the GD library, and the PHP module for multibyte strings, enter the following command:yum install php php-mysqlnd php-pdo php-gd php-mbstring

  • The following message is displayed:Total download size: 12 M
    Installed size: 40 M
    Is this ok [y/N]:

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

  • To restart the Apache web server, type the following command:
    systemctl restart httpd.service

Installing PHP Modules

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

To view the available options for PHP modules and libraries, enter the following command:
yum search php-
To get detailed information about a PHP module, enter the command below:

yum info package_name

Example:

yum info php-embedded.x86_64

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

yum install package1 package2

Example:

yum install php-cli.x86_64 php-devel.x86_64 php-dba.x86_64

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

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.

  • To create the script in the /var/www/html directory, enter the following command:
    vi /var/www/html/info.php
    The vi editor opens.

Notes
  • The vi editor has an insert mode and a command mode. You can enter the insert mode by pressing the i key. In this mode, the entered characters are immediately inserted into the text. To enter the command mode, press the ESC key afterwards. When 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 type 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, open your corresponding URL in your web browser using the following format:

    http://123.123.123.123/info.php

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

  • Type [y] and press [Enter].