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

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 made sure that your server has sufficient hardware capacity before installing the LAMP stack.

  • You installed CentOS Stream 9 on your server.

Installing Apache

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

    yum update

  • To install Apache, enter the following command:

    yum install httpd


    The following message is displayed:

    Total download size: 2.3 M
    Installed size: 6.5 M
    Is this ok [y/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 generate a test page, enter the following command:

    echo "Welcome to this site!" > /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://111.222.333.444

    If you are presented with 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, enter the following command:

    yum install mariadb-server mariadb

    The following message is displayed:

    Total download size: 26 M
    Installed size: 135 M
    Is this ok [y/N]:

  • Type [y] and press [Enter].

    MariaDB is 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.

    The following message is then displayed:

    Switch to unix_socket authentication [Y/n]:

  • Type [n] and press [Enter].

    You are then asked if you want to change 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 the 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: 9.3 M
    Installed size: 44 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, type 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 will then open.

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 enter the following PHP code:

    <?php phpinfo(); ?>

  • To enter 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 the corresponding URL of your server in a web browser in the following format:

    http://123.123.123.123/info.php

  • To remove the displayed page, type the following command:

    rm /var/www/html/info.php

    The following message is displayed:

    rm: remove regular file '/var/www/html/info.php'?

  • Type y and press Enter.