Installing the Lamp Stack on a Cloud Server with Centos 7 Minimal Installation

This article explains how to install a LAMP stack on a Cloud Server running CentOS 7.

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

Requirements

  • Before installing the LAMP stack, you have ensured that your server has sufficient hardware capacity.

  • You have CentOS 7 installed on your server.

Installing Apache

Follow these steps to install Apache:

  • To check if an update is available for CentOS 7, enter the following command:
    yum update

  • To install Apache, type the following command:
    yum install httpd The following message is displayed:
    Total download size: 3.0 M Installed
    size: 10 M Is
    this ok [y/d/N]:

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

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

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

  • To verify that Apache has been 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 installation of Apache was successful.

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

Installing MariaDB

  • To install MariaDB, type the following command:
    yum install mariadb-server mariadb
    The following message will be displayed:
    Total download size: 21 M
    Installed size: 110 M
    Is this ok [y/d/N]:

  • Enter [y] and press [Enter], and MariaDB will be installed.

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

  • 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 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.

  • Enter [y] and press [Enter].

  • Enter a new root password, repeat it, and then press [Enter].

    The following message will be displayed:
    By default, a MariaDB installation has an anonymous user, allowing anyone to
    log into MariaDB without having 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], and the following message will be displayed:
    Disallow root login remotely? [Y/n]

  • Enter [y] and press [Enter], and the following message appears:
    Remove test database and access to it?

  • Enter [y] and press [Enter], and the following message appears:
    Reload privilege tables now?

  • To reload the authorisation tables, enter [y]. To confirm the entry, press [Enter].

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

Installing PHP

To install the PHP scripting language, proceed as follows:

  • To install the PHP-MySQL package, type the following command:
    yum install php php-mysql

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

  • To continue the installation, enter [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, type 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, enter [y]. Then press [Enter].

Testing PHP

To test if PHP is installed properly, create a script with the editor. This must be stored in the directory /var/www/html.

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

Note

The editor vi 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, then press the ESC key. If you use the command mode, your keyboard input is interpreted as a command.

Please Note

Vi cannot be terminated in insert mode. Therefore, you will need to enter command mode to exit.

  • Press [i] and enter the following PHP code:
    <?php phpinfo(); ?>

  • To enter the command mode, press [ESC]. Then enter the command :wq 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, enter the following command:
    rm /var/www/html/info.php