How To Install LAMP on CentOS 7/RHEL 7

How To Install Linux, Apache, MySQL (MariaDB), PHP (LAMP) stack On CentOS 7/RHEL 7

Introduction

LAMP stack is a group of open source softwares which are installed together to host dynamic websites and web apps on a server. LAMP means – Linux operating system with Apache web server. The data would be stored in a MySQL database and the  content would be processed by PHP.

Note: A non-root user is required on your server.

Step 1. Install Apache

If you already have a CentOS/RHEL operating system running, the first step to setup LAMP stack would be to install the web server.

1. Install Apache:

[[email protected] ~]$ sudo yum install httpd

When prompted, confirm that you are executing the command with sudo privileges.

Installed:
  httpd.x86_64 0:2.4.6-93.el7.centos                                                                                                                                     
Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7           apr-util.x86_64 0:1.5.2-6.el7           httpd-tools.x86_64 0:2.4.6-93.el7.centos           mailcap.noarch 0:2.1.41-2.el7          
Complete!
[[email protected] ~]$

2. Start Apache:

[[email protected] ~]$ sudo systemctl start httpd
[[email protected] ~]$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2020-08-09 17:56:31 IST; 7s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 16682 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─16682 /usr/sbin/httpd -DFOREGROUND
├─16683 /usr/sbin/httpd -DFOREGROUND
├─16684 /usr/sbin/httpd -DFOREGROUND
├─16685 /usr/sbin/httpd -DFOREGROUND
├─16686 /usr/sbin/httpd -DFOREGROUND
└─16687 /usr/sbin/httpd -DFOREGROUND

Aug 09 17:56:10 technicalmint systemd[1]: Starting The Apache HTTP Server...
Aug 09 17:56:21 technicalmint httpd[16682]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::3335:de3b:718...is message
Aug 09 17:56:31 technicalmint systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[[email protected] ~]$ 

3. Check if the service is running by going to your server’s public IP address. The browser should display the test CentOS/RHEL Apache web page:

http://your_server_IP_address/

 

4. Set up Apache to start at boot:

[[email protected] ~]$ sudo systemctl enable httpd
[sudo] password for technicalmint:
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[[email protected] ~]$

Step 2. Install MySQL (MariaDB)

MariaDB is a forked version of MySQL. It is an open source software application. We will need MariaDB to store information for our website. We can install MariaDB together with required additional packages on our CentOS/RHEL server:

[[email protected] ~]$ sudo yum install mariadb-server mariadb

Installed:
mariadb.x86_64 1:5.5.65-1.el7 mariadb-server.x86_64 1:5.5.65-1.el7

Dependency Installed:
perl.x86_64 4:5.16.3-295.el7 perl-Carp.noarch 0:1.26-244.el7 perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7
perl-DBD-MySQL.x86_64 0:4.023-6.el7 perl-DBI.x86_64 0:1.627-4.el7 perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-Encode.x86_64 0:2.51-7.el7
perl-Exporter.noarch 0:5.68-3.el7 perl-File-Path.noarch 0:2.09-2.el7 perl-File-Temp.noarch 0:0.23.01-3.el7 perl-Filter.x86_64 0:1.49-3.el7
perl-Getopt-Long.noarch 0:2.40-3.el7 perl-HTTP-Tiny.noarch 0:0.033-3.el7 perl-IO-Compress.noarch 0:2.061-2.el7 perl-Net-Daemon.noarch 0:0.48-5.el7
perl-PathTools.x86_64 0:3.40-5.el7 perl-PlRPC.noarch 0:0.2020-14.el7 perl-Pod-Escapes.noarch 1:1.04-295.el7 perl-Pod-Perldoc.noarch 0:3.20-4.el7
perl-Pod-Simple.noarch 1:3.28-4.el7 perl-Pod-Usage.noarch 0:1.63-3.el7 perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 perl-Socket.x86_64 0:2.010-5.el7
perl-Storable.x86_64 0:2.45-3.el7 perl-Text-ParseWords.noarch 0:3.29-4.el7 perl-Time-HiRes.x86_64 4:1.9725-3.el7 perl-Time-Local.noarch 0:1.2300-2.el7
perl-constant.noarch 0:1.27-2.el7 perl-libs.x86_64 4:5.16.3-295.el7 perl-macros.x86_64 4:5.16.3-295.el7 perl-parent.noarch 1:0.225-244.el7
perl-podlators.noarch 0:2.5.1-3.el7 perl-threads.x86_64 0:1.87-4.el7 perl-threads-shared.x86_64 0:1.43-6.el7

