Install LAMP Stack on AlmaLinux 9 with Easy Steps – OrcaCore

Posted on

Install LAMP Stack on AlmaLinux 9 with Easy Steps - OrcaCore

Install LAMP Stack on AlmaLinux 9 with Easy Steps – OrcaCore

This guide, brought to you by Orcacore, will walk you through the process of installing a LAMP stack on AlmaLinux 9. The LAMP stack is a prevalent open-source solution stack widely used in web development. Understanding how to install Install LAMP Stack on AlmaLinux 9 is a fundamental skill for any aspiring web developer or system administrator.

The LAMP stack comprises four essential components, each playing a critical role in establishing a fully functional web development environment. The acronym LAMP stands for:

  • Linux: The operating system. In this case, AlmaLinux 9.
  • Apache: The web server.
  • MySQL/MariaDB: The database management system.
  • PHP: The server-side scripting language.

These components work together to create dynamic, database-driven websites.

Before you begin, ensure you have the following prerequisites: You must log in to your server as a non-root user with sudo privileges and set up a basic firewall. You can refer to the Orcacore guide on the Initial Server Setup with AlmaLinux 9 for detailed instructions.

1. Install Apache Web Server

Apache serves as the foundation for your web server on AlmaLinux 9. Start by updating your local package index:

sudo dnf update -y

Next, install Apache using the following command:

sudo dnf install httpd -y

After the installation completes, start and enable the Apache service to ensure it runs automatically on boot:

sudo systemctl enable httpd
sudo systemctl start httpd

Verify that Apache is active and running:

sudo systemctl status httpd

The output should resemble this:

**Output**
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
     Active: **active** (**running**) since Tue 2022-09-20 03:48:24 EDT; 6s ago
       Docs: man:httpd.service(8)
   Main PID: 4651 (httpd)
     Status: "Started, listening on: port 80"
      Tasks: 213 (limit: 23609)
     Memory: 27.0M
        CPU: 123ms
     CGroup: /system.slice/httpd.service
...

Assuming you have enabled firewalld, allow HTTP traffic through the AlmaLinux firewall:

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

Reload the firewall to apply the new rules:

sudo firewall-cmd --reload

Now, access the default Apache page by entering your server’s IP address in your web browser:

http://server-IP-address

You should see the Apache test page.

2. Install MariaDB Database Engine

MariaDB will be used as the database server in this setup. MariaDB is readily available in the AlmaLinux 9 base repository. Install it using the dnf command:

sudo dnf install mariadb-server mariadb -y

Once the installation finishes, start and enable the MariaDB service:

sudo systemctl enable mariadb
sudo systemctl start mariadb

Verify that the service is running correctly:

sudo systemctl status mariadb
**Output**
● mariadb.service - MariaDB 10.5 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
     Active: **active** (**running**) since Tue 2022-09-20 03:53:05 EDT; 6s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 7431 ExecStartPre=/usr/libexec/mariadb-check-socket (code=exited, status=0/SUCCESS)
    Process: 7453 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/SUCCESS)
    Process: 7550 ExecStartPost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/SUCCESS)
   Main PID: 7535 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 14 (limit: 23609)
     Memory: 73.3M
...

Secure the MariaDB installation using the mysql_secure_installation script. This script will prompt you to set a root password, remove anonymous users, disallow remote root login, and remove the test database:

 sudo mysql_secure_installation
Enter current password for root (enter for none):
Set root password? [Y/n] Y
New password:
Re-enter new password:
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

Log in to the MariaDB console using the following command:

sudo mysql -u root -p
**Outtput**
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 9
Server version: 10.5.16-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]>

3. Set up PHP Server Scripting

AlmaLinux 9’s default repository provides PHP 8.0. Install PHP and common PHP extensions using this command:

sudo dnf install php php-curl php-bcmath php-gd php-soap php-zip php-curl php-mbstring php-mysqlnd php-gd php-xml php-intl php-zip -y

Verify the PHP installation by checking the version:

