How to install Apache, MySQL, and PHP (LAMP) on AlmaLinux

These are the steps to install and setup a LAMP - Linux, Apache, MySQL/MariaDB, PHP - server stack on AlmaLinux 8 and AlmaLinux 9. These instructions are also valid for CentOS Stream 8/9, and RHEL 8/9. 

1. Update your system

Update your system to ensure that all the packages are up to date and rebuild the repo cache. 

sudo dnf update -y

2. Install Apache

The next step is to install the Apache web server and some additional tools.

sudo dnf install httpd httpd-tools -y

Now you will want to start Apache and enable Apache to start when the system boots so you won't need to manually start Apache.

sudo systemctl enable --now httpd

Alternatively, you can run these as individual commands (Note: this is not necessary if you ran the line above).

sudo systemctl start httpd
sudo systemctl enable httpd

3. Update firewall rules

If you have firewalld running on your system, you will need to allow inbound access to Apache otherwise no one, including you, will be able to access your site. Run the following commands:

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

To check that the rules are open and the Apache server is working properly, you can now go to http://your-server-ip/ (replace "your-server-ip" with your server's IP address) and you should see the Apache Test Page.

4. Install MySQL/MariaDB

MariaDB is a fork of MySQL and, for the most part, is generally compatible with MySQL. Both database servers are available and you have the option to choose whichever one you want or if you have a specific need for one. 

Install MySQL:

sudo dnf install mysql mysql-server -y

Install MariaDB:

sudo dnf install mariadb mariadb-server -y

Start and Enable MySQL:

sudo systemctl enable --now mysqld

Alternatively, you can run these as individual commands (Note: this is not necessary if you ran the line above).

sudo systemctl start mysqld
sudo systemctl enable mysqld

Start and Enable MariaDB:

sudo systemctl enable --now mariadb

Alternatively, you can run these as individual commands (Note: this is not necessary if you ran the line above).

sudo systemctl start mariadb
sudo systemctl enable mariadb

5. Secure MySQL/MariaDB Installation

This is the same whether you are running MySQL or MariaDB. This step is important to secure MySQL/MariaDB. You'll be asked to set a root password (this can, and should be different from your server's root password), remove anonymous users, disallow root login remotely, remove test database, and reload the privilege tables. You should answer "Y" to all of these.

sudo mysql_secure_installation

6. Install PHP

Please note: PHP 7.x is EOL and out of support and is no longer receiving updates. We recommend installing at least PHP 8.1, but strongly recommend using PHP 8.2 if your application can support it.

Install EPEL

One of the prerequisites is to have EPEL installed. You can install it with the following command:

sudo dnf install epel-release -y

Enable PowerTools Repo (AlmaLinux 8)

You will also need to install the PowerTools repo on AlmaLinux 8. You can enable it with the following command:

sudo dnf config-manager --set-enabled powertools

Install Remi's Repo For Enterprise Linux 8

If you're running AlmaLinux 8, run the following command to install Remi's Repo for EL8.

sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm -y

Install Remi's Repo For Enterprise Linux 9

If you're running AlmaLinux 9, you will rull the following command:

sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-9.rpm -y

Reset PHP

Run the following command to reset the PHP version:

sudo dnf module reset php

Enable PHP 8.1 from Remi's Repo as Default PHP

Now you need to tell your system to use PHP 8.1 from Remi's Repo as the default version of PHP.

sudo dnf module enable php:remi-8.1  

Install PHP and Additional PHP Modules

Use the following command to install PHP and some common PHP modules most applications need:

sudo dnf install -y php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring php-zip

If you need to install any additional PHP modules, you can run the following:

sudo dnf install php-<module-name>

For example, if you needed to install the LDAP module, you would run sudo dnf install php-ldap

Restart Apache

After installing PHP, you may need to restart Apache in order to have Apache recognize PHP and the installed modules. You can use the following command:

sudo systemctl restart httpd

7. Test PHP

Now you can make sure that everything is working properly. Create a test.php file in /var/www/html/ and in that file, add in the following code:

<?php

phpinfo();
?>

Save the file and then attempt to browse to it at http://server-ip/test.php and you should see a page similar to the following:

Conclusion

You should now have a fully functional LAMP server. If you need additional features such as SSL, please review our guide on how to enable SSL support and install a certificate.

 

 

Article Information
  • Article ID: 386
  • Category: AlmaLinux
  • Viewed 26 times.
  • Rating:
    (0)
  • Was this article helpful?
  • Yes No
Did you find this article helpful?