Table of Contents#
- Prerequisites
- Installing Apache
- Installing MariaDB
- Installing PHP
- Configuring PHP with Apache
- Testing the LAMP Stack
- Conclusion
- References
1. Prerequisites#
Before we begin, make sure you have the following:
- A server running AlmaLinux 8.4. You can use a physical server, a virtual machine (VM), or a cloud instance.
- Root or sudo privileges. If you're not logged in as root, you'll need to use the
sudocommand before each administrative operation.
2. Installing Apache#
Apache is the web server component of the LAMP stack. To install it, follow these steps:
Step 1: Update the System#
First, update the package lists on your AlmaLinux system:
sudo dnf update -yThis ensures that you have the latest package information and security updates.
Step 2: Install Apache#
Now, install the Apache web server:
sudo dnf install httpd -yThe -y option automatically answers "yes" to any prompts during the installation.
Step 3: Start and Enable Apache#
After the installation is complete, start the Apache service:
sudo systemctl start httpdTo make sure Apache starts automatically when the server boots, enable it:
sudo systemctl enable httpdStep 4: Check Apache Status#
Verify that Apache is running correctly by checking its status:
sudo systemctl status httpdYou should see a message indicating that the service is active (running).
3. Installing MariaDB#
MariaDB is the database management system in the LAMP stack. Here's how to install it:
Step 1: Install MariaDB Server#
Install the MariaDB server package:
sudo dnf install mariadb-server -yStep 2: Start and Enable MariaDB#
Start the MariaDB service:
sudo systemctl start mariadbEnable it to start on boot:
sudo systemctl enable mariadbStep 3: Secure MariaDB Installation#
Run the MariaDB security script to set a root password and improve security:
sudo mysql_secure_installationYou'll be prompted to answer several questions, such as setting a root password, removing anonymous users, disabling remote root login, and removing the test database. Follow the prompts and enter appropriate values.
4. Installing PHP#
PHP is the programming language used to create dynamic web pages. Let's install it:
Step 1: Install PHP and Extensions#
Install PHP along with common extensions (like php-mysqlnd for database connectivity):
sudo dnf install php php-mysqlnd -yYou can install additional PHP extensions as needed for your specific web applications (e.g., php-gd for image processing, php-xml for XML handling).
5. Configuring PHP with Apache#
Step 1: Restart Apache#
After installing PHP, restart Apache to load the PHP module:
sudo systemctl restart httpdStep 2: Create a Test PHP File#
Create a test PHP file in the Apache document root (/var/www/html). For example:
sudo nano /var/www/html/info.phpAdd the following content to the file:
<?php
phpinfo();
?>Save the file (press Ctrl+X, then Y, then Enter in nano).
6. Testing the LAMP Stack#
Step 1: Test Apache#
Open a web browser and enter your server's IP address (e.g., http://your_server_ip). You should see the default Apache test page.
Step 2: Test PHP#
Now, visit the test PHP file you created. Enter http://your_server_ip/info.php in your web browser. You should see a page displaying detailed information about your PHP installation.
Step 3: Test MariaDB (Optional)#
You can also test MariaDB connectivity. Create a simple PHP script to connect to the MariaDB database (replace your_username, your_password, and your_database with appropriate values):
sudo nano /var/www/html/test_mysql.phpAdd the following code:
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ". $conn->connect_error);
}
echo "Connected successfully";
$conn->close();
?>Save the file and access it in your web browser (http://your_server_ip/test_mysql.php). If everything is configured correctly, you should see the "Connected successfully" message.
7. Conclusion#
Congratulations! You've successfully installed the LAMP stack on AlmaLinux 8.4. You can now start developing and hosting web applications using this powerful combination. Remember to keep your software updated regularly to ensure security and performance.