TechBuzzPro

How to Install LAMP Stack on CentOS

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Update your system
    Run sudo yum update -y (CentOS 7) or sudo dnf update -y (CentOS 8) to refresh package lists and apply security patches. This ensures compatibility and reduces the chance of conflicts.
  2. Install Apache web server
    Use sudo yum install httpd -y (CentOS 7) or sudo dnf install httpd -y (CentOS 8). Then start and enable it: sudo systemctl start httpd and sudo systemctl enable httpd.
  3. Adjust firewall rules
    If firewalld is active, allow HTTP and HTTPS: sudo firewall-cmd --permanent --add-service=http and sudo firewall-cmd --permanent --add-service=https, then reload with sudo firewall-cmd --reload.
  4. Install MariaDB (MySQL-compatible database)
    Run sudo yum install mariadb-server mariadb -y (CentOS 7) or sudo dnf install mariadb-server -y (CentOS 8). Start and enable it: sudo systemctl start mariadb and sudo systemctl enable mariadb.
  5. Secure the database
    Run sudo mysql_secure_installation and 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.
  6. Install PHP
    Install PHP and common modules: sudo yum install php php-mysqlnd php-fpm php-common -y (CentOS 7) or sudo dnf install php php-mysqlnd php-fpm -y (CentOS 8). For CentOS 8, you may also need php-json and php-mbstring if not included.
  7. Restart Apache
    After installing PHP, restart Apache to load the new module: sudo systemctl restart httpd.
  8. Test the installation
    Create a test PHP file: sudo nano /var/www/html/info.php and add <?php phpinfo(); ?>. Save and exit. Then visit http://your_server_ip/info.php in a browser. You should see the PHP information page. Delete the file afterwards: sudo rm /var/www/html/info.php.
  9. 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.php file exposes sensitive PHP configuration details; remove it after testing.

Tips

  • Use sudo systemctl status httpd and sudo systemctl status mariadb to verify services are running without errors.
  • For CentOS 8, consider using the dnf command instead of yum; 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, and php -v for 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://localhost to isolate the issue.
  • Permission denied errors – If files are not readable by Apache, adjust ownership: sudo chown -R apache:apache /var/www/html and 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.