Dependency Updated:
mariadb-libs.x86_64 1:5.5.65-1.el7

Complete!
[[email protected] ~]$

Now that the MariaDB installation was completed successfully, we can run the command below to start the service.

[[email protected] ~]$ sudo systemctl start mariadb
[[email protected] ~]$ sudo systemctl status mariadb
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2020-08-09 18:25:32 IST; 6s ago
Process: 16977 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 16894 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 16976 (mysqld_safe)
CGroup: /system.slice/mariadb.service
├─16976 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─17138 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-fil...

Aug 09 18:25:30 technicalmint mariadb-prepare-db-dir[16894]: MySQL manual for more instructions.
Aug 09 18:25:30 technicalmint mariadb-prepare-db-dir[16894]: Please report any problems at http://mariadb.org/jira
Aug 09 18:25:30 technicalmint mariadb-prepare-db-dir[16894]: The latest information about MariaDB is available at http://mariadb.org/.
Aug 09 18:25:30 technicalmint mariadb-prepare-db-dir[16894]: You can find additional information about the MySQL part at:
Aug 09 18:25:30 technicalmint mariadb-prepare-db-dir[16894]: http://dev.mysql.com
Aug 09 18:25:30 technicalmint mariadb-prepare-db-dir[16894]: Consider joining MariaDB's strong and vibrant community:
Aug 09 18:25:30 technicalmint mariadb-prepare-db-dir[16894]: https://mariadb.org/get-involved/
Aug 09 18:25:30 technicalmint mysqld_safe[16976]: 200809 18:25:30 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Aug 09 18:25:30 technicalmint mysqld_safe[16976]: 200809 18:25:30 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Aug 09 18:25:32 technicalmint systemd[1]: Started MariaDB database server.
[[email protected] ~]$

You can use mysql_secure_installation script to secure database (set root password, disable remotely logon from root, remove test database and remove anonymous users).

[[email protected] ~]$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!


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] Y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[[email protected] ~]$

Step 3. Install PHP

PHP is a general-purpose server-side scripting language which works with HTML to display the content on your website. It connects to the MariaDB database management system.

We can install PHP and MySQL extension using the command below:

[[email protected] ~]$ sudo yum install php php-mysql


Installed:
php.x86_64 0:5.4.16-48.el7 php-mysql.x86_64 0:5.4.16-48.el7

Dependency Installed:
libzip.x86_64 0:0.10.1-8.el7 php-cli.x86_64 0:5.4.16-48.el7 php-common.x86_64 0:5.4.16-48.el7 php-pdo.x86_64 0:5.4.16-48.el7

Complete!
[[email protected] ~]$

We need to restart Apache web server one more time for it to work with PHP by typing:

[[email protected] ~]$ sudo systemctl restart httpd

Step 4. Test PHP Processing on your Web Server

We can create a basic PHP script to test our config. We will call the info.php script. In order for Apache to find the file, it must be saved to a specific directory called the “web root”.

In CentOS 7/RHEL 7, this directory is located at /var/www/html/. We can create the file there:

[[email protected] ~]$ sudo vi /var/www/html/info.php

Enter the following PHP code in the file and save it:

<?php phpinfo(); ?>

If you are running a firewall, run the following commands to allow HTTP and HTTPS traffic:

[[email protected] ~]$ sudo firewall-cmd --permanent --zone=public --add-service=http 
[[email protected] ~]$ sudo firewall-cmd --permanent --zone=public --add-service=https
[[email protected] ~]$ sudo firewall-cmd --reload

Now we can test if our web server can display the content generated by the PHP script we created. Just need visit the page in our web browser using our server’s public IP address again.

http://your_server_IP_address/info.php

 

That’s it in this article, thanks for reading it! Please share it across if you think it’s good and share your feedback in the comment section below.

 

Leave a Reply

Your email address will not be published. Required fields are marked *