Set Up a Complete LAMP Environment on CentOS

Comprehensive Guide: Setting Up a Complete LAMP Environment on CentOS

This article provides a step-by-step, in-depth walkthrough to deploy a fully functional LAMP (Linux, Apache, MariaDB/MySQL, PHP) stack on a CentOS server. Ideal for developers, system administrators, and enthusiasts seeking a robust web hosting platform.

1. System Prerequisites and Initial Preparation

  • CentOS Version: This guide targets CentOS 7 and CentOS 8. Confirm your version via cat /etc/centos-release.
  • Root or Sudo Access: A user with sudo privileges or direct root login.
  • Network Configuration: Static IP or DHCP reservation. Ensure you can SSH into your server:
    ssh user@your_server_ip
  • Firewall SELinux: We will configure firewalld and SELinux policies for secure operation.

1.1 Update the System

sudo yum update -y

Reboot if kernel or critical packages were upgraded:

sudo reboot

2. Installing Apache HTTP Server

2.1 Install and Enable Apache

sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd

2.2 Adjust Firewall for HTTP/S

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

2.3 Verify Apache is Running

  • Access http://your_server_ip in a browser. You should see the default CentOS Apache page.
  • Service status: sudo systemctl status httpd.

3. Installing MariaDB (or MySQL)

3.1 Install and Start Database Server

sudo yum install mariadb-server -y
sudo systemctl start mariadb
sudo systemctl enable mariadb

3.2 Secure the Installation

Run the interactive security script:

sudo mysql_secure_installation
  • Set a strong root password.
  • Remove anonymous users.
  • Disallow remote root login.
  • Remove test databases.
  • Reload privilege tables.

4. Installing PHP and Extensions

4.1 Install PHP Package

sudo yum install php php-mysqlnd php-fpm php-xml php-mbstring -y

4.2 Configure PHP-FPM (Optional)

If you prefer php-fpm with mod_proxy_fcgi:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

4.3 Restart Apache

sudo systemctl restart httpd

5. Testing the LAMP Stack

5.1 Create a PHP Info Page

ltphp
phpinfo()
gt

Save as /var/www/html/info.php and access http://your_server_ip/info.php.

5.2 Database Connection Test

  • Log in to MariaDB: mysql -u root -p.
  • Create a test database and user, then connect via PHP script or CLI.

6. Fine-Tuning Security and Performance

6.1 SELinux Configuration

  • Allow HTTPD scripts to connect to the database:
    sudo setsebool -P httpd_can_network_connect_db on
  • If using php-fpm or custom document roots, update file contexts:
    sudo semanage fcontext -a -t httpd_sys_rw_content_t /var/www/html(/.)
    sudo restorecon -Rv /var/www/html

6.2 Performance Tuning

Component Tuning Tip
Apache Adjust MaxKeepAliveRequests and KeepAliveTimeout in /etc/httpd/conf/httpd.conf.
MariaDB Enable query cache, optimize innodb_buffer_pool_size in /etc/my.cnf.d/server.cnf.
PHP Use OPcache – enable and tune in /etc/php.d/10-opcache.ini.

7. Optional: phpMyAdmin Installation

  • Install EPEL repo: sudo yum install epel-release -y
  • Install phpMyAdmin: sudo yum install phpmyadmin -y
  • Configure access by editing /etc/httpd/conf.d/phpMyAdmin.conf to allow your IP or VPN range.
  • Restart Apache: sudo systemctl restart httpd.

8. Remote Access and Secure Management

For administrative tasks and secure data transfer, utilize a reliable VPN solution:

  • ExpressVPN – high-speed servers and robust encryption.
  • NordVPN – advanced security features and audit-proof privacy.

9. Backup and Maintenance Strategies

  • Automate database backups via cron and mysqldump.
  • Rotate Apache and PHP logs using logrotate.
  • Perform periodic yum updates and security scans (Lynis, ClamAV).
  • Monitor resource usage with htop, vnstat, and netdata.

10. Conclusion

By following these steps, you will have deployed a secure, high-performance LAMP environment on CentOS, suitable for production workloads and development projects. Customize configurations further to match your unique application requirements. Happy hosting!

Download TXT




Leave a Reply

Your email address will not be published. Required fields are marked *