php -v
**Output**
PHP 8.0.13 (cli) (built: Nov 16 2021 18:07:21) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.13, Copyright (c) Zend Technologies
with Zend OPcache v8.0.13, Copyright (c), by Zend Technologies

Test PHP integration with the web server by creating a simple PHP file:

sudo vi /var/www/html/info.php

Add the following code to the file:

<?php

phpinfo();

?>

Save and close the file. Restart Apache to apply the changes:

sudo systemctl restart httpd

In your web browser, navigate to your server’s IP address followed by /info.php:

http://server-IP/info.php

You should see detailed PHP information.

Once you’ve confirmed PHP is working, remove the info.php file for security reasons:

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

Conclusion

Installing the Install LAMP Stack on AlmaLinux 9 provides a robust and reliable environment for hosting dynamic websites and web applications. It leverages the combined power of Linux, Apache, MySQL/MariaDB, and PHP. This guide provides a step-by-step approach to Install LAMP Stack on AlmaLinux 9.

Alternative Solutions for Installing a LAMP Stack on AlmaLinux 9

While the above method is a classic and effective way to install a LAMP stack, here are two alternative approaches:

1. Using Tasksel (If Available):

Tasksel is a Debian/Ubuntu tool that simplifies the installation of multiple related packages. While not natively available on AlmaLinux, it’s worth mentioning as a concept. A similar tool could be created or adapted for AlmaLinux. The idea is to provide a menu-driven interface to select pre-defined "tasks," such as installing a LAMP server. This simplifies the process for beginners and ensures all necessary packages are installed.

  • Explanation: Tasksel abstracts away the individual package installations and configurations. The user simply selects "LAMP server," and the tool handles the rest.

  • Hypothetical Code Example (Conceptual):

    # This is a conceptual example, Tasksel doesn't exist for AlmaLinux natively
    sudo dnf install tasksel  # If a Tasksel-like tool existed for AlmaLinux
    sudo tasksel
    # Then, in the Tasksel menu, select "LAMP Server"

    This approach would require significant development to create a Tasksel-like tool and define the LAMP server task.

2. Using Docker Compose:

Docker Compose allows you to define and manage multi-container Docker applications. This is a modern, highly portable, and reproducible way to set up a LAMP stack. Each component (Apache, MariaDB, PHP) runs in its own container, making it easier to manage and isolate dependencies.

  • Explanation: Docker containers provide isolated environments for each component of the LAMP stack. Docker Compose orchestrates the creation and linking of these containers, defining the network and volumes for persistent data.

  • Code Example (docker-compose.yml):

version: "3.8"
services:
  db:
    image: mariadb:10.6
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: your_root_password
      MYSQL_DATABASE: your_database_name
      MYSQL_USER: your_database_user
      MYSQL_PASSWORD: your_database_password
    volumes:
      - db_data:/var/lib/mysql
    ports:
      - "3306:3306"

  web:
    image: php:8.0-apache
    restart: always
    ports:
      - "80:80"
    volumes:
      - ./html:/var/www/html
    depends_on:
      - db
    environment:
      PHP_MYSQLND_HOST: db

volumes:
  db_data:

To use this, create a directory named html in the same directory as the docker-compose.yml file. Place your PHP files (like index.php) inside the html directory.

To start the LAMP stack:

docker-compose up -d

To stop the LAMP stack:

docker-compose down

This Docker Compose configuration defines two services: db (MariaDB) and web (PHP with Apache). The db service uses the mariadb:10.6 image, sets environment variables for the database credentials, and mounts a volume for persistent data. The web service uses the php:8.0-apache image, mounts your HTML/PHP files into the Apache document root, and depends on the db service. The depends_on instruction ensures that the database service is started before the web service.

The key advantage of Docker is its portability. This same docker-compose.yml file can be used to deploy the LAMP stack on any system with Docker installed, ensuring a consistent environment across development, testing, and production. This simplifies the process to Install LAMP Stack on AlmaLinux 9, or any other Linux distribution that supports Docker.