Install Apache Web Server
2025-02-11, Tue
This post records process of installing Apache HTTP Server.
1. on Debian GNU/Linux
The whole process of installing Apache server along with PHP is quite smooth with following command:
apt-get install apache2 apache2-doc php
Debian puts apach2 configs in customized directory /etc/apache2/
,
with nicely categoried *-available
and *-enabled
sub-directories for,
well, all configs and enabled configs.
To manage the httpd service, systemctl
is preferred to apache2ctl
.
Environment variables like APACHE-RUN-USER
and APACHE-RUN-GROUP
are
defined in /etc/apache2/envvars
, while error log could be found in
/var/log/aapche2/error.log
.
1.1. Integrate with PHP
Things just work out of the box, which means there is no extra config
needed to be done before PHP page shows up correctly in browser. That
been said, be aware that Apache do need execution
permisson1 on the directory path when one is
provided as symbolic link in DocumentRoot. i.e. if /var/www/html
is the DocumentRoot, and a symbolic link is created by
cd /var/ww/html/ ln -s /path/to/other/place/ other-alias
Ensure that /path
, /path/to
, /path/to/other
and /path/to/other/place
all have proper execution available assigned for the Apache run user.
When things didn't work as expected, instead of scratching head and guessing
randomly, go check the error log first.
2. on macOS
The offical Apache HTTP Server site2 doesn't provide binary build for macOS, so it could be either installed through third party package manager (e.g. Homebrew, MacPorts) or compiled from source code. Since there's no recommended way on the project site, I chose to compile from source code.
The first step is to install Apache Portable Runtime (APR)3, along with APR-util. After it's compiled & installed, go to httpd source directory and run
./configure --with-apr=/usr/local/apr
before continuing on the usual make & make install
process. /usr/local/apr
is the default installation path of APR, adjust it accordingly if it's changed.
Funny experience: I tried to run ./configure --with-apr /usr/local/apr
first
but failed, only to realize that it is shell script being executed here – instead
of the usual command.
After installation complete, use apachectl
to manage the httpd serivce.
Configurations are located in /etc/apache2/
, and error logs could be found in
/var/log/apache2/error_log
.
2.1. Integrate with PHP
Somehow the PHP official site4 has put "Installation on macOS using third-party packages"5 as the first entry – which I assume is the recommended way. So I followed the instruction and used Homebrew to complete installation –only to find that the Apache HTTP Server won't recognize the PHP module installed. i.e.
apachectl configtest
This command complains the PHP module is not properly signed. A quick
online search reveals that libphp.so
should be signed by
codesign
6. (Perhaps I should compile PHP
from source anyway.)
After all is settled, ensure proper directory permissions are configured when symbolic links are added into DocumentRoot.
Footnotes:
Execution Permission on directory works
different from files. To quote Linux file permissions explained from
Red Hat Blog: "…(it) allows you to change your working directory
(using cd
) or pass through this directory on your way to a
subdirectory underneath".
Apache HTTP Server project https://httpd.apache.org
Apache Portal Runtime project https://apr.apache.org
PHP official site https://www.php.net
PHP Installation on macOS https://www.php.net/manual/en/install.macosx.php
How to sign Homebrew PHP module in macOS https://www.simplified.guide/macos/apache-php-homebrew-codesign