Current Location: Home> Latest Articles> Complete Guide to Configuring Apache and PHP on Linux

Complete Guide to Configuring Apache and PHP on Linux

gitbox 2025-06-27

Setting Up Apache and PHP on Linux

Apache (HTTPD) and PHP remain one of the most popular combinations for web development. This guide walks you through setting up a fully functional web server with Apache and PHP on a Linux system, from preparation to security optimization.

Preparing the Environment

Before beginning the configuration, ensure your system is up to date. Use the following commands to update Ubuntu or CentOS:

sudo apt update && sudo apt upgrade  # Ubuntu
sudo yum update                      # CentOS

Installing Apache HTTPD

On Ubuntu, use this command to install Apache:

sudo apt install apache2

For CentOS, run:

sudo yum install httpd

After installation, start the Apache service and enable it to launch on boot:

sudo systemctl start httpd
sudo systemctl enable httpd

Configuring Apache for PHP

To enable PHP support in Apache, edit the main configuration file. For CentOS:

/etc/httpd/conf/httpd.conf

For Ubuntu:

/etc/apache2/apache2.conf

Make sure to add the following line to support PHP file handling:

AddType application/x-httpd-php .php

Installing PHP and Modules

On Ubuntu, install PHP and necessary modules using:

sudo apt install php libapache2-mod-php php-mysql

On CentOS:

sudo yum install php php-mysql

Testing PHP Setup

To verify your PHP setup, create a test file in Apache's default root directory:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Then open http://your_server_ip/info.php in your browser. If you see the PHP configuration page, your setup was successful.

Security and Performance Optimization

After installation, enhance your system's security and performance with these practices:

  • Regularly update the system and installed packages to patch vulnerabilities
  • Configure a firewall to restrict unnecessary port access
  • Disable unused Apache modules to streamline performance

Conclusion

By following these steps, you have successfully set up Apache and PHP on a Linux server. This configuration is suitable for most web hosting and development needs. For long-term stability and security, keep your system updated and follow best practices for optimization.