Current Location: Home> Latest Articles> How to Compile and Install Apache and PHP in a Specific Directory | Complete Guide

How to Compile and Install Apache and PHP in a Specific Directory | Complete Guide

M66 2025-07-18

How to Compile and Install Apache and PHP in a Specific Directory

When setting up a website server, Apache and PHP are two crucial components. This guide will walk you through how to compile and install these two programs in a specific directory, helping you perform customized configurations on a Linux environment.

Preparation

Before installing Apache and PHP, make sure your system has the necessary compilation tools and dependencies. Typically, you will need the following packages:

  • gcc: Used for compiling source code
  • make: Used for building software
  • autoconf: Used to generate configure scripts
  • libtool: Used to generate executable programs
  • apr and apr-util: Auxiliary libraries required by Apache

You can install these packages using your package manager. For example, on Ubuntu, use the following commands:

sudo apt-get update
sudo apt-get install gcc make autoconf libtool libapr1 libaprutil1

Download Source Code

Download the required version of Apache and PHP from the official websites. In this example, we use Apache 2.4.46 and PHP 7.4.11, but feel free to choose other versions if needed:

wget https://downloads.apache.org/httpd/httpd-2.4.46.tar.gz
wget https://www.php.net/distributions/php-7.4.11.tar.gz

Extract Source Code

After downloading the source code, extract the downloaded files and navigate to the respective source code directories:

tar -xzvf httpd-2.4.46.tar.gz
tar -xzvf php-7.4.11.tar.gz
cd httpd-2.4.46

Compile and Install Apache

Now, compile and install Apache. First, run the configure script to generate the Makefile:

./configure --prefix=/path/to/install/apachepath

Here, “/path/to/install/apachepath” is the directory path where you want to install Apache. Then run the make command to compile and install:

make
sudo make install

Once the installation is complete, start the Apache server:

/path/to/install/apachepath/bin/apachectl start

Configure Apache

After installing Apache, you will need to edit the httpd.conf configuration file and make necessary configurations, such as setting up virtual hosts and ports according to your needs.

Compile and Install PHP

Next, let's compile and install PHP. Go to the PHP source directory, run the configure script to generate the Makefile, and specify the PHP installation directory:

cd ../php-7.4.11
./configure --prefix=/path/to/install/phppath --with-apxs2=/path/to/install/apachepath/bin/apxs

After that, run the make command to compile and install PHP:

make
sudo make install

Configure PHP

After installing PHP, modify the Apache httpd.conf file to add the following lines at the end in order to load the PHP module:

LoadModule php7_module /path/to/install/phppath/libphp7.so
AddType application/x-httpd-php .php

Finally, restart the Apache server for the changes to take effect:

/path/to/install/apachepath/bin/apachectl restart

Conclusion

At this point, you have successfully compiled and installed Apache and PHP in a specific directory. You can now further customize the configuration according to your needs, ensuring the stability and performance of your server. By following these steps, you can set up a web server tailored to your requirements.