Hamsa K
Editor
8 min read | 4 years ago

How to Install Apache, Mysql and PHP (LAMP Stack) in Centos 8

Install LAMP Stack on Centos 8

First we need to update packages with below command

[root@centos ~]# yum update

Step 1: Install Apache

[root@centos ~]# yum install httpd -y
[root@centos ~]# yum install httpd
Last metadata expiration check: 0:05:43 ago on Fri 22 Nov 2019 03:08:33 PM IST.
Dependencies resolved.
========================================================================================================================================================================
 Package                                 Arch                        Version                                                       Repository                      Size
========================================================================================================================================================================
Installing:
 httpd                                   x86_64                      2.4.37-12.module_el8.0.0+185+5908b0db                         AppStream                      1.7 M
Installing dependencies:
 apr                                     x86_64                      1.6.3-9.el8                                                   AppStream                      125 k
 apr-util                                x86_64                      1.6.1-6.el8                                                   AppStream                      105 k
 centos-logos-httpd                      noarch                      80.5-2.el8                                                    AppStream                       24 k
 httpd-filesystem                        noarch                      2.4.37-12.module_el8.0.0+185+5908b0db                         AppStream                       35 k
 httpd-tools                             x86_64                      2.4.37-12.module_el8.0.0+185+5908b0db                         AppStream                      102 k
 mod_http2                               x86_64                      1.11.3-3.module_el8.0.0+185+5908b0db                          AppStream                      158 k
Installing weak dependencies:
 apr-util-bdb                            x86_64                      1.6.1-6.el8                                                   AppStream                       25 k
 apr-util-openssl                        x86_64                      1.6.1-6.el8                                                   AppStream                       27 k
Transaction Summary
========================================================================================================================================================================
Install  9 Packages
Total download size: 2.3 M
Installed size: 6.0 M
Is this ok [y/N]: y

once above packages are installed start and stop apache service with below commands.

[root@centos ~]# systemctl start httpd
[root@centos ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

Now you can open Apache port 80 in firewalld as below

firewall-cmd --permanent --add-service=http --zone=public
firewall-cmd --reload
firewall-cmd --list-services --zone=public

Now you can check apache test page in browser like http://ip_address.

Step 2: Install mysql server

[root@centos ~]# yum install mysql-server
Last metadata expiration check: 0:19:29 ago on Fri 22 Nov 2019 03:08:33 PM IST.
Dependencies resolved.
========================================================================================================================================================================
 Package                                         Arch                      Version                                                   Repository                    Size
========================================================================================================================================================================
Installing:
 mysql-server                                    x86_64                    8.0.17-3.module_el8.0.0+181+899d6349                      AppStream                     22 M
Installing dependencies:
 mariadb-connector-c-config                      noarch                    3.0.7-1.el8                                               AppStream                     13 k
 mecab                                           x86_64                    0.996-1.module_el8.0.0+41+ca30bab6.9                      AppStream                    397 k
 mysql                                           x86_64                    8.0.17-3.module_el8.0.0+181+899d6349                      AppStream                     11 M
 mysql-common                                    x86_64                    8.0.17-3.module_el8.0.0+181+899d6349                      AppStream                    143 k
 mysql-errmsg                                    x86_64                    8.0.17-3.module_el8.0.0+181+899d6349                      AppStream                    557 k
 protobuf-lite                                   x86_64                    3.5.0-7.el8                                               AppStream                    150 k
 checkpolicy                                     x86_64                    2.8-2.el8                                                 BaseOS                       338 k
 libevent                                        x86_64                    2.1.8-5.el8                                               BaseOS                       253 k
 policycoreutils-python-utils                    noarch                    2.8-16.1.el8                                              BaseOS                       228 k
 python3-audit                                   x86_64                    3.0-0.10.20180831git0047a6c.el8                           BaseOS                        85 k
 python3-libsemanage                             x86_64                    2.8-5.el8                                                 BaseOS                       127 k
 python3-policycoreutils                         noarch                    2.8-16.1.el8                                              BaseOS                       2.2 M
 python3-setools                                 x86_64                    4.2.0-2.el8                                               BaseOS                       598 k
Transaction Summary
========================================================================================================================================================================
Install  14 Packages
Total download size: 37 M
Installed size: 227 M
Is this ok [y/N]: y           

Once above packages are installed start mysql service using below command

[root@centos ~]# systemctl start mysqld
[root@centos ~]# systemctl status mysqld

secure mysql using command mysql_secure_installation

[root@centos ~]# /usr/bin/mysql_secure_installation
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

Next press 'Y' for all to remove anonymous users, disallow remote root login, remove test database and reload privileges tables to effect the changes made.

Now you can now login to your MySQL 8 server as a root user with the password set above.

[root@centos ~]# mysql -u root -p

now you create database and exit . To check mysql version run below command

[root@centos ~]# mysql --version
mysql  Ver 8.0.17 for Linux on x86_64 (Source distribution)

Step 3: Install php

By default CentOS ships with PHP 7.2. So you can install PHP using the following command.

yum install -y php php-mysqlnd

If you want to install diffrent versions like 7.2/7.3/7.4 then use remi repository.

Before installing remi repo we will install EPEL repository like  below

[root@centos ~]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

Then install Remi Repository configuration package like below

[root@centos ~]# yum install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

After installing both epel and Remi repositories we will install php

First we will list the available PHP module stream.

[root@centos ~]# yum module list php
Remi's Modular repository for Enterprise Linux 8 - x86_64                                                                               104 kB/s | 510 kB     00:04
Safe Remi's RPM repository for Enterprise Linux 8 - x86_64                                                                              215 kB/s | 1.4 MB     00:06
Last metadata expiration check: 0:00:01 ago on Fri 22 Nov 2019 04:41:20 PM IST.
CentOS-8 - AppStream
Name                           Stream                                Profiles                                            Summary
php                            7.2 [d]                               common [d], devel, minimal                          PHP scripting language
Remi's Modular repository for Enterprise Linux 8 - x86_64
Name                           Stream                                Profiles                                            Summary
php                            remi-7.2                              common [d], devel, minimal                          PHP scripting language
php                            remi-7.3 [e]                          common [d], devel, minimal                          PHP scripting language
php                            remi-7.4                              common [d], devel, minimal                          PHP scripting language

Now enable remi-7.3 module to install php-7.3

[root@centos ~]# yum module enable php:remi-7.3 -y

Install php modules if you require any of the them from below list.

[root@centos ~]# yum install php php-devel php-gd php-bcmath php-pdo php-mcrypt php-tidy php-mysql php-xml php-soap php-imap php-pgsql php-mbstring

After installing package list you can check php version by following command.

[root@centos ~]# php -v
PHP 7.3.12 (cli) (built: Nov 19 2019 10:24:51) ( NTS )

Now restart apache service 

[root@centos ~]# systemctl restart httpd

Now we will go to default document root folder of apache

[root@centos ~]# cd /var/www/html/
[root@centos html]# vi info.php
<?php
phpinfo();
?>

save the file. Now open your browser and type http://ip_address/info.php

sample output:



Warning! This site uses cookies
By continuing to browse the site, you are agreeing to our use of cookies. Read our terms and privacy policy