centos

centos lamp如何安装apache

小樊
39
2025-10-28 11:03:08
栏目: 智能运维

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:

1. Update System Packages

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

2. Install Apache (httpd)

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

3. Start Apache Service

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

4. Enable Apache to Start on Boot

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

5. Configure Firewall to Allow HTTP/HTTPS Traffic

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

6. Verify Apache Installation

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.

Optional: Post-Installation Configuration

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.

0
看了该问题的人还看了