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.

dnf 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).

dnf 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.

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

By default, Apache uses the system hostname as its global ServerName. If the system hostname can’t be resolved in DNS, then you will probably see the following error after running sudo apachectl configtest command.

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

To solve this problem, we can set a global ServerName in Apache. Install the Nano command-line text editor and use it to create a new configuration file.

sudo dnf install nano

sudo nano /etc/httpd/conf.d/servername.conf

Add the following line in this file.

ServerName localhost

Save and close the file. To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X. Reload Apache for the change to take effect.

sudo systemctl reload httpd

Now if you run the sudo apachectl configtest command again, you won’t see the above error message.

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.

dnf 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.

dnf 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.

Apache Automatic Restart

If for any reason your Apache process is killed, you need to run the following command to restart it.

sudo systemctl restart httpd

Instead of manually typing this command, we can make Apache automatically restart by editing the httpd.service systemd service unit. To override the default systemd service configuration, we create a separate directory.

sudo mkdir -p /etc/systemd/system/httpd.service.d/

Then create a file under this directory.

sudo nano /etc/systemd/system/httpd.service.d/restart.conf

Add the following lines in the file, which will make Apache automatically restart 5 seconds after a failure is detected.

[Service]
Restart=always
RestartSec=5s

Save and close the file. Then reload systemd.

sudo systemctl daemon-reload

To check if this would work, kill Apache with:

sudo pkill httpd

Then check Apache status. You will find Apache automatically restarted.

systemctl status httpd

Allow Apache to Make Outgoing Network Connections

By default, SELinux forbids Apache to make outgoing network connections. If Apache needs to make requests to an outside network service, then run the following command to allow this action.

setsebool -P httpd_can_network_connect on

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: 10 Average: 5]

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

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

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

  • sjekoken
    5 years ago

    Very nice, clean, tutorial (as usual), thanks!

  • This was a clean and solid CentOS tutorial. Everything is working as intended. Thank you. 10/10

  • Lalatendu
    4 years ago

    Its help me a lot

  • powderflask
    4 years ago

    Thank you so much. Clean, clear, concise. Very much appreciated.

  • hoangthai
    4 years ago

    thank you very much … .

  • Thank you so so much, that was really good and helpful, really appreciated

  • It would be great if you can write how to START USING the LAMP stack after this – maybe by showing a test website with a backend database and a reactive HTML5 UI.

  • Sunil Gautam
    4 years ago

    One of the most neatly written guide. Special claps for the TIP for pressing q. (I was realy stuck at that point and then saw the red coloured note.)

  • Thanks a Ton bro. Very well written

  • Mark J.
    4 years ago

    Nice, quite helpful. Direct and to the point!

  • Josiah Heng
    4 years ago

    I’ve been using Linux for a long time for alot of things, but nowadays I’m primarily on Kali Linux for pentesting work. Your tutorials are one of the most accurate and amongst the best ever. I hope RedHat Inc. hires you one day. You have added not only clarity to the instructions, you have even added value by doing things like add restart.conf file to auto-restart Apache after a kill command is executed to it or perhaps service crash. I hope you write a book on Linux Administration one day, or maybe become the next official author to write books for the RHCE syllabus. Keep up the good work, you rock man!!

  • David Bores
    4 years ago

    Thank you so much..!!

  • calicozac
    4 years ago

    I spent 3 years at a state college, I have learned more from your site in the last few weeks than I did there. When I get a real job I’ll buy you a few beers!!

  • Just one question: You are setting up automatic restart for Apache – why not for php-fpm?

  • Brilliant write up, keep up the good work.

  • I wanted to also share this as i had problems trying to connect to Google Cloud Datastore from a CentOS 8 machine and i kept getting CURL 7 error.

    CURL ERROR 7 Failed to connect to Permission denied

    Running the below command fixed the issue for me.

    setsebool -P httpd_can_network_connect on

    Thought it was worth mentioning in your article as i was racking my brains on why i couldn’t connect.

    Hope this helps someone.

    • Xiao Guoan (Admin)
      3 years ago

      Yes, by default, SELinux forbids Apache/Nginx to make outgoing network connections.

  • Let say, I want to install apache with php, python on the same server and I would want the database on another server. How do I do that?

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.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here