Home AlmalinuxHow to Install LAMP Stack on AlmaLinux 9 (Apache, MariaDB, PHP)

How to Install LAMP Stack on AlmaLinux 9 (Apache, MariaDB, PHP)

by Knowledge Base Linux
Install LAMP on AlmaLinux 9 hosting stack layers

The LAMP stack — Linux, Apache, MariaDB (or MySQL) and PHP — is the classic foundation for dynamic websites and still the default choice for WordPress and most PHP applications. This tutorial shows you how to install LAMP on AlmaLinux 9 using only official repositories: Apache as the web server, MariaDB as the database, and PHP served through PHP-FPM, finishing with a secured database and a working test page.

Prerequisites – How to Install LAMP Stack on

  • An AlmaLinux 9 server with sudo access and an updated system (sudo dnf update -y).
  • Ports 80/443 open in the firewall (configured in Step 2).
  • Optional: a domain name pointing at the server.

Step 1: Install Apache – How to Install LAMP Stack on

sudo dnf install -y httpd
sudo systemctl enable --now httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Open http://your-server-ip — the AlmaLinux Apache test page confirms the web server is running. If it does not load, check systemctl status httpd before continuing.

Step 2: Install MariaDB and Secure It

Next in the stack is the database. AlmaLinux 9 ships MariaDB 10.5:

sudo dnf install -y mariadb-server
sudo systemctl enable --now mariadb
sudo mysql_secure_installation

Set a strong root password in the wizard and accept the security defaults (remove anonymous users, disallow remote root login, drop the test database). Then create a database and user for your site:

sudo mysql -u root -p -e "CREATE DATABASE webdb; CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'StrongPassword!'; GRANT ALL PRIVILEGES ON webdb.* TO 'webuser'@'localhost'; FLUSH PRIVILEGES;"
Install LAMP on AlmaLinux 9 PHP code and database connection

Step 3: Install PHP with PHP-FPM to Install LAMP on AlmaLinux 9

Modern AlmaLinux uses PHP-FPM with Apache’s event MPM — faster and leaner than the old mod_php model. Install PHP and the extensions most applications require:

sudo dnf install -y php php-fpm php-mysqlnd php-cli php-gd php-xml php-mbstring php-json php-opcache php-intl
sudo systemctl enable --now php-fpm

Apache needs to know how to hand PHP files to FPM. AlmaLinux’s php package already drops a proxy configuration into /etc/httpd/conf.d/php.conf that forwards .php requests to the FPM socket, so in most cases no manual handler is required. Restart Apache to pick everything up:

sudo systemctl restart httpd

Step 4: Adjust SELinux for Web Content

If your document root is the default /var/www/html, labels are already correct. For custom locations, apply the web content type and allow outbound connections from PHP if your app calls external APIs:

sudo chcon -R -t httpd_sys_content_t /var/www/html
sudo setsebool -P httpd_can_network_connect 1

Step 5: Test the Full Stack

Create a test page and a small database-connection check:

sudo tee /var/www/html/info.php <<'EOF'
<?php
phpinfo();
EOF
cat <<'EOF' | sudo tee /var/www/html/dbtest.php
<?php
$conn = new mysqli('localhost', 'webuser', 'StrongPassword!', 'webdb');
echo $conn->connect_error ? 'Connection failed' : 'Database connection OK';
EOF

Browse to /info.php to verify PHP runs and lists the MySQL modules, then /dbtest.php to confirm PHP can reach MariaDB with your application user. Remove both test files when finished — they reveal configuration details:

sudo rm /var/www/html/info.php /var/www/html/dbtest.php

To host several sites on this stack afterwards, follow our guide to Apache virtual hosts on AlmaLinux 9, and remember to keep the system patched with the workflow in the official AlmaLinux wiki.

Troubleshooting

PHP files download instead of executing: PHP-FPM is not running or Apache was not restarted after installing php — check both services. Database connection fails: confirm MariaDB is active, the user was created for 'localhost' (socket) vs '127.0.0.1' (TCP) as your app expects, and the password has no shell-quoting issues. Permission denied writing uploads: the target directory needs the SELinux type httpd_sys_rw_content_t and write access for the apache user.

Useful Apache Settings for Real Websites

Most PHP applications, WordPress included, expect two Apache behaviors that are off or restricted by default. First, pretty permalinks need mod_rewrite and permission to use .htaccess files. The module ships enabled on AlmaLinux; allow overrides for your document root in the site configuration:

<Directory /var/www/html>
    AllowOverride All
    Require all granted
</Directory>

Second, raise the default limits for uploads so large images and imports stop failing: set upload_max_filesize = 64M and post_max_size = 64M in /etc/php.ini, then restart php-fpm and httpd. While you are there, set your timezone (date.timezone = Asia/Ho_Chi_Minh) so application logs and scheduled posts behave as users expect.

A note on architecture choices: this guide uses PHP-FPM with Apache’s event MPM, which is the configuration AlmaLinux is tuned for. The older prefork MPM with mod_php still exists for legacy modules, but it uses considerably more memory per connection. Unless a specific extension forces your hand, stay with FPM — it is the setup every modern guide, including the rest of this series, assumes.

Conclusion

You now know how to install LAMP on AlmaLinux 9 end to end: Apache serving pages, MariaDB secured with a dedicated application account, and PHP running through PHP-FPM. This reliable, fully open-source stack is ready for WordPress, WooCommerce or your own PHP applications — deploy with confidence.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.