Anasayfa / Software / How to Set Up a LAMP Stack on CentOS – A Complete Guide

How to Set Up a LAMP Stack on CentOS – A Complete Guide

technology

Setting up a LAMP stack on CentOS is one of the most rewarding ways to get a reliable web server up and running quickly. Whether you are hosting a personal blog, a small business site, or a testing environment for a larger application, the combination of Linux, Apache, MariaDB (or MySQL), and PHP gives you a powerful, flexible platform. In this guide we’ll walk through every step—from updating the OS to verifying that your first PHP page works—while highlighting common mistakes and sharing a few seasoned‑developer tricks.

What You’ll Need

  • A fresh installation of CentOS 7 or 8 (minimal install is fine)
  • Root or sudo privileges
  • Internet connectivity for package downloads
  • A basic understanding of Linux command line
  • A text editor such as vim, nano, or vi

Step 1: Update the System

Before you install anything, make sure the OS packages are up to date. This prevents version conflicts and pulls in the latest security patches.

Run the following commands as root or with sudo:

sudo yum update -y

On CentOS 8 the package manager is dnf, so you can also use:

sudo dnf update -y

After the update finishes, reboot the server to ensure the new kernel (if any) is loaded:

sudo reboot

Once the system is back online, log in again and continue with the next step.

Step 2: Install Apache (httpd)

Apache is the web server component of LAMP. Installing it on CentOS is straightforward.

Execute:

sudo yum install -y httpd

Start the service and enable it to launch at boot:

sudo systemctl start httpd

sudo systemctl enable httpd

Verify that Apache is running:

sudo systemctl status httpd

You should see active (running). By default Apache serves files from /var/www/html. Open a browser and navigate to http://your_server_ip/; you should see the default CentOS test page.

Step 3: Install MariaDB (MySQL compatible)

MariaDB provides the database layer. It’s a drop‑in replacement for MySQL and is the default on newer CentOS releases.

Install it with:

sudo yum install -y mariadb-server mariadb

Start and enable the service:

sudo systemctl start mariadb

sudo systemctl enable mariadb

Run the security script to set a root password, remove anonymous users, disallow remote root login, and delete test databases:

sudo mysql_secure_installation

The script will prompt you for several choices; answer “Y” to all except the one asking whether to change the root password if you already set one.

Test the connection:

mysql -u root -p

If you can log in, the database layer is ready.

Step 4: Install PHP and Required Extensions

PHP interprets the dynamic content that Apache serves. Install the core package plus a few common extensions:

sudo yum install -y php php-mysqlnd php-fpm php-cli php-common

On CentOS 8 you may need to enable the EPEL repository first:

sudo dnf install -y epel-release

After installation, restart Apache so it picks up the PHP module:

sudo systemctl restart httpd

Check the installed version:

php -v

You should see something like PHP 7.4.x (or newer).

Step 5: Test the Complete LAMP Stack

Create a simple PHP info file to confirm that Apache, PHP, and MariaDB can talk to each other.

Run:

echo "" | sudo tee /var/www/html/info.php

Now open http://your_server_ip/info.php in a browser. You should see the PHP information page, which includes sections for Apache and MySQL/MariaDB. If you see this page, your LAMP stack is functional.

For good hygiene, remove the file after testing:

sudo rm /var/www/html/info.php

Step 6: (Optional) Configure a Virtual Host

Most production setups use virtual hosts so you can host multiple sites on a single server. Create a new configuration file:

sudo nano /etc/httpd/conf.d/example.com.conf

Paste the following, adjusting ServerName and DocumentRoot as needed:

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog /var/www/example.com/error.log
CustomLog /var/www/example.com/requests.log combined
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Create the directory structure and a test index file:

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com/public_html
echo "<h1>Welcome to Example.com</h1>" > /var/www/example.com/public_html/index.html

Restart Apache:

sudo systemctl restart httpd

Now point your browser to http://example.com (or edit /etc/hosts for local testing) and you should see the welcome message.

Common Mistakes to Avoid

1. Skipping the update step. Installing packages on an outdated system often leads to dependency errors.

2. Forgetting to enable services at boot. If you reboot and Apache or MariaDB doesn’t start, you’ll waste precious troubleshooting time.

3. Leaving the default root MySQL password blank. The mysql_secure_installation script is essential for production security.

4. Mismatching PHP versions. Some applications require PHP 7.2+; installing an older version from the base repo can cause compatibility issues.

5. Incorrect file permissions. Apache runs as apache (or www-data on some distros). If your web root is owned by root, you may get 403 errors.

Tips and Tricks

• Use firewall-cmd --permanent --add-service=http and --add-service=https to open ports 80 and 443 on CentOS firewalld.

• Enable mod_rewrite for clean URLs: sudo yum install -y mod_rewrite and set AllowOverride All in your virtual host.

• Install phpMyAdmin from EPEL for a web‑based database manager: sudo yum install -y phpmyadmin.

• Keep your PHP extensions lean. Only install what your application truly needs; extra modules increase the attack surface.

• Regularly back up /var/www and your MariaDB databases using mysqldump and tar.

Frequently Asked Questions

Can I use MySQL instead of MariaDB?

Yes. On CentOS you can replace mariadb-server with mysql-community-server from the official MySQL repo, but MariaDB is fully compatible and receives faster updates in the default repositories.

Do I need SELinux disabled for LAMP?

No. SELinux can stay enabled; you just need to apply the correct contexts. For example, after creating a new web directory run sudo restorecon -R -v /var/www/example.com to label files correctly.

How do I enable HTTPS on this stack?

Install mod_ssl and obtain a certificate from Let’s Encrypt:

sudo yum install -y mod_ssl certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com

The tool will automatically edit your virtual host to listen on port 443 and configure redirection from HTTP.

Conclusion

With the steps above you now have a fully functional LAMP stack on CentOS, ready to host dynamic web applications. By keeping the system updated, securing MariaDB, and following the best‑practice tips, you’ll enjoy a stable, secure environment that scales as your projects grow. Happy coding!

Photo by Portraits of Modern Dairy on Unsplash

Etiketlendi: