Skip to content

Instantly share code, notes, and snippets.

@rizkhanriaz
Last active October 23, 2023 19:16
Show Gist options
  • Save rizkhanriaz/9e7d581eda271ecf4fba7dd2636fec9f to your computer and use it in GitHub Desktop.
Save rizkhanriaz/9e7d581eda271ecf4fba7dd2636fec9f to your computer and use it in GitHub Desktop.
Compile Apache from source on Centos 7
###############################################################
Apache Installation
###############################################################
# Install required tools for compilation
sudo yum install autoconf expat-devel libtool libnghttp2-devel pcre-devel -y
# Download source code
cd #switch to home dir
curl -O -L https://github.com/apache/httpd/archive/2.4.37.tar.gz #latest stable release Apache
curl -O -L https://github.com/apache/apr/archive/1.6.5.tar.gz #Apache Runtime library. Required for Apache HTTPD
curl -O -L https://github.com/apache/apr-util/archive/1.6.1.tar.gz #Apache Runtime library. Required for Apache HTTPD
# Unpack downloaded sources
tar -zxvf 2.4.37.tar.gz
tar -zxvf 1.6.5.tar.gz
tar -zxvf 1.6.1.tar.gz
# Apache requires APR library for compilation. Copy the source codes to correct directory
# It's important to not to include version number in APR directories.
# If you just copy apr-1.6.5 without changing the name, it will give you a warning about missing apr directory.
cp -r apr-1.6.5 httpd-2.4.37/srclib/apr
cp -r apr-util-1.6.1 httpd-2.4.37/srclib/apr-util
# compile apache (avoid running package compiling as root or sudo )
cd httpd-2.4.33 # change dir to downloaded apache source
./buildconf
./configure --enable-ssl --enable-so --enable-http2 --with-mpm=event --with-included-apr --with-ssl=/usr/local/openssl --prefix=/usr/local/apache2
make
# After compiling install HTTPD with sudo
sudo make install
# Cleanup downloaded source
cd
rm -rf 1.6.1.tar.gz 1.6.5.tar.gz 2.4.37.tar.gz apr-1.6.5 apr-util-1.6.1 httpd-2.4.37
# Add Apache executables to PATH ( You can use nano instead of vi)
# create and open the following file
sudo vi /etc/profile.d/httpd.sh
# paste the following content, save and exit
pathmunge /usr/local/apache2/bin
# Add Systemd entry
# create and open the following file
sudo vi /etc/systemd/system/httpd.service
# paste the following content, save and exit
[Unit]
Description=The Apache HTTP Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl -k start
ExecReload=/usr/local/apache2/bin/apachectl -k graceful
ExecStop=/usr/local/apache2/bin/apachectl -k graceful-stop
PIDFile=/usr/local/apache2/logs/httpd.pid
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# reload the systemctl daemon
sudo systemctl daemon-reload
# start Apache httpd server (ignore the warnings at this stage)
sudo systemctl start httpd
###############################################################
Apache Configuration
###############################################################
# Create dedicated user and group for Apache
sudo groupadd www
sudo useradd httpd -g www --no-create-home --shell /sbin/nologin
# Configure apache httpd.conf
# Open httpd conf file
sudo vi /usr/local/apache2/conf/httpd.conf
# Make sure that ServerRoot is set to the same value as --prefix during ./configure
ServerRoot /usr/local/apache2
# Set ServerName to prevent warning on Apache start
ServerName localhost
# Default port set to 80 - HTTP protocol
Listen 80
# Set user and group
User httpd
Group www
# Configure entry file
DirectoryIndex index.php index.html
# Hide Apache version from header and from error files
# not in the config by default so will need to add it in the bottom of the file
ServerTokens Prod
ServerSignature off
# Disable ETag to prevent disposing sensitive values like iNode
# not in the config by default so will need to add it in the bottom of the file
FileETag none
## (save and exit httpd.conf) ##
# Restart httpd
sudo systemctl restart httpd
###############################################################
Configure Apache Modules
###############################################################
# disable (comment with #) all LoadModule in /usr/local/apache2/conf/httpd.conf
# now let's enable required modules so that apache will work
# Uncomment below given modules in /usr/local/apache2/conf/httpd.conf
# These modules must be enabled to start Apache
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule mime_module modules/mod_mime.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule dir_module modules/mod_dir.so
# To use PHP with PHP-FPM
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
# Enable pretty links and mod_rewrite that is highly used in all frameworks and CMSes
LoadModule rewrite_module modules/mod_rewrite.so
# Useful for WordPress sites - enables Require for setting up access to given resources.
LoadModule access_compat_module modules/mod_access_compat.so
# enables Alias.
LoadModule alias_module modules/mod_alias.so
# Enable gzip extension for compressing static files
LoadModule deflate_module modules/mod_deflate.so
LoadModule filter_module modules/mod_filter.so
# Enable expires header for caching assets on browser side
LoadModule expires_module modules/mod_expires.so
# Enable SSL
LoadModule http2_module modules/mod_http2.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
LoadModule ssl_module modules/mod_ssl.so
# Enable status module for monitoring Apache.
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule status_module modules/mod_status.so
# Other
LoadModule remoteip_module modules/mod_remoteip.so
LoadModule setenvif_module modules/mod_setenvif.so
# At the bottom of the file uncomment the line
Include conf/extra/httpd-mpm.conf
# Add this line (gzip conf)
Include conf/extra/httpd-deflate.conf
## (save and exit httpd conf) ##
###############################################################
Gzip Settings
###############################################################
# Create and open new file
sudo vi /usr/local/apache2/conf/extra/httpd-deflate.conf
# Paste the following content, save and exit the file
<IfModule mod_deflate.c>
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/ecmascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
</IfModule>
</IfModule>
###############################################################
Vhost Configurations
###############################################################
# create vhost conf dirs
sudo mkdir /usr/local/apache2/conf/sites-available
sudo mkdir /usr/local/apache2/conf/sites-enabled
# open httpd conf file
sudo vi /usr/local/apache2/conf/httpd.conf
# Add this line to the end of the file, save and exit
IncludeOptional /usr/local/apache2/conf/sites-enabled/*.conf
# Restart apache
sudo systemctl restart httpd
# Run Apache httpd on system start
sudo systemctl enable httpd
@douglasawh
Copy link

You might consider adding to the instructions that people need the EPEL repo enabled

@douglasawh
Copy link

pretty sure you also need openssl-devel.

In any case, it didn't get past ./configure --enable-ssl --enable-so --enable-http2 --with-mpm=event --with-included-apr --with-ssl=/usr/bin --prefix=/usr/local/apache2 until I installed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment