Installing Apache in CentOS for LAMP Stack
Apache is a core component of the LAMP (Linux, Apache, MySQL, PHP) stack, serving as the web server that processes and delivers web content. Below are the detailed steps to install Apache on CentOS for a LAMP environment:
Before installing Apache, update your system to ensure all existing packages are up-to-date. This helps avoid compatibility issues and installs the latest security patches. Run the following command as the root user or with sudo privileges:
sudo yum update -y
Apache is available in CentOS’s default YUM repository. Use the yum package manager to install the httpd package (the RPM package name for Apache). The -y flag automatically confirms installation prompts to streamline the process:
sudo yum install httpd -y
Once the installation completes, start the Apache service immediately to begin serving web content. Use the systemctl command (the modern way to manage services in CentOS 7/8) to start the service:
sudo systemctl start httpd
To ensure Apache launches automatically after a system reboot (critical for production environments), enable it to start on boot. This command creates a symlink from the Apache service file to the system’s startup directory:
sudo systemctl enable httpd
CentOS systems often have firewalld enabled by default, which blocks incoming traffic to unauthorized ports. To allow web traffic (HTTP on port 80 and HTTPS on port 443), add the corresponding services to the public zone and reload the firewall rules:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
After completing the above steps, verify that Apache is running correctly. Open a web browser and navigate to your server’s IP address (e.g., http://192.168.1.100). You should see the default Apache welcome page (a blue screen with “Testing 123” or similar text), confirming that the installation was successful.
sudo systemctl status httpd
Look for “active (running)” in the output./etc/httpd/conf/httpd.conf. Use a text editor (e.g., vi or nano) to modify settings like the default port (Listen directive) or document root (DocumentRoot)./etc/httpd/conf.d/ (e.g., example.com.conf). Each file should include directives like ServerName, DocumentRoot, and ErrorLog to define site-specific behavior.sudo setenforce 0
Or permanently disable it by editing /etc/selinux/config and setting SELINUX=disabled.By following these steps, you’ll have a fully functional Apache web server installed on your CentOS system, ready to serve content for your LAMP stack.