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.
First, download the latest stable source packages from the official Apache and PHP websites. Make sure to use official releases for security and stability.
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
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
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
Edit the Apache configuration file httpd.conf to set listening ports, virtual hosts, and other parameters according to your needs.
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
Edit the PHP configuration file php.ini to adjust memory limits, upload sizes, and other settings based on your project requirements.
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.
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.