How to install Apache, PHP & MySQL on AlmaLinux 8

AlmaLinux 8 is a community-driven Linux distribution that is compatible with Red Hat Enterprise Linux 8. It is a free and open-source alternative to CentOS 8, which has shifted its focus to CentOS Stream. AlmaLinux 8 is suitable for running various web applications that require a LAMP stack. LAMP stands for Linux, Apache, MySQL and PHP, which are the essential components of a web server environment.

In this article, we will show you how to install and configure Apache, MySQL and PHP on AlmaLinux 8. We will also show you how to install phpMyAdmin, a web-based tool for managing MySQL databases.

Prerequisites

Before you begin, you need to have the following:

  • A system running AlmaLinux 8 with sudo privileges
  • A stable internet connection
  • A web browser to access the web server

Step 1: Update the system

The first step is to update the system packages and repositories to ensure that you have the latest versions and security patches. To do this, run the following command:

sudo dnf update

You may need to enter your password and confirm the installation by typing y when prompted.

Step 2: Install Apache web server

The next step is to install Apache, which is the most popular and widely used web server software. Apache can serve static and dynamic web pages using the HTTP or HTTPS protocol. To install Apache on AlmaLinux 8, run the following command:

sudo dnf install httpd httpd-tools

This command will install Apache and some additional tools that are useful for managing the web server.

Step 3: Start and enable Apache service

After installing Apache, you need to start its service and enable it to run automatically at system boot. This will ensure that your web server is always up and running. To do this, run the following commands:

sudo systemctl start httpd
sudo systemctl enable httpd

You can check the status of the Apache service by running:

sudo systemctl status httpd

You should see something like this:

● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-12-14 10:15:20 CET; 5min ago
     Docs: man:httpd.service(8)
 Main PID: 1234 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 2377)
   Memory: 15.4M
   CGroup: /system.slice/httpd.service
           ├─1234 /usr/sbin/httpd -DFOREGROUND
           ├─1235 /usr/sbin/httpd -DFOREGROUND
           ├─1236 /usr/sbin/httpd -DFOREGROUND
           ├─1237 /usr/sbin/httpd -DFOREGROUND
           └─1238 /usr/sbin/httpd -DFOREGROUND

Dec 14 10:15:20 almalinux systemd[1]: Starting The Apache HTTP Server...
Dec 14 10:15:20 almalinux httpd[1234]: Server configured, listening on: port 80
Dec 14 10:15:20 almalinux systemd[1]: Started The Apache HTTP Server.

Step 4: Configure firewall rules

By default, AlmaLinux 8 comes with firewalld, which is a firewall management tool that controls the network traffic. To allow external access to your web server, you need to open ports 80 and 443 on your firewall. Port 80 is used for HTTP connections and port 443 is used for HTTPS connections.

To open port 80, run the following command:

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

To open port 443, run the following command:

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

To apply the changes, reload the firewall by running:

sudo firewall-cmd --reload

You can verify that the ports are open by running:

sudo firewall-cmd --list-all

You should see something like this:

public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: cockpit dhcpv6-client http https ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

Notice that the services section includes http and https.

Step 5: Test Apache web server

At this point, you have successfully installed and configured Apache on your AlmaLinux 8 system. To test if it is working properly, you can access it from your web browser using your server’s IP address or hostname. To find out your server’s IP address, run the following command:

ip a

You should see something like this:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:a1:b2:c3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.10/24 brd 192.168.122.255 scope global dynamic noprefixroute eth0
       valid_lft 3519sec preferred_lft 3519sec
    inet6 fe80::5054:ff:fea1:b2c3/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

In this example, the IP address of the server is 192.168.122.10.

Now, open your web browser and type http://192.168.122.10 in the address bar. You should see the default Apache welcome page, which looks something like this:

This means that your web server is up and running.

Step 6: Install MySQL or MariaDB on AlmaLinux 8

MySQL is a widely used open-source relational database management system (RDBMS) that supports SQL queries and data manipulation. MySQL is often used in conjunction with PHP to store and retrieve data for web applications.

MariaDB is a fork of MySQL that is developed and maintained by the original creators of MySQL. MariaDB is fully compatible with MySQL and offers some additional features and improvements.

AlmaLinux 8 provides both MySQL and MariaDB as options for installing a database server. You can choose either one according to your preference and needs.

Install MySQL on AlmaLinux 8

To install MySQL on AlmaLinux 8, run the following command:

sudo dnf install mysql-server

This command will install MySQL and its dependencies on your system.

Install MariaDB on AlmaLinux 8

To install MariaDB on AlmaLinux 8, run the following command:

sudo dnf install mariadb-server

This command will install MariaDB and its dependencies on your system.

Step 7: Start and enable MySQL or MariaDB service

After installing either MySQL or MariaDB, you need to start its service and enable it to run automatically at system boot. This will ensure that your database server is always available for your web applications.

To start and enable MySQL service, run the following commands:

sudo systemctl start mysqld
sudo systemctl enable mysqld

To start and enable MariaDB service, run the following commands:

sudo systemctl start mariadb
sudo systemctl enable mariadb

You can check the status of the database service by running:

sudo systemctl status mysqld # for MySQL
sudo systemctl status mariadb # for MariaDB

You should see something like this:

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-12-14 10:30:20 CET; 5min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 2345 (mysqld)
   Status: "Server is operational"
    Tasks: 39 (limit: 2377)
   Memory: 332.4M
   CGroup: /system.slice/mysqld.service
           └─2345 /usr/libexec/mysqld --based