How to Install LAMP Stack on AlmaLinux 9

A LAMP stack is a group of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym which represents the Linux operating system, with the Apache web server. The site data is stored in a MySQL database, and dynamic content is processed by PHP.

Install Apache Web Server

First, we will start by installing the Apache web server. To complete the installation, use the following command.

dnf install httpd httpd-tools

Output:

[root@server ~]# dnf install httpd httpd-tools
Last metadata expiration check: 1:53:57 ago on Fri May 20 16:53:51 2022.
Dependencies resolved.
================================================================================
 Package                   Arch       Version               Repository     Size
================================================================================
Installing:
 httpd                     x86_64     2.4.51-7.el9_0        appstream     1.4 M
 httpd-tools               x86_64     2.4.51-7.el9_0        appstream      81 k
Installing dependencies:
 almalinux-logos-httpd     noarch     90.4-1.el9            appstream      14 k
 apr                       x86_64     1.7.0-11.el9          appstream     123 k
 apr-util                  x86_64     1.6.1-20.el9          appstream      95 k
 apr-util-bdb              x86_64     1.6.1-20.el9          appstream      13 k
 httpd-filesystem          noarch     2.4.51-7.el9_0        appstream      14 k
 mailcap                   noarch     2.1.49-5.el9          baseos         32 k
Installing weak dependencies:
 apr-util-openssl          x86_64     1.6.1-20.el9          appstream      15 k
 mod_http2                 x86_64     1.15.19-2.el9         appstream     149 k
 mod_lua                   x86_64     2.4.51-7.el9_0        appstream      61 k

Transaction Summary
================================================================================
Install  11 Packages

Once the installation is complete, enable Apache (to start automatically upon system boot), start the web server and verify the status using the commands below.

systemctl enable httpd

systemctl start httpd

systemctl status httpd

Output:

[root@server ~]# 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 2022-05-20 18:51:16 CEST; 10s ago
       Docs: man:httpd.service(8)
   Main PID: 1085 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec:   0 B/sec"
      Tasks: 213 (limit: 5912)
     Memory: 35.2M
        CPU: 123ms
     CGroup: /system.slice/httpd.service
             ├─1085 /usr/sbin/httpd -DFOREGROUND
             ├─1086 /usr/sbin/httpd -DFOREGROUND
             ├─1087 /usr/sbin/httpd -DFOREGROUND

To make your pages available to public, you will have to edit your firewall rules to allow HTTP and HTTPS requests on your web server by using the following commands.

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

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

firewall-cmd --reload

Output:

[root@server ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@server ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@server ~]# firewall-cmd --reload
success

Verify that the web server is running and accessible by accessing your server’s IP address.

And restart tht Web Server to reflect the changes made.

systemctl restart httpd

From your browser,

http://IP_address

Replace the IP_address with the actual IP address.

image

Install PHP

If you would like to install a different version of PHP from remi repository, please refer Install PHP wiki.

We will install the EPEL Repository,

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm

To install PHP on AlmaLinux, use the command below.

dnf install php

We will need other PHP modules as well,

dnf install -y php-mysqlnd php-dom php-simplexml php-xml php-xmlreader php-curl php-exif php-ftp php-gd php-iconv  php-json php-mbstring php-posix php-sockets php-tokenizer

Now restart your web server so that Apache knows that it will be serving PHP requests as well.

systemctl restart httpd 

Test your PHP, by creating a simple info.php file with a phinfo() in it. The file should be placed in the directory root for your web server, which is /var/www/html.

To create the file use:

echo "<?php phpinfo() ?>" > /var/www/html/info.php

Now again, access http://localhost/info.php or http://yourserver-ip-address/info.php. You should see a page similar to below one.

image

Install MariaDB Server

MariaDB is a popular database server. The installation is simple and requires just a few steps as shown.

dnf install mariadb-server mariadb

Output:

[root@server ~]# dnf install mariadb-server mariadb
Last metadata expiration check: 2:00:16 ago on Fri May 20 16:53:51 2022.
Dependencies resolved.
===========================================================================================================================
 Package                                  Architecture       Version                           Repository             Size
===========================================================================================================================
Installing:
 mariadb                                  x86_64             3:10.5.13-1.el9                   appstream             1.6 M
 mariadb-server                           x86_64             3:10.5.13-1.el9                   appstream             9.3 M
Installing dependencies:
 checkpolicy                              x86_64             3.3-1.el9                         appstream             339 k
 libaio                                   x86_64             0.3.111-13.el9                    baseos                 23 k
 mariadb-common                           x86_64             3:10.5.13-1.el9                   appstream              27 k
 mariadb-connector-c                      x86_64             3.2.6-1.el9_0                     appstream             194 k
 mariadb-connector-c-config               noarch             3.2.6-1.el9_0                     appstream             9.7 k
 mariadb-errmsg                           x86_64             3:10.5.13-1.el9                   appstream             183 k
 mysql-selinux                            noarch             1.0.4-2.el9                       appstream              35 k
 perl-AutoLoader                          noarch             5.74-479.el9                      appstream              30 k
 perl-B                                   x86_64             1.80-479.el9                      appstream             188 k
 perl-Carp                                noarch             1.50-460.el9                      appstream              29 k
 perl-Class-Struct                        noarch             0.66-479.el9                      appstream              31 k
 perl-DBD-MariaDB                         x86_64             1.21-15.el9                       appstream             151 k

Once the installation is complete, enable MariaDB (to start automatically upon system boot), start the MariaDB and verify the status using the commands below.

systemctl enable mariadb

systemctl start mariadb

systemctl status mariadb

Output:

[root@server ~]# systemctl status mariadb
● mariadb.service - MariaDB 10.5 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
     Active: active (running) since Fri 2022-05-20 18:07:30 CEST; 4s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 6551 ExecStartPre=/usr/libexec/mariadb-check-socket (code=exited, status=0/SUCCESS)
    Process: 6573 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/SUCCESS)
    Process: 6665 ExecStartPost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/SUCCESS)
   Main PID: 6653 (mariadbd)

Finally, you will want to secure your MariaDB installation by issuing the following command.

mysql_secure_installation

Output:

[root@server ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

Once secured, you can connect to MySQL and review the existing databases on your database server by using the following command.

mysql -e "SHOW DATABASES;" -p

Output:

[root@server ~]# mysql -e "SHOW DATABASES;" -p
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

This concludes our topic of installing LAMP stack on AlmaLinux 9.