For those who seek help in different areas of software and hardware platform.

How To Set Up a LAMP Stack (Apache, MariaDB, PHP) on CentOS/RHEL 8

This guide will walk you through the steps to set up a LAMP Stack on CentOS/RHEL 8. LAMP is a combination of Linux operating system, the Apache HTTP Server, the MySQL/MariaDB relational database management system, and the PHP programming language.

Note: With CentOS 8 release, yum command has been replaced with dnf and in near future yum package manager will be discontinued. It is now recommended to use dnf for installing packages but if you still wish to use yum you can use it.

Prerequisites

You will need one (physical or virtual) with CentOS/RHEL 8 minimal installed having sudo non-root user privileges

Set Timezone

You can correct your server timezone by typing the following command but make sure you replace Asia/Karachi with yours
sudo timedatectl set-timezone Asia/Karachi

Adding EPEL Repository

It is always recommended to add extra packages for enterprise Linux on your fresh CentOS/RHEL server:
sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo dnf config-manager --set-enabled PowerTools
sudo dnf -y update
If you are on RHEL 8, execute the following command as well:
sudo ARCH=$( /bin/arch )
sudo subscription-manager repos --enable "codeready-builder-for-rhel-8-${ARCH}-rpms"
sudo dnf config-manager --set-enabled PowerTools

Installing PHP

You can install latest PHP and its commonly used extensions with the below command:
sudo dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
sudo dnf -y install php php-common php-process php-xmlrpc php-xml php-soap php-snmp php-recode php-bcmath php-cli php-dba php-dbg php-mbstring php-odbc php-pecl-apcu-devel php-pecl-zip php-pgsql php-pecl-apcu php-pear php-pdo php-opcache php-devel php-embedded php-enchant php-gd php-fpm php-gmp php-intl php-ldap php-json php-mysqlnd php-pdo php-gd php-mbstring zip unzip tar wget

Installing Apache

Type the following command to install Apache web server:
sudo dnf -y install httpd httpd-devel mod_ssl openssl
Now starting Apache service and making persistent even when system reboots:
sudo systemctl start httpd
sudo systemctl enable httpd

Installing MariaDB

If you would like to user MariaDB as your database, type the below command to install:
sudo dnf -y install mariadb-server
Starting MariaDB service and making it persistent even when system reboots:
sudo systemctl start mariadb
sudo systemctl enable mariadb
Or if you still wish to use MySQL as your database, type the below command to install:
sudo dnf -y install mysql-server mysql

Securing MariaDB/MySQL

By default MariaDB/MySQL has no root password and anyone can intrude into your database, so run the below script and follow on screen instruction to secure it:
sudo mysql_secure_installation
Response to the following prompts on your server like below:
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!

Testing Apache, PHP

Now create info.php page under /var/www/html to test your Apache and PHP functionality:
sudo vi /var/www/html/info.php
Add following code into it:
<?php
phpinfo();
?>
Save and close file.

Now disable default welcome page from Apache like below:
sudo mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/bkpwelcome
Reload Apache to take changes into effect:
sudo systemctl reload httpd
Add firewall rules to allow HTTP traffic:
sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --reload
Open up your preferred web browser and navigate to http://your_server_name_or_ip/info.php


Enabling SSL

It is always recommended to access your web services over secure protocol HTTPS. As you have already installed mod_ssl while installing Apache in earlier steps, you just need to add the below firewall rules to allow HTTPS traffic over default port 443 and deny access to HTTP on port 80.
sudo firewall-cmd --zone=public --permanent --remove-service=http
sudo firewall-cmd --zone=public --permanent --add-service=https
sudo firewall-cmd --reload

Wrapping up

You have successfully set up a foundation for serving PHP websites and applications to your visitors, using Apache as web server and MariaDB/MySQL as database server.
Share:

0 comments:

Post a Comment

Comments with links will not be published.

Video Tutorials