How to install a LAMP Stack (Linux, Apache, MariaDB and PHP with PHP-FPM) on a CentOS 9 Stream

Install a minimum CentOS 9 Stream on a cloud based service or install it on your own server

Here we will use dnf install / update instead of yum install / update as dnf is the newer and better software package manager than yum.

Login as root or get root access
# su -
enter password
Run OS update
# dnf update

Install some of the basic Linux useful tools
# dnf install nano htop net-tools

Create a user and disable SSH root access for better security
# useradd yourusername
# passwd yourusername
enter password
After creating a regular user, disable root login for better security
# nano /etc/ssh/sshd_config
If there is a line like below:
PermitRootLogin yes
Change it to:
PermitRootLogin no
Then restart SSH service:
# systemctl restart sshd

Set or Change Host Name
# hostnamectl set-hostname testserver.myserver.com

Edit host file and change the host name to your server's host name:

# nano /etc/hosts

You may need to restart network manager for the host changes to take affect:

# systemctl restart NetworkManager

Install ntp date server
# dnf install ntp
# systemctl enable ntpd.service
# systemctl start ntpd.service

Install Apache
# dnf install httpd httpd-tools mod_ssl openssl
# systemctl enable httpd.service
# systemctl start httpd.service

Check your Apache install configuration:

# systemctl status httpd

Configure firewall to allow HTTP and HTTPS access:

# firewall-cmd --permanent --zone=public --add-service=http
# firewall-cmd --permanent --zone=public --add-service=https
# firewall-cmd --reload

Configure Apache MPM

Edit MPM module configuration:

# nano /etc/httpd/conf.modules.d/00-mpm.conf

I prefer mpm event module, so I edit mpm module configuration file and comment out mpm worker to disable mpm worker:
#LoadModule mpm_worker_module modules/mod_mpm_worker.so
And uncomment mpm event to enable mpm event:
LoadModule mpm_event_module modules/mod_mpm_event.so

I use below configuration for mpm event module. This configuration is for a server or a cloud that has about 8 core CPU and 64 GB RAM allocated to Apache. This configuration may not work for all scenarios so use this configuration at your own risk and monitor and test your Apache if you decide to use below configuration:

# nano /etc/httpd/conf.d/mpm.conf
 
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 300
TimeOut 60
<IfModule mpm_event_module>
    StartServers            8
    ServerLimit             20
    ThreadsPerChild         50
    MinSpareThreads        500 
    MaxSpareThreads        750
    ThreadLimit           64
    MaxRequestWorkers	   1000 
    MaxConnectionsPerChild   10000
</IfModule>

Restart Apache so the changes takes affect:

# systemctl restart httpd.service

Install PHP

The default dnf install php on CentOS 9 Stream installs PHP 8.0. If you want to install a different PHP version, then you can use REMI Repository to install a different version of PHP.

# dnf install php php-fpm php-mysqlnd php-common php-opcache php-cli php-mbstring php-curl php-xml php-json php-pdo

Special Situation when you need to install a different version of PHP; you can use REMI Repository

# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
# dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm
# dnf --enablerepo=remi install php81

For other PHP versions - examples:
# dnf --enablerepo=remi install php56
# dnf --enablerepo=remi install php71 
# dnf --enablerepo=remi install php73 
# dnf --enablerepo=remi install php74

Configure PHP-FPM & PHP

Edit www.conf

# nano /etc/php-fpm.d/www.conf

Update the www.conf to below configuration

; listen = 127.0.0.1:9000
listen = /var/run/php-fpm/www.sock
listen.owner = apache
listen.group = apache
listen.mode = 0660

Edit php.conf:

# nano /etc/httpd/conf.d/php.conf

And update the php.conf with below configurations

#
# Redirect to local php-fpm (no mod_php in default configuration)
#
<IfModule !mod_php5.c>
  <IfModule !mod_php7.c>
    # Enable http authorization headers
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1

    <FilesMatch \.(php|phar)$>
        SetHandler "proxy:unix:/var/run/php-fpm/www.sock|fcgi://localhost"
    </FilesMatch>
  </IfModule>
</IfModule>
 

PHP-FPM Connections / Performance Settings

Edit PHP-FPM connection settings:

# nano /etc/php-fpm.d/www.conf

I use below configuration for PHP-FPM connections settings. This configuration is for a server or a cloud that has about 8 core CPU and 64 GB RAM allocated to PHP-FPM. This configuration may not work for all scenarios so use this configuration at your own risk and monitor and test your Apache if you decide to use below configuration

pm = static
pm.max_children = 500
pm.max_requests = 8000

Start and Enable PHP-FPM

# systemctl enable php-fpm.service
# systemctl start php-fpm.service

Install MariaDB

In my opinion, MariaDB is a better version of MySQL, so I install MariaDB:

# dnf install mariadb-server mariadb
# systemctl enable mariadb
# systemctl start mariadb

Use below command to secure your MariaDB installation:

# mysql_secure_installation