How to create a LAMP (Linux, Apache, MoiraDB, PHP) environment

Posted in php, blog on November 4, 2020 by Henk Verlinde ‐ 4 min read

How to create a LAMP (Linux, Apache, MoiraDB, PHP) environment

Introduction

I created a LAMP environment that is a little difficult to create from scratch, so I will leave it as a memorandum.

Environment

  1. CentOS 7

Check if Apache is installed with the following command.

[user@123-45-67-89 ~]# httpd -v
bash: httpd: command not found

Install it if not.

[user@123-45-67-89 ~] # sudo yum install httpd
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: ty1.mirror.newmediaexpress.com
 * extras: ty1.mirror.newmediaexpress.com
 * updates: ty1.mirror.newmediaexpress.com
~~~~~~
~~~~~~
Complete!

Check if Apache is installed.

[user@123-45-67-89 ~]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Apr 24 2019 13:45:48

[user@123-45-67-89 ~]# service httpd status
Redirecting to /bin/systemctl status httpd.service
● httpd.service—The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)

Start it if it is not running.

Start it if it is not started
[user@123-45-67-89 ~]# sudo service httpd start
Redirecting to /bin/systemctl start httpd.service

Check if it is started.

[user@123-45-67-89 ~]# service httpd status
● httpd.service—The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-05-17 02:28:43 UTC; 21s ago
     Docs: man:httpd(8)
           man:apachectl(8)
~~~~~~~
~~~~~~~
Hint: Some lines were ellipsized, use -l to show in full.

Add a setting to start Apache when the server starts.

[user@123-45-67-89 ~]# sudo systemctl enable httpd

Access the server IP and confirm that the following screen is displayed.

If the above screen is not displayed, FireWall may be interfering.

Execute the following command to check whether the firewall is set.

[user@123-45-67-89 ~]# sudo firewall-cmd --state
running

If [runnnig] is displayed, hit the following command.

[user@123-45-67-89 ~]# sudo firewall-cmd --zone=public --add-service=http --permanent
success
[user@123-45-67-89 ~]# sudo firewall-cmd --reload

If there is no problem, try accessing again.

Install PHP

The standard yum repository for CentOS 7 only provides up to PHP5.4. If you want to install the latest PHP version, please refer to the article below. [Install PHP7 on CentOS7]

If you don’t mind PHP5.4, install PHP here Execute the command below to check if PHP is installed.

[user@123-45-67-89 ~]# php -v
bash: php: command not found

If not installed, run the following to install PHP.

[centos@ip-172-31-38-77 ~]$ sudo yum install php
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ty1.mirror.newmediaexpress.com
 * extras: ty1.mirror.newmediaexpress.com
 * updates: ty1.mirror.newmediaexpress.com
~~~~~~~~
~~~~~~~~
Complete!

Restart Apache.

[user@123-45-67-89 ~]# service httpd restart

Create and display a test page. Before that, open httpd.conf and check the document root.

[user@123-45-67-89 ~]# cd /etc/httpd/conf
[user@123-45-67-89conf]#ls
httpd.conf magic
[user@123-45-67-89 conf]# sudo vi httpd.conf

Check the DocumentRoot. It’s probably set to “/var/www/html” by default.

# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

Go to DocumentRoot.

[user@123-45-67-89conf]# cd /var/www/html
[user@123-45-67-89 html]# sudo vi phpinfo.php
<!--phpinfo.php -->
<?php
phpinfo();
?>

Once you have placed the above source, access with [IP address]/phpinfo.php and confirm that the following screen is displayed.

PHP configuration is now complete.

Configure MariaDB

[user@123-45-67-89 ~]# mysql --version

Install MariaDB.

[user@123-45-67-89 ~]# sudo yum install mariadb mariadb-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ty1.mirror.newmediaexpress.com
 * extras: ty1.mirror.newmediaexpress.com
 * updates: ty1.mirror.newmediaexpress.com
~~~~~~~~~
~~~~~~~~~
Complete!

Check if MariaDB is installed.

[user@123-45-67-89 ~]# mysql --version
mysql Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1

Check if MariaDB is running.

[user@123-45-67-89 ~]# service mariadb status
Redirecting to /bin/systemctl status mariadb.service
● mariadb.service—MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

Perhaps [Active: incative (dead)] is displayed and you can confirm that MariaDB has not been started. Execute the following command to start MariaDB.

[user@123-45-67-89 ~]# sudo service mariadb start
Redirecting to /bin/systemctl start mariadb.service

Let’s check again.

[user@123-45-67-89 ~]# service mariadb status
Redirecting to /bin/systemctl status mariadb.service
● mariadb.service—MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
~~~~~~~~
~~~~~~~~
Hint: Some lines were ellipsized, use -l to show in full.

MoiraDB is now installed. Let’s create a table to see if it works properly.

Execute the following command and log in to the sql server. You will be asked for a password, just press Enter.

[user@123-45-67-89 ~]# mysql -u root -p
Enter password: [Press Enter]

Create DB.

MariaDB [(none)]>create database db_name;
Query OK, 1 row affected (0.00 sec)

Check if it was created.

MariaDB [(none)]>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db_name |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

If there is no problem so far, the LAMP environment will be completed. that’s all. good job for today.