How to Install LAMP Stack on CentOS
In this article
This guide walks you through installing a complete LAMP stack (Linux, Apache, MySQL/MariaDB, PHP) on CentOS. By the end, you'll have a working web server capable of serving dynamic PHP applications. You'll need a CentOS 7 or 8 system with root or sudo access and a terminal. No prior web server experience is required.
What You'll Need
- A CentOS 7 or 8 server or virtual machine
- Root access or a user with sudo privileges
- An active internet connection to download packages
- Basic familiarity with the command line
Step-by-Step
- Update your system
Runsudo yum update -y(CentOS 7) orsudo dnf update -y(CentOS 8) to refresh package lists and apply security patches. This ensures compatibility and reduces the chance of conflicts. - Install Apache web server
Usesudo yum install httpd -y(CentOS 7) orsudo dnf install httpd -y(CentOS 8). Then start and enable it:sudo systemctl start httpdandsudo systemctl enable httpd. - Adjust firewall rules
If firewalld is active, allow HTTP and HTTPS:sudo firewall-cmd --permanent --add-service=httpandsudo firewall-cmd --permanent --add-service=https, then reload withsudo firewall-cmd --reload. - Install MariaDB (MySQL-compatible database)
Runsudo yum install mariadb-server mariadb -y(CentOS 7) orsudo dnf install mariadb-server -y(CentOS 8). Start and enable it:sudo systemctl start mariadbandsudo systemctl enable mariadb. - Secure the database
Runsudo mysql_secure_installationand follow the prompts: set a root password, remove anonymous users, disallow remote root login, remove test database, and reload privileges. This is critical for production. - Install PHP
Install PHP and common modules:sudo yum install php php-mysqlnd php-fpm php-common -y(CentOS 7) orsudo dnf install php php-mysqlnd php-fpm -y(CentOS 8). For CentOS 8, you may also needphp-jsonandphp-mbstringif not included. - Restart Apache
After installing PHP, restart Apache to load the new module:sudo systemctl restart httpd. - Test the installation
Create a test PHP file:sudo nano /var/www/html/info.phpand add<?php phpinfo(); ?>. Save and exit. Then visithttp://your_server_ip/info.phpin a browser. You should see the PHP information page. Delete the file afterwards:sudo rm /var/www/html/info.php. - Set correct file ownership (optional but recommended)
For a typical web root, set ownership to the Apache user:sudo chown -R apache:apache /var/www/html.
Common Mistakes
- Forgetting to enable services – If you don't run
systemctl enable, Apache and MariaDB won't start automatically after a reboot. - Skipping firewall rules – Even if Apache runs, external clients can't reach it if the firewall blocks port 80.
- Using MySQL instead of MariaDB – CentOS 7/8 ship with MariaDB as the default MySQL replacement. Installing the MySQL package from Oracle can cause conflicts.
- Not restarting Apache after PHP install – Apache won't recognize PHP until it's restarted.
- Leaving the test file in place – The
info.phpfile exposes sensitive PHP configuration details; remove it after testing.
Tips
- Use
sudo systemctl status httpdandsudo systemctl status mariadbto verify services are running without errors. - For CentOS 8, consider using the
dnfcommand instead ofyum; it's the default package manager and has faster dependency resolution. - If you need PHP 7.4 or later on CentOS 7, you may need to enable the EPEL or Remi repository, but the default version (PHP 5.4 on CentOS 7) works for many legacy apps.
- Set a strong database root password during the secure installation step; don't leave it blank.
- Document your installed versions with
httpd -v,mysql --version, andphp -vfor future reference.
Troubleshooting
- Apache won't start – Check for port conflicts with
sudo netstat -tulpn | grep :80. If another service uses port 80, stop it or change Apache's port in/etc/httpd/conf/httpd.conf. - PHP file downloads instead of executing – This usually means PHP isn't installed or Apache isn't loading the PHP module. Reinstall PHP and restart Apache.
- Database connection fails – Ensure MariaDB is running:
sudo systemctl status mariadb. Also verify you used the correct host (usually 'localhost') and credentials. - Cannot access from another computer – Check firewall rules and confirm the server's IP address. Also test locally with
curl http://localhostto isolate the issue. - Permission denied errors – If files are not readable by Apache, adjust ownership:
sudo chown -R apache:apache /var/www/htmland set permissions to 755 for directories and 644 for files. - yum/dnf command not found – On minimal CentOS installs, ensure you're using the correct package manager. If both fail, check your network connection and repositories.
Frequently Asked Questions
What is the difference between MySQL and MariaDB?
MariaDB is a community-developed fork of MySQL, maintained by the original MySQL developers. CentOS includes MariaDB as the default database server, and it is fully compatible with MySQL for most use cases.
Do I need to install PHP separately if I install Apache?
Yes, Apache and PHP are separate packages. Apache serves static files, while PHP is a scripting language that runs dynamic content. You must install both and configure Apache to process PHP files.
Can I use Nginx instead of Apache with this stack?
Yes, but that would be a LEMP stack (Linux, Nginx, MySQL, PHP). This guide focuses on Apache, but the database and PHP installation steps are similar.
How do I change the web root directory?
Edit /etc/httpd/conf/httpd.conf and modify the DocumentRoot directive. Also update the corresponding <Directory> block. Then restart Apache.
Is it safe to use the root user for MySQL?
For production, create a dedicated database user with limited privileges. The root account should only be used for administration, not for application connections.
How do I install additional PHP extensions?
Use sudo yum install php-extension-name (or dnf on CentOS 8). Common ones include php-gd, php-xml, and php-mbstring. After installing, restart Apache.