How to Install LAMP in Red Hat RHEL 8
In this guide we will see how to install LAMP in Red Hat RHEL 8 using dnf
, the default package manager.
Introduction
In our Linux Support interventions, the LAMP (Linux, Apache, MySQL, PHP), LEMP (NginX) and similar stacks are critical for Web Apps, CMS and e-commerce such as WordPress, Magento, Drupal and many others.
To install LAMP in Red Hat RHEL 8 we start by installing the Apache web server with HTTPS, the first component of the stack, which will be responsible for serving content.
To optimize web server performance, we recommend using PHP-FPM, mpm_event
and HTTP2. Most websites use secure connections. We will also see how to generate a valid and free HTTPS certificate with Let's Encrypt.
Installing LAMP in Red Hat RHEL 8
1. Install Apache2
By default, the mpm_prefork
multi-process module is provided, which allows you to use mod_php
or php8.0, the in-house interpreter. This approach is not recommended for heavily-visited sites, since having reached the maximum number of threads, the web server stops responding until a thread is freed.
Let's install Apache2
sudo dnf install httpd.
Let's make sure the
mpm_event_module
is in use to ensure better performance.sudo grep mpm_event_module /etc/httpd/conf.modules.d/00-mpm.conf
If nothing is displayed we edit the file
/etc/httpd/conf.modules.d/00-mpm.conf
so that theLoadModule
line looks like the following, to loadmpm_event
LoadModule mpm_event_module modules/mod_mpm_event.so
We check in the same previous way that
http2
is active.sudo grep http2_module /etc/httpd/conf.modules.d/10-h2.conf
In case it is not we edit the file
/etc/httpd/conf.modules.d/10-h2.conf
as below:LoadModule http2_module modules/mod_http2.so.
Restart Apache2.
sudo systemctl restart httpd
Let's make sure Apache2 is enabled at startup
sudo systemctl enable httpd sudo systemctl start httpd
Let's check that Apache2
httpd
is enabledsudo systemctl status httpd
● httpd.service - The Apache HTTP Server. Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2024-10-18 02:44:28 UTC; 8s ago Main PID: 74751 (httpd)
We allow HTTP and HTTPS traffic through the firewall
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
2. Install PHP-FPM
PHP-FPM is an alternative implementation of PHP FastCGI, known for its speed, three times faster than the classic module, with additional functionality. FPM is a real daemon that stays listening for PHP requests via a UNIX port or socket. It represents the P of our LAMP.
Let's install FPM and FastCGI.
sudo dnf install php php-fpm
Let's restart the PHP FPM process.
sudo systemctl restart php-fpm.
Let's restart Apache2.
sudo systemctl restart httpd
PHP-FPM Virtual Host Configuration.
You can now configure all Apache2 virtual hosts to use FPM with the template below, or create new ones within /etc/httpd/conf.d/
using a name ending in .conf
, starting with this example:
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.mydomain.ext
# Other directives here
<FilesMatch ".+\.ph(p[345]?|t|tml)$">
SetHandler "proxy:unix:/run/php-fpm/www.sock"
</FilesMatch>
</VirtualHost>
Now we can test the web server and PHP by creating the file /var/www/html/info.php
:
<?php
phpinfo();
phpinfo(INFO_MODULES);
?>
We install the necessary modules and PHP-MySQL
. We can choose to install some of the more common modules as well.
sudo dnf install php-pear \
php-{mysqlnd,gd,intl,snmp,xsl,pdo,mbstring}
3. Install Let's Encrypt for Free SSL Certificates
Using Let's Encrypt and the certbot
daemon, you can generate universally recognized, secure and free SSL certificates. You need to configure the DNS name of the website to reach the IP of our server.
Example of DNS configuration.
To configure DNS, we access our provider's panel and create a record of type "A".
mydomain.ext A 1.1.1.1
We install Let's Encrypt to generate SSL and TLS certificates.
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noar
ch.rpm
sudo dnf install certbot python3-certbot-apache
We accept the key if requested:
Importing GPG key 0x2F86D6A1:
Userid : "Fedora EPEL (8) "
Fingerprint : 94E2 79EB 8D8F 25B2 1810 ADF1 21EA 45AB 2F86 D6A1
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
Is this ok [y/N]: Y
Let's install Let's Encrypt certbot
sudo dnf install certbot python3-certbot-apache
Let's restart Apache2 httpd to let it generate the self-signed certificates
sudo systemctl restart httpd
Let's create the Apache2 configuration.
sudo certbot --apache
We test the renewal of certificates.
sudo certbot renew --dry-run.
We check that the Cron scheduled process for renewal is present.
sudo grep certbot /etc/cron.d/ -R
If the resolution of our domain is correct, our virtual hosts will respond in HTTPS, returning the famous "green padlock" on the browser.
Apache2 Virtual Hosts
HTTP Virtual Hosts
To configure an HTTP virtual host, we can use a simple definition like the one listed below by creating a file in the /etc/httpd/conf.d
directory
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<FilesMatch ".+\.ph(p[345]?|t|tml)$">
SetHandler "proxy:unix:/run/php-fpm/www.sock"
</FilesMatch>
</VirtualHost>
HTTPS Virtual Hosts
HTTPS virtual hosts include the necessary paths for using SSL and enabling PHP.
<VirtualHost *:443>
ServerAdmin [email protected]
DocumentRoot "/var/www/data/online"
ServerName mydomain.ext
ServerAlias www.mydomain.ext
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Protocols h2 h2c http/1.1
SSLEngine on
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mydomain.ext/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.ext/privkey.pem
<FilesMatch ".+\.ph(p[345]?|t|tml)$">
SetHandler "proxy:unix:/run/php-fpm/www.sock"
</FilesMatch>
</VirtualHost>
4. Install MySQL or MariaDB
To install MySQL or MariaDB, we use the corresponding packages mysql-server
and mariadb-server
.
To install MySQL server:
sudo dnf install mysql-server
To install MariaDB (I personally prefer it):
sudo dnf install mariadb-server
For detailed configuration, you can refer to our guide to installing and configuring MySQL for CentOS Stream 9.
Red Hat consulting
Do you need support? Try our professional Red Hat Consulting services now 30' Free!
Conclusion
We managed to install LAMP in Red Hat RHEL 8, now our server is ready to host a CMS or Web App
If you found this guide useful, please share it on social, others may need it.