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.
Before installing Apache and PHP, make sure your system has the necessary compilation tools and dependencies. Typically, you will need the following packages:
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 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
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
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
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.
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
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
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.