A LAMP stack (Linux, Apache, MySQL/MariaDB, PHP) is one of the most popular web development platforms. This guide will walk you through setting up a complete LAMP stack on AlmaLinux, creating a robust foundation for hosting dynamic websites and web applications.
Prerequisites
Before starting, ensure you have:
- A fresh installation of AlmaLinux
- Root or sudo access
- A stable internet connection
- Basic knowledge of command line operations
Step 1: Update System Packages
Start by updating your system to ensure all packages are current:
sudo dnf update -y
This ensures you have the latest security patches and bug fixes.
Step 2: Install Apache Web Server
Apache is one of the most popular web servers in the world. Install it using:
sudo dnf install httpd -y
Start and Enable Apache
Start the Apache service:
sudo systemctl start httpd
Enable Apache to start automatically on boot:
sudo systemctl enable httpd
Configure Firewall
Allow HTTP and HTTPS traffic through the firewall:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Test Apache
Open your web browser and navigate to your server’s IP address:
http://your-server-ip
You should see the default AlmaLinux Apache welcome page.
Step 3: Install MySQL (MariaDB) Database Server
AlmaLinux uses MariaDB, a drop-in replacement for MySQL:
sudo dnf install mariadb-server mariadb -y
Start and Enable MariaDB
sudo systemctl start mariadb
sudo systemctl enable mariadb
Secure MariaDB Installation
Run the security script to remove insecure defaults:
sudo mysql_secure_installation
Follow the prompts to:
- Set a root password
- Remove anonymous users
- Disallow root login remotely
- Remove test database
- Reload privilege tables
Step 4: Install PHP
Install PHP along with commonly used modules:
sudo dnf install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring -y
Restart Apache
Restart Apache to load the PHP module:
sudo systemctl restart httpd
Step 5: Test PHP Processing
Create a test PHP file to verify PHP is working:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Navigate to:
http://your-server-ip/info.php
You should see the PHP information page displaying PHP version and configuration details.
Remove Test File
For security reasons, remove the test file after verification:
sudo rm /var/www/html/info.php
Step 6: Configure Virtual Hosts (Optional)
To host multiple websites, set up virtual hosts:
Create Directory Structure
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www
Create Virtual Host Configuration
sudo nano /etc/httpd/conf.d/example.com.conf
Add the following configuration:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/html
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
Test and Reload Apache
Test configuration:
sudo apachectl configtest
Reload Apache:
sudo systemctl reload httpd
Step 7: Optimize Your LAMP Stack
Enable Apache Modules
Enable useful Apache modules:
sudo dnf install mod_ssl mod_security -y
Configure PHP Settings
Edit PHP configuration for better performance:
sudo nano /etc/php.ini
Recommended settings:
memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
Enable OPcache
Ensure OPcache is enabled for better PHP performance:
sudo nano /etc/php.d/10-opcache.ini
Add or modify:
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
Testing Your LAMP Stack
Create a simple PHP application to test database connectivity:
sudo nano /var/www/html/test.php
Add the following code:
<?php
$servername = "localhost";
$username = "root";
$password = "your_password";
try {
$conn = new PDO("mysql:host=$servername", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully to MariaDB!";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Security Best Practices
Hide Apache Version
Edit Apache configuration:
sudo nano /etc/httpd/conf/httpd.conf
Add:
ServerTokens Prod
ServerSignature Off
Secure PHP
Hide PHP version:
sudo nano /etc/php.ini
Set:
expose_php = Off
Regular Updates
Set up automatic security updates:
sudo dnf install dnf-automatic -y
sudo systemctl enable dnf-automatic.timer
sudo systemctl start dnf-automatic.timer
Monitoring and Maintenance
Monitor Apache Status
Enable Apache status module:
sudo nano /etc/httpd/conf.d/status.conf
Add:
<Location "/server-status">
SetHandler server-status
Require local
</Location>
Log Rotation
Logs are automatically rotated by logrotate. Check configuration:
cat /etc/logrotate.d/httpd
Conclusion
Congratulations! You’ve successfully set up a complete LAMP stack on AlmaLinux. Your server is now ready to host dynamic websites and web applications. Remember to:
- Keep your system updated regularly
- Monitor server logs for issues
- Implement proper security measures
- Create regular backups of your data
- Test your applications thoroughly before deploying to production
With this LAMP stack, you have a solid foundation for web development projects ranging from simple websites to complex web applications.