How to Install LAMP Stack on CentOS 8 Using PHP?

03-Apr-2023

.

Admin

How to Install LAMP Stack on CentOS 8 Using PHP?

Hi Dev,

Install Linux, apache, MySQL, ,PHP or LAMP on the centOS 8; Through this tutorial, we will learn how to install and configure LAMP (Linux, Apache, MySQL, and PHP) on CentOS 8.

Step 1: Install Apache


The Apache web server packages are available under the default AppStream repository on CentOS 8. You just need to update the DNF cache and install packages using the following commands.

sudo dnf update

sudo dnf install httpd httpd-tools

After installation, enable the httpd service and start.

sudo systemctl enable httpd.service

sudo systemctl start httpd.service

Now check the Apache service status:

Output:

httpd.service - The Apache HTTP Server

Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)

Drop-In: /usr/lib/systemd/system/httpd.service.d

└─php-fpm.conf

Active: active (running) since Sat 2022-03-22 04:27:24 EDT; 5s ago

Docs: man:httpd.service(8)

Main PID: 8644 (httpd)

Status: "Started, listening on: port 80"

Tasks: 213 (limit: 8918)

Memory: 21.5M

CGroup: /system.slice/httpd.service

├─8644 /usr/sbin/httpd -DFOREGROUND

├─8650 /usr/sbin/httpd -DFOREGROUND

├─8651 /usr/sbin/httpd -DFOREGROUND

├─8652 /usr/sbin/httpd -DFOREGROUND

└─8653 /usr/sbin/httpd -DFOREGROUND

Step 2: Install MySQL Server

Use the following command to install MySQL server including required packages on your system.

sudo dnf -y install @mysql

MySQL service to auto-start on the system start. Also start service manually for the first time.

sudo systemctl enable mysqld.service

sudo systemctl start mysqld.service

Then check the service current status using the following command:

sudo systemctl status mysqld.service

mysqld.service - MySQL 8.0 database server

Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

Active: active (running) since Sat 2022-03-22 02:49:33 EDT; 5min ago

Process: 5841 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)

Process: 5706 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)

Process: 5682 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)

Main PID: 5798 (mysqld)

Status: "Server is operational"

Tasks: 38 (limit: 8918)

Memory: 442.1M

CGroup: /system.slice/mysqld.service

└─5798 /usr/libexec/mysqld --basedir=/usr

The MySQL packages provide mysql_secure_installation command to apply the security. Just run the below command on terminal:

sudo mysql_secure_installation

and follow the on-screen instructions. Below are the details which require user input.

Press y|Y for Yes, any other key for No: y

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2

New password: [ENTER STRONG PASSWORD HERE]

Re-enter new password: RE ENTER PASSWORD HERE

Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

Step 3: Install PHP

First of all, you need to add the REMI repository to your system. Just execute the following command to add the repository.

sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm

Here we are enabling the module for installing PHP 7.4. You can change this to PHP 7.3 or PHP 7.2 as per your requirements.

sudo dnf module reset php

sudo dnf module enable php:remi-7.4

Install PHP packages along with required PHP modules.

sudo dnf install php php-mysqlnd

You many also need to install other required php modules as per your application requirements. The below command will install few frequently used php modules:

dnf install php-gd php-zip php-mcrypt php-json php-xml php-gettext php-curl php-intl

Step 4: Create Apache VirtualHost

All the packages required for LAMP environment is installed. Now, create the Apache virtual host. By default the Apache uses php-fpm for processing PHP files on CentOS 8 system.

Create and edit virtual host configuration file in the favorite text editor:

sudo vim /etc/httpd/conf.d/nicesnippets.example.net.conf

Add the following content in the configuration file. Make sure to change required parameters.

<VirtualHost *:80>

ServerAdmin admin@example.com

DocumentRoot /var/www/html

ServerName nicesnippets.example.net

ServerAlias www.nicesnippets.example.net

ErrorLog logs/nicesnippets.net-error.log

CustomLog logs/nicesnippets.net-access.log combined

</VirtualHost>

Save the virtual host configuration file and reload Apache to apply changes.

sudo systemctl restart httpd.service

Step 5: Allow Ports in Firewalld

If your system has firewalld installed ans active, you need to allow Apache ports. This will allow network users to access web application from remote systems.

The following commands will open the required ports for you.

sudo firewall-cmd --zone=public --permanent --add-service=http

sudo firewall-cmd --zone=public --permanent --add-service=https

sudo firewall-cmd --reload

Step 6: Test Setup

All done. To test the environment, create a PHP script with phpinfo() function. Place this file to your server document root. Use the below command to do this.

echo "<?php phpinfo(); ?>" < /var/www/html/info.php

Then access info.php using server IP address (for default VirtualHost) for your configured domain in Apache VirtualHost.

http://nicesnippets.example.net/info.php

I hope it could help you...

#PHP