How to Install LAMP Stack on CentOS 8/RHEL 8

This tutorial is going to show you how to install LAMP stack on CentOS 8 and RHEL 8.

What’s LAMP Stack?

A software stack is a set of software tools bundled together. LAMP stands for Linux, Apache, MariaDB/MySQL and PHP, all of which are open source. It is the most common software stack that powers dynamic websites and web applications. Linux is the operating system; Apache is the web server; MariaDB/MySQL is the database server and PHP is the server-side scripting language responsible for generating dynamic web pages.

Prerequisites

You can download and install RHEL 8 by following the tutorial below.

If you are looking for a VPS (Virtual Private Server), then you can register an account at Vultr via my referral link to get $50 free credit for use over 30 days.

This tutorial uses root account to manage administration tasks. To switch to root, run the following command and enter root password.

su -

Step 1: Update Software Packages

Before we install the LAMP stack, it’s a good idea to run the following command to update repository and software packages.

yum update

Step 2: Install Apache Web Server on CentOS 8/RHEL 8

Enter the following command to install Apache Web server. The httpd-tools package will install some useful utilities like Apache HTTP server benchmarking tool (ab).

yum install httpd httpd-tools

After it’s installed, we can start Apache with this command:

systemctl start httpd

Enable Apache to auto start at system boot time by running the following command.

systemctl enable httpd

Now check its status.

systemctl status httpd

Output:

 httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-10-12 06:43:15 UTC; 14s ago
     Docs: man:httpd.service(8)
 Main PID: 14515 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 5092)
   Memory: 24.8M
   CGroup: /system.slice/httpd.service
           ├─14515 /usr/sbin/httpd -DFOREGROUND
           ├─14516 /usr/sbin/httpd -DFOREGROUND
           ├─14517 /usr/sbin/httpd -DFOREGROUND
           ├─14518 /usr/sbin/httpd -DFOREGROUND
           └─14519 /usr/sbin/httpd -DFOREGROUND

Enabled” indicates that auto start at boot time is enabled and we can see that Apache is running.

Hint: If the above command doesn’t immediately quit after running. You need to press “q” to make it quit.

Check Apache version.

httpd -v

Output:

Server version: Apache/2.4.37 (centos)
Server built: Oct 7 2019 21:42:02

To test if Apache web server is running properly, we can create an index.html file under the default document root (/var/www/html/) with the following command.

echo "Welcome to this site!" > /var/www/html/index.html

If you are installing LAMP on your local CentOS 8/RHEL 8 computer, then type 127.0.0.1 or localhost in the browser address bar. You should see the welcome message, which means Apache Web server is running properly.

install-apache-on-centos-8-RHEL-8

By default, CentOS 8/RHEL 8 forbids public access to port 80. To allow other computers to access the web page, we need to open port 80 in firewalld, the dynamic firewall manager on RHEL/CentOS. Run the following command to open port 80.

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

If you want to enable HTTPS on Apache later, then you also need to open port 443.

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

The --permanent option will make this firewall rule persistent across system reboots. Next, reload the firewall daemon for the change to take effect.

systemctl reload firewalld

Now the Apache web page is accessible publicly.

Finally, we need to make user apache as the owner of web directory. By default it’s owned by the root user.

chown apache:apache /var/www/html -R

Step 3: Install MariaDB Database Server on CentOS 8/RHEL 8

MariaDB is a drop-in replacement for MySQL. It is developed by former members of MySQL team who are concerned that Oracle might turn MySQL into a closed-source product. Enter the following command to install MariaDB on CentOS 8/RHEL 8.

yum install mariadb-server mariadb -y

After it’s installed, we need to start it.

systemctl start mariadb

Enable auto start at system boot time.

systemctl enable mariadb

Check status:

systemctl status mariadb

output:

 mariadb.service - MariaDB 10.3 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-10-12 09:02:53 UTC; 33s ago
     Docs: man:mysqld(8)
           https://mariadb.com/kb/en/library/systemd/
 Main PID: 18608 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 30 (limit: 5092)
   Memory: 77.0M
   CGroup: /system.slice/mariadb.service
           └─18608 /usr/libexec/mysqld --basedir=/usr

Enabled” indicates that auto start at boot time is enabled and we can see that MariaDB server is running. Now we need to run the security script.

mysql_secure_installation

When it asks you to enter MariaDB root password, press Enter key as the root password isn’t set yet. Then enter y to set the root password for MariaDB server.

install mariadb on redhat 8 centos 8

Next, you can press Enter to answer all remaining questions, which will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security. (Note that the letter Y is capitalized, which means it’s the default answer.)

mysql_secure_installation rhel8 centos8

Now you can run the following command and enter MariaDB root password to log into MariaDB shell.

mysql -u root -p

mariadb shell login

To exit, run

exit;

Step 4: Install PHP on CentOS 8/RHEL 8

Install PHP and some common modules using the following command.

yum install php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring -y

Apache web server on CentOS 8/RHEL 8 by default uses PHP-FPM instead of mod_php to run PHP code, so in the above command we also installed php-fpm. After it’s installed, we need to start it.

systemctl start php-fpm

Enable auto start at system boot time.

systemctl enable php-fpm

Check status:

systemctl status php-fpm

output:

 php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-10-12 09:54:37 UTC; 3s ago
 Main PID: 19755 (php-fpm)
   Status: "Ready to handle connections"
    Tasks: 6 (limit: 5092)
   Memory: 24.5M
   CGroup: /system.slice/php-fpm.service
           ├─19755 php-fpm: master process (/etc/php-fpm.conf)
           ├─19757 php-fpm: pool www
           ├─19758 php-fpm: pool www
           ├─19759 php-fpm: pool www
           ├─19760 php-fpm: pool www
           └─19761 php-fpm: pool www

Enabled” indicates that auto start at boot time is enabled and we can see that PHP-FPM is running. The php-fpm package installs a php.conf file in /etc/httpd/conf.d/ directory, so we need to restart Apache web server, in order to run PHP code.

systemctl restart httpd

We also need to run the following command to tell SELinux to allow Apache to execute PHP code via PHP-FPM.

setsebool -P httpd_execmem 1

Step 5: Test PHP

To test PHP-FPM with Apache Web server, we need to create a info.php file in the document root directory.

nano /var/www/html/info.php

Paste the following PHP code into the file.

<?php phpinfo(); ?>

Save and close the file. If you installed LAMP stack on a local CentOS 8/RHEL 8 server, type in 127.0.0.1/info.php or localhost/info.php in the browser address bar. You should see your server’s PHP information. This means PHP scripts can run properly with Apache web server.

If RHEL 8/CentOS is running on a remote server, then enter server-ip-address/info.php in browser address bar. Replace sever-ip-address with your actual IP address.

PHP-FPM-RHEL-8-CentOS-8
If the browser fails to display the PHP info but prompt you to download the info.php file, simply restart Apache and PHP-FPM.

sudo systemctl restart httpd php-fpm

Then you should be able to see the PHP info in the web browser.

Wrapping Up

Congrats! You have successfully installed Apache, MariaDB and PHP7.2 on Red Hat 8 or CentOS 8. For your server’s security, you should delete info.php file now to prevent hacker seeing it.

rm /var/www/html/info.php

I hope this tutorial helped you. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial
[Total: 1 Average: 5]

2 Responses to “How to Install LAMP Stack on CentOS 8/RHEL 8

  • Very well written! Kudos!!! Thanks a lot for this contribution.

  • Jaimie
    4 days ago

    Amazing guide, had to do this in class for an assignment it worked great 5 stars!

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.