How to Install the LAMP Stack on Linux — A Fast, Step-by-Step Guide
Hosting a website or app on a VPS? This fast, step-by-step guide shows how to install LAMP stack on common Linux distributions and secure it for production.
Setting up a reliable web hosting environment is a foundational task for system administrators, developers, and site owners. The LAMP stack — Linux, Apache, MySQL/MariaDB, and PHP — remains one of the most widely used platform choices due to its maturity, compatibility and flexibility. This guide provides a fast, step-by-step walkthrough to install and configure a production-ready LAMP stack on common Linux distributions, together with practical explanations on architecture, use cases, security hardening and deployment suggestions for VPS-based hosting.
Understanding the LAMP stack: components and how they interact
The LAMP acronym represents four layers that together power many web applications:
- Linux — the operating system that provides process isolation, network stack, and package management.
- Apache — the HTTP server that accepts client requests, serves static assets, and passes dynamic requests to PHP.
- MySQL/MariaDB — the relational database backend that stores application data.
- PHP — the server-side scripting language that generates dynamic content.
Request flow: a client connects to Apache, which either serves a static file or invokes PHP to render dynamic output. PHP interacts with the database to fetch or modify data. Understanding this flow is important when troubleshooting performance or security issues.
When LAMP is the right choice
LAMP is appropriate for a wide range of hosting scenarios:
- Traditional CMS-driven sites (WordPress, Drupal, Joomla).
- Custom PHP applications or frameworks that expect Apache (Laravel, Symfony with mod_rewrite support).
- Sites where tight OS-level control is required — typical for VPS or dedicated servers.
- Environments that prioritize straightforward upgrades, large ecosystem support and predictable behavior.
For microservices, container-first architectures or applications built in other languages (Node.js, Python, Go), consider alternatives. However, for many content-driven websites and small-to-medium enterprise applications, LAMP continues to be a practical, cost-effective solution.
Prerequisites and planning
Before installing, make decisions on the following:
- Linux distribution: This guide covers Debian/Ubuntu and RHEL/CentOS/AlmaLinux — pick the distro you manage.
- Database choice: MySQL vs MariaDB — MariaDB is a drop-in replacement with additional features; many distributions ship MariaDB by default.
- PHP execution model: mod_php (Apache module) vs PHP-FPM + mod_proxy_fcgi — PHP-FPM is recommended for better resource isolation and support for multiple PHP versions.
- SSL termination: Use Certbot/Let’s Encrypt or other CA to provide HTTPS.
- VPS specs: evaluate CPU, RAM and disk I/O relative to expected traffic. Use SSD-backed VPS for better performance.
Step-by-step installation (Debian/Ubuntu)
1) Update the system and install Apache:
Use the package manager to update repositories and install components. Example commands: ‘sudo apt update && sudo apt upgrade’, ‘sudo apt install apache2’.
2) Install MariaDB or MySQL:
Install the database server: ‘sudo apt install mariadb-server’ then run the secure installation: ‘sudo mysql_secure_installation’ to set the root password, remove anonymous users, disallow remote root login and remove test databases.
3) Install PHP and recommended extensions:
Install PHP-FPM and common extensions: ‘sudo apt install php-fpm php-mysql php-cli php-curl php-xml php-mbstring php-gd’. Configure Apache to use PHP-FPM via the ‘proxy_fcgi’ and ‘setenvif’ modules; enable the necessary Apache modules and reload: ‘sudo a2enmod proxy_fcgi setenvif’, ‘sudo a2enconf php7.4-fpm’ (replace version as appropriate), then ‘sudo systemctl reload apache2’.
4) Configure a sample virtual host:
Create a site directory under ‘/var/www/example.com/public_html’ and set ownership. Create a virtual host file in ‘/etc/apache2/sites-available/example.com.conf’ with directives such as ServerName, DocumentRoot, and ProxyPassMatch to route PHP files to the PHP-FPM socket. Enable the site with ‘sudo a2ensite example.com’ and reload Apache.
5) Test PHP processing and database connectivity:
Create a phpinfo script in the webroot, e.g. ‘info.php’ with ”, then visit it via browser to verify PHP is working. Test connecting to the database using the MySQL client and a test user with appropriate privileges.
Step-by-step installation (RHEL/CentOS/AlmaLinux)
1) Update packages and install Apache:
Use ‘sudo dnf update’ (or ‘yum’ on older systems). Install Apache with ‘sudo dnf install httpd’ then start and enable the service: ‘sudo systemctl enable –now httpd’.
2) Install MariaDB:
Install the server package: ‘sudo dnf install mariadb-server’ then enable and secure: ‘sudo systemctl enable –now mariadb’ and run the secure script ‘sudo mysql_secure_installation’.
3) Install PHP and PHP-FPM:
Install PHP-FPM and modules: ‘sudo dnf install php php-fpm php-mysqlnd php-xml php-gd php-mbstring’. Configure Apache to use PHP-FPM by enabling the ‘proxy_fcgi’ connector and configuring a virtual host to pass requests to ‘127.0.0.1:9000’ or the FPM socket. Start and enable PHP-FPM with ‘sudo systemctl enable –now php-fpm’.
4) SELinux considerations:
If SELinux is enabled, allow Apache to make network connections and use the HTTPD script execution context: ‘sudo setsebool -P httpd_can_network_connect on’, and adjust file contexts with ‘semanage fcontext’ and ‘restorecon’ if necessary.
Security hardening and best practices
Securing a LAMP server is essential for production environments. Key hardening steps:
- Least privilege: run services with default service accounts and grant narrow database user permissions (avoid using root/DB root for applications).
- Firewall: permit only necessary ports (80, 443, and SSH). Use ‘ufw’ on Debian/Ubuntu or firewalld on RHEL-based systems.
- SSH security: disable root login, use key-based auth, change default port if desired and use fail2ban to block bruteforce attempts.
- HTTPS: obtain certificates from Let’s Encrypt using Certbot and enforce HSTS and modern TLS ciphers.
- Keep packages updated: apply security updates promptly and consider unattended-upgrades for critical patches.
- Web server configuration: turn off directory listing, restrict file upload paths, disable unnecessary modules and configure proper ‘ServerTokens’ and ‘ServerSignature’ to reduce information leakage.
- Database remote access: avoid binding the database to 0.0.0.0 unless necessary. If remote access is required, secure it with TLS and firewall rules.
Performance tuning for production
To get the most out of a VPS-hosted LAMP stack, tune both Apache and PHP-FPM:
- Apache MPM choice: prefer the event or worker MPM in high-concurrency scenarios and combine with PHP-FPM rather than mod_php.
- KeepAlive and timeouts: set optimized KeepAliveTimeout and MaxKeepAliveRequests to balance latency and connection counts.
- PHP-FPM pools: configure pool settings (‘pm’, ‘pm.max_children’, ‘pm.start_servers’, ‘pm.max_requests’) based on available RAM and memory consumption per PHP process.
- Static assets: offload static content to a CDN or at minimum leverage aggressive caching headers and compressed content (gzip or brotli).
- OPcache: enable and tune PHP OPcache to drastically reduce PHP compilation overhead.
- Database optimization: configure InnoDB buffer pool size to 60–80% of available memory (if DB is the main workload), add indexes based on query patterns, and enable slow query logging to identify hotspots.
Common troubleshooting steps
When something goes wrong, use a systematic approach:
- Check Apache logs: ‘/var/log/apache2/error.log’ or ‘/var/log/httpd/error_log’.
- Check PHP-FPM logs and status: ‘systemctl status php-fpm’ and the FPM slow log if enabled.
- Validate database connectivity: test with the CLI client and ensure correct credentials and host/port are used.
- Use ‘ss’ or ‘netstat’ to verify listening ports and sockets and ‘ps’ to inspect running processes.
- If SELinux is enforcing, inspect AVC denials with ‘ausearch’ or ‘journalctl’ and adjust policies accordingly.
Choosing the right VPS for your LAMP deployment
When planning infrastructure on a VPS provider, match resources to workload:
- CPU: dynamic PHP workloads benefit from higher single-core performance; choose CPU types accordingly.
- RAM: allocate RAM for Apache/PHP-FPM workers and database buffer pool; for small WordPress sites 1–2 GB may suffice, but production sites generally require 4+ GB.
- Disk I/O: use SSD-backed storage; consider IOPS needs for database-heavy applications and provision accordingly.
- Network: ensure low latency and adequate bandwidth if serving global traffic; consider CDN integration for static content.
- Backups and snapshots: enable regular backups and snapshot capability to recover quickly from failures or failed updates.
Advantages of LAMP compared to alternatives
LAMP’s strengths include:
- Maturity and ecosystem: decades of community development, extensive documentation and numerous third-party modules.
- Compatibility: wide support for popular CMSs and PHP frameworks.
- Flexibility: easy to tune, add modules, swap components (e.g., MariaDB vs MySQL) without major rework.
Consider alternatives like LEMP (Nginx instead of Apache) for lower memory footprint and static file performance, or container-based deployments for microservices. Nonetheless, for many sites, LAMP provides a sensible balance between familiarity and capability.
Summary and next steps
Installing a LAMP stack on a Linux VPS is a straightforward process, but production-readiness depends on correct configuration, security hardening, and ongoing tuning. Use PHP-FPM for better process isolation, secure the database and server services, enable HTTPS and tune Apache/PHP and database settings to suit your workload. Monitor logs and resource usage and adopt incremental optimizations as traffic grows.
For site owners and developers looking to deploy quickly on reliable infrastructure, consider provisioning a VPS with a trusted provider. For example, VPS.DO offers a range of VPS plans in the USA optimized for web hosting and application deployments — see the USA VPS options at https://vps.do/usa/. Choosing an appropriately sized VPS can save time on tuning and allow you to focus on application-level improvements.