Current Location: Home> Latest Articles> Complete Guide to Compiling and Installing Apache and PHP from Source for Easy Environment Setup

Complete Guide to Compiling and Installing Apache and PHP from Source for Easy Environment Setup

M66 2025-08-07

Overview of Compiling and Installing Apache and PHP from Source

Apache and PHP are essential components in web development. Compiling from source allows for more flexible configuration and optimized performance. This article guides you step-by-step through the process of compiling and installing Apache and PHP, with practical command examples and configuration advice.

Downloading Apache and PHP Source Packages

First, download the latest stable source packages from the official Apache and PHP websites. Make sure to use official releases for security and stability.

Extracting Source Packages

After downloading, extract the source packages to your desired directory using commands like:

tar -zxvf httpd-2.4.46.tar.gz
tar -zxvf php-7.4.12.tar.gz

Installing Dependencies

Before compiling Apache and PHP, install the necessary dependencies. On Ubuntu, update your packages and install required libraries with:

sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libapr1-dev libaprutil1-dev

Compiling and Installing Apache

Navigate to the Apache source directory and run the configure script, then compile and install:

./configure --prefix=/usr/local/apache2
make
sudo make install

Once installed, start the Apache service with:

sudo /usr/local/apache2/bin/apachectl start

Configuring Apache

Edit the Apache configuration file httpd.conf to set listening ports, virtual hosts, and other parameters according to your needs.

Compiling and Installing PHP

Navigate to the PHP source directory, run the configure script with Apache integration options, then compile and install:

./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl
make
sudo make install

Configuring PHP

Edit the PHP configuration file php.ini to adjust memory limits, upload sizes, and other settings based on your project requirements.

Testing Apache and PHP

Create a test PHP file named info.php with the following content:

<?php
phpinfo();
?>

Place this file in Apache’s default web root directory (typically /usr/local/apache2/htdocs/) and visit http://localhost/info.php in your browser. If the PHP information page appears, your setup is successful.

Summary

This guide has helped you complete the compilation, installation, and basic configuration of Apache and PHP from source, allowing you to build a customized development environment. For continued use, consider further optimizing configurations and security settings as needed.