How to install Apache 2.4 from source in CentOS 7

By | June 10, 2015

In this post we will see How to install Apache 2.4 from source in CentOS 7. This will work on CentOS 6 as well. Now a question naturally comes to mind… why to install it from source when prebuilt RPM packages are readily available in the repository..??? There may be many reasons:

1) Apache started serving the web world from 1995 and as on date it powers more the 50 percent of all the webservers in the world. Naturally it is huge, it has lot of modules, has inbuilt language interpretes for various programming langauges like php pyhton etc. All of these features makes it a hot favourite of the webmasters across the globe and webhosting companies, but at the same time these same features makes apache very resource hungry and slow on performance (especially with the prefork mpm).

2) The default installation of apache from the repository in CentOS installs apache with a plethora of modules enabled by default, which naturally makes it resource hungry, and you may just need 30 percent of the modules in your production environment.

3) Now because a repository based installation has got whole bunch of modules which are enabled by default, it becomes a tiring task to do a performance optimization on apache by disabling one unused module at a time, restarting apache and testing your site and hoping that nothing breaks.

4) Personally I like the way aphche is installed from the repositories in debian/ubuntu, minimal modules are enabled by default, and you can enable additional modules as and when need. And I must confess, if I hate CentOS, it is just because of the way apache is installted from the repositories, you literally have a big fat elephant at hand to handle.

5) Lets assume, you have a small VPS with just 512MB ram??? You have chosen CentOS as your distro of choice, and installed apache from the repository, if it is not optimized, and you started attracting some traffic to your site….. you are dead long before you realize. In this kind of small environment, compiling and installing apache from source makes a great sense and enable only those modules which are needed.

Phew!!!! Quite a bit of reasoning there. Lets get ahead with the installation.

1) Install some prerequisite packages:

yum install wget gcc pcre-devel openssl-devel

2) Download apache, apr and apr-util source tarball and install apache:

As of this writing the stable version of apache 2.4  is apache 2.4.12, apr-1.5.2 and apr-util-1.5.4

cd ~
mkdir sources
cd sources
wget http://ftp.piotrkosoft.net/pub/mirrors/ftp.apache.org//httpd/httpd-2.4.12.tar.bz2
wget http://ftp.ps.pl/pub/apache//apr/apr-1.5.2.tar.bz2
wget http://ftp.ps.pl/pub/apache//apr/apr-util-1.5.4.tar.bz2
tar -xvf httpd-2.4.12.tar.bz2
tar -xvf apr-1.5.2.tar.bz2
tar -xvf apr-util-1.5.4.tar.bz2
cp -r apr-1.5.2 httpd-2.4.12/srclib/apr
cp -r apr-util-1.5.4 httpd-2.4.12/srclib/apr-util
cd httpd-2.4.12
./configure --prefix=/etc/apache2 --enable-ssl --enable-so --with-included-apr --with-mpm=event
make
make install

Things to be noted here is apache will be installed in “/etc/apache2” directory due to “–prefix=/etc/apache2” added to “./configure”, likewise it will enable “ssl” support, “so” (dynamic modules support), included “apr”  and “event mpm”.

3) Some Basic Apache related Configuration before starting Apache:

We need to ensure that the ownership if the directory “/etc/apache2” is with the user apache:

chown -R apache.root /etc/apache2

Some changes that needs to be made in “httpd.conf” file

cd /etc/apache2/conf
cp httpd.conf httpd.conf.bak
vi httpd.conf

Inside the file, Set the user and group values to apache

User apache
Group apache

Set ServerName to whatever You want, it will prevent warning when apache will start

ServerName example.com

Uncomment the line Server-pool management (MPM specific file)

Include conf/extra/httpd-mpm.conf

And add these lines beneath the above lines:

Include conf/vhosts/*.conf

Also uncomment the server default settings line

Include conf/extra/httpd-default.conf

Now lets look at the the modules part inside this httpd.conf file, you will find a lot of lines like these:

LoadModule authn_file_module modules/mod_authn_file.so
#LoadModule authn_dbm_module modules/mod_authn_dbm.so

Some of these lines begin with a comment i.e a # sign and some without it.  Lines without the beginning # sign define the modules which are loaded. In order to make things a bit easier, I have listed the modules, that are mostly needed to run a basic apache server.

LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule filter_module modules/mod_filter.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule mime_module modules/mod_mime.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule dir_module modules/mod_dir.so
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so

Please note you may not need all of these modules in your environment, you can just use these as a reference to comment out the unused modules and uncomment the needed ones. Once you are done, save the file and exit.

Next, we will create apache virtual hosts, as defined above in httpd.conf file

cd /etc/apache2/conf
mkdir vhosts
cd vhosts
touch example.com.conf
vi example.com.conf
<VirtualHost *:80>
ServerName example.com
 DocumentRoot /etc/apache2/htdocs
 <Directory /etc/apache2/htdocs>
 AllowOverride All
 Require all granted
 </Directory>
ErrorLog "/var/log/apache/error.log"
CustomLog "/var/log/apache/access.log" common
</VirtualHost>

Save the file.

In httpd.conf file we enabled mpm settings. Now it’s time to edit this file.

cd /etc/apache2/conf/extra 
vi httpd-mpm.conf

By default apache’s pid file httpd.pid is located in logs file, let’s change it to /var/run directory.

PidFile "/var/run/httpd.pid"

A quick view of the event mpm vales

<IfModule mpm_event_module>
 StartServers 2
 MinSpareThreads 2
 MaxSpareThreads 4
 ThreadsPerChild 30
 MaxRequestWorkers 300
 MaxConnectionsPerChild 1000
</IfModule>

The values, should be sufficient for a small server with less load and less resources, but for heavy loaded servers you may need to adjust. have a look at this link to understand mpm settings.

Next, we do not want much of our server related information to got out to the external world. So open the file “/etc/apache2/conf/extra/httpd-default.conf

cd /etc/apache2/conf/extra
vi httpd-default.conf

and make the following changes in the file

ServerTokens Prod
ServerSignature Off

Save the file

4) Create the log directory and Start Apache server:

mkdir /var/log/apache && /etc/apache2/bin/apachectl start

To check the active apache modules

/etc/apache2/bin/apachectl -M

To check syntaz errors in configuration filies

/etc/apache2/bin/apachectl -t

To restart apache server

/etc/apache2/bin/apachectl -k graceful

Make sure apache starts on server reboot

echo '/etc/apache2/bin/apachectl start' >> /etc/rc.local

Open port number 80 and 443 if needed from the firewall in CentOS 7

firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp
firewall-cmd --reload

Open port number 80 and 443 if needed from the firewall in CentOS 6

iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
service iptables save

That’s it our apche webserver is up and running now.

 

2 thoughts on “How to install Apache 2.4 from source in CentOS 7

  1. Nick

    Do you seriously not see the problem with your “guide”?

    wget http://ftp.piotrkosoft.net/pub/mirrors/ftp.apache.org//httpd/httpd-2.4.12.tar.bz2
    wget http://ftp.ps.pl/pub/apache//apr/apr-1.5.2.tar.bz2
    wget http://ftp.ps.pl/pub/apache//apr/apr-util-1.5.4.tar.bz2
    tar -zxvf httpd-2.4.12.tar.gz
    tar -zxvf apr-1.5.2.tar.gz
    tar -zxvf apr-util-1.5.4.tar.gz

    You tell people to download bz2 files and attempt to extract gz files….Last I looked, bzip2 and gzip are not the same thing.

    Reply
    1. rudraraj Post author

      Good Evening Mr. Nick,
      Thank you so much for pointing out the typographical error. I have corrected it.
      Your feedback is highly appreciated.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *