{"id":1494,"date":"2022-07-26T17:34:52","date_gmt":"2022-07-26T16:34:52","guid":{"rendered":"http:\/\/m0aws.co.uk\/?page_id=1494"},"modified":"2022-11-20T11:58:06","modified_gmt":"2022-11-20T11:58:06","slug":"build-a-pihole-using-apache2-web-server","status":"publish","type":"page","link":"https:\/\/m0aws.co.uk\/?page_id=1494","title":{"rendered":"Build a PiHole using Apache2 Web Server"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">For those that don&#8217;t know, a <a rel=\"noreferrer noopener\" href=\"https:\/\/pi-hole.net\" target=\"_blank\">PiHole<\/a> is a network wide advertisement blocking system that can also provide DNS and DHCP services to the local LAN.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ve been using <a rel=\"noreferrer noopener\" href=\"https:\/\/pi-hole.net\" target=\"_blank\">PiHole<\/a> servers here for many years to reduce the amount of advertising spam we have to endure whilst surfing the web. By adding block lists you can choose what level of advert blocking you want. It can also be used to block access to the many unsavoury websites that litter the internet today. Especially useful if you have children!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The PiHole server is controlled via an extensive web interface that provides all the functionality necessary to manage the service.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By default the PiHole installer uses <em><strong>lighttpd<\/strong><\/em> as a web server. This is a very light weight web server package that lacks the functionality offered by Apache.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ve been using Apache for many years now and know it well. I have a number of web services running on a number of Apache web servers some of which are public facing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To take advantage of my existing Apache web servers I&#8217;ve aways installed my PiHole services onto my Apache web servers rather than creating a complete new virtual machine and using the default lighttpd. Since Apache is more than capable of running multiple web sites and proxy services simultaneously it makes sense to fully utilise this facility.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"1595\" src=\"https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/M0AWS-PiHole-Management-interface.png\" alt=\"\" class=\"wp-image-1496\" srcset=\"https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/M0AWS-PiHole-Management-interface.png 2000w, https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/M0AWS-PiHole-Management-interface-300x239.png 300w, https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/M0AWS-PiHole-Management-interface-768x612.png 768w, https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/M0AWS-PiHole-Management-interface-1536x1225.png 1536w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><figcaption>View of the PiHole web based management interface<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">I often get asked online &#8220;How do I install PiHole on an Apache Web Server?&#8221;. Trying to answer this in a Facebook group or mailing list is extremely difficult and so, I&#8217;ve decided to write this article explaining how to do it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, what&#8217;s required to run Apache web server? <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All you really need is a PC\/RaspberryPi\/Server\/Virtual-Machine running your favourite Linux distribution. Any of the popular Linux distributions will suffice. I prefer to use Ubuntu or Debian for my servers and have almost 30 headless virtual machines running currently all of which use Ubuntu Server Edition or Debian Linux. For this article I&#8217;m using Ubuntu Server 18.04.6LTS. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Note<\/em><\/strong>: If you&#8217;re using Ubuntu Server 20.04.xLTS or 22.04.xLTS you need to reference my <a href=\"https:\/\/m0aws.co.uk\/?page_id=1654\" target=\"_blank\" rel=\"noreferrer noopener\">new article<\/a> as the build process has changed for the newer versions of Ubuntu Server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For this build I&#8217;m assuming you already have your Linux O\/S installed on your PC\/RaspberryPi\/Server\/VM and that it has a static IP Address assigned, is connected to your local LAN and has internet access. If you are using a version of Linux that has a GUI installed then you will need to open a terminal to enter the commands below. If like me you are using headless Linux installs then you will need to SSH into the server to be able to enter the commands required to build the PiHole.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first thing we need to do is install the Apache web server package. This is very easy and requires just one command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install apache2 -y<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once this completes, open your favourite web browser and point it at the IP Address of your PiHole server.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/192.168.1.212<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see the default Apache installation webpage being displayed.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1607\" height=\"1802\" src=\"https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/apache2-default-login-page.png\" alt=\"\" class=\"wp-image-1393\" srcset=\"https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/apache2-default-login-page.png 1607w, https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/apache2-default-login-page-268x300.png 268w, https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/apache2-default-login-page-768x861.png 768w, https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/apache2-default-login-page-1370x1536.png 1370w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><figcaption>Default Apache2 web page<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We now need to install PHP and the associated apache libraries.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install php php-common php-gd php-mysql php-sqlite3 php7.4 php7.4-cli php7.4-common php7.4-gd php7.4-json php7.4-mysql php7.4-opcache php7.4-readline php7.4-sqlite3\n\nsudo apt install libapache2-mod-php libapache2-mod-php7.4<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can check these all installed correctly using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">dpkg -l | grep php<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We should see the following. <strong>Note<\/strong> version numbers may differ slightly.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ii  libapache2-mod-php             2:7.4+76                       all          server-side, HTML-embedded scripting language (Apache 2 module) (default)\nii  libapache2-mod-php7.4          7.4.30-1+deb11u1               amd64        server-side, HTML-embedded scripting language (Apache 2 module)\nii  php                            2:7.4+76                       all          server-side, HTML-embedded scripting language (default)\nii  php-common                     2:76                           all          Common files for PHP packages\nii  php-gd                         2:7.4+76                       all          GD module for PHP [default]\nii  php-mysql                      2:7.4+76                       all          MySQL module for PHP [default]\nii  php-sqlite3                    2:7.4+76                       all          SQLite3 module for PHP [default]\nii  php7.4                         7.4.30-1+deb11u1               all          server-side, HTML-embedded scripting language (metapackage)\nii  php7.4-cli                     7.4.30-1+deb11u1               amd64        command-line interpreter for the PHP scripting language\nii  php7.4-common                  7.4.30-1+deb11u1               amd64        documentation, examples and common module for PHP\nii  php7.4-gd                      7.4.30-1+deb11u1               amd64        GD module for PHP\nii  php7.4-json                    7.4.30-1+deb11u1               amd64        JSON module for PHP\nii  php7.4-mysql                   7.4.30-1+deb11u1               amd64        MySQL module for PHP\nii  php7.4-opcache                 7.4.30-1+deb11u1               amd64        Zend OpCache module for PHP\nii  php7.4-readline                7.4.30-1+deb11u1               amd64        readline module for PHP\nii  php7.4-sqlite3                 7.4.30-1+deb11u1               amd64        SQLite3 module for PHP<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next we need to restart the Apache services to inherit the new settings and libraries.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl restart apache2<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">At this point we are ready to install the PiHole service. Using wget, download the installation script.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wget -O basic-install.sh https:\/\/install.pi-hole.net<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using your favourite editor, in my case &#8216;vi&#8217;, edit the installation script.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">vi basic-install.sh<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Scroll down to the line that starts with the text INSTALL_WEB_SERVER and change its value from true to false. (At the time of writing this its is on line 113 in the script)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INSTALL_WEB_SERVER=false<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once you&#8217;ve made the change, save the file and exit your editor. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We now need to copy the installation script into the default web server website directory. You can find this directory by concatenating the Apache default setup file and searching for the <strong><em>DocumentRoot<\/em><\/strong> directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cat \/etc\/apache2\/sites-available\/000-default.conf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will show the contents of the default Apache configuration as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;VirtualHost *:80&gt;\n\t# The ServerName directive sets the request scheme, hostname and port that\n\t# the server uses to identify itself. This is used when creating\n\t# redirection URLs. In the context of virtual hosts, the ServerName\n\t# specifies what hostname must appear in the request's Host: header to\n\t# match this virtual host. For the default virtual host (this file) this\n\t# value is not decisive as it is used as a last resort host regardless.\n\t# However, you must set it for any further virtual host explicitly.\n\t#ServerName www.example.com\n\n\tServerAdmin webmaster@localhost\n\tDocumentRoot <strong>\/var\/www\/html<\/strong>\n\n\t# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,\n\t# error, crit, alert, emerg.\n\t# It is also possible to configure the loglevel for particular\n\t# modules, e.g.\n\t#LogLevel info ssl:warn\n\n\tErrorLog ${APACHE_LOG_DIR}\/error.log\n\tCustomLog ${APACHE_LOG_DIR}\/access.log combined\n\n\t# For most configuration files from conf-available\/, which are\n\t# enabled or disabled at a global level, it is possible to\n\t# include a line for only one particular virtual host. For example the\n\t# following line enables the CGI configuration for this host only\n\t# after it has been globally disabled with \"a2disconf\".\n\t#Include conf-available\/serve-cgi-bin.conf\n&lt;\/VirtualHost&gt;\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Looking at the default Ubuntu\/Debian Apache configuration the web server is configured to serve web pages from the<strong><em> \/var\/www\/html<\/em><\/strong> directory as shown above by the <strong><em>DocumentRoot<\/em><\/strong> tag.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Issue the following command to copy the installation script into the <strong><em>\/var\/www\/html<\/em><\/strong>  directory and then move into the directory using the &#8216;cd&#8217; command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo cp .\/basic-install.sh \/var\/www\/html\/\n\ncd \/var\/www\/html\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We now need to make the script executable by setting the &#8216;X&#8217; bit for the owner of the file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo chmod 700 .\/basic-install.sh<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now run the script and follow the instructions as detailed below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo .\/basic-install.sh<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Select <strong><em>OK<\/em><\/strong> on the first pop-up to <strong><em>transform your device into a network-wide ad blocker!<\/em><\/strong> and on the donation pop-up.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Select <strong>Continue<\/strong> on the &#8220;Static IP Needed&#8221; as you have already done this. (You have, haven&#8217;t you?)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the next pop-up, select the upstream DNS server you want to forward external DNS lookups to. For this article I selected Cloudflare. This can be changed once the installation is complete.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Select <strong>Yes<\/strong> to use the suggested blocklist. Again this can be changed later once installed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Select <strong>Yes<\/strong> to install the Admin Web Interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Make sure you select <strong>No<\/strong> on the Web Server Pop-up as we do <strong>not<\/strong> want to install <strong><em>lighttpd<\/em><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the next screen it&#8217;s entirely up to you if you want to enable logging or not. The same applies on the privacy screen that follows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At this point the installation will start. It shouldn&#8217;t take long to install and should complete with the installation complete pop-up.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1250\" height=\"665\" src=\"https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/Pihole-install-complete-screen.png\" alt=\"\" class=\"wp-image-1519\" srcset=\"https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/Pihole-install-complete-screen.png 1250w, https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/Pihole-install-complete-screen-300x160.png 300w, https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/Pihole-install-complete-screen-768x409.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><figcaption>PiHole installation complete screen<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Make a note of the information on the screen as you will need this later.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At this point the PiHole web interface should be installed in the <strong><em>\/var\/www\/html\/<\/em><\/strong> directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ls -la<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should now see the directory structure as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">total 152\ndrwxr-xr-x 4 root root   4096 Jul 26 16:46 .\ndrwxr-xr-x 3 root root   4096 Jul 26 15:45 ..\ndrwxr-xr-x 7 root root   4096 Jul 26 16:46 admin\n-rwx------ 1 root root 125428 Jul 26 16:32 basic-install.sh\n-rw-r--r-- 1 root root  10701 Jul 26 15:45 index.html\ndrwxr-xr-x 2 root root   4096 Jul 26 16:46 pihole<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We need to change the ownership of the files and directories to that of the web server so that it can serve the web application correctly.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo chown -R www-data:www-data .\/*<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next we need to add the user <strong><em>www-data<\/em><\/strong> to the PiHole group in the <strong><em>\/etc\/group<\/em><\/strong> file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using your favourite editor edit the<strong><em> \/etc\/group<\/em><\/strong> file. Note you will need to use sudo to obtain root privileges to gain access to the file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo vi \/etc\/group<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Scroll down until you see the PiHole entry in the file and then add <strong><em>www-data<\/em><\/strong> to the end of the line as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pihole:x:998:www-data<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once we&#8217;ve made the change, save and exit the file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The installation is now complete and all that is left to do is login to the web interface using the password displayed during installation and configure the service as normal.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/192.168.1.212\/admin<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"2026\" height=\"1952\" src=\"https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/Pihole-web-interface.png\" alt=\"\" class=\"wp-image-1530\" srcset=\"https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/Pihole-web-interface.png 2026w, https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/Pihole-web-interface-300x289.png 300w, https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/Pihole-web-interface-768x740.png 768w, https:\/\/m0aws.co.uk\/wp-content\/uploads\/2022\/07\/Pihole-web-interface-1536x1480.png 1536w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><figcaption>New PiHole web interface<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When new versions of PiHole are released the upgrades are performed exactly the same way as when you use lighttpd, there are no special steps required when using Apache web server.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo pihole -up<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">More soon &#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For those that don&#8217;t know, a PiHole is a network wide advertisement blocking system that can also provide DNS and DHCP services to the local LAN. I&#8217;ve been using PiHole servers here for many years to reduce the amount of advertising spam we have to endure whilst surfing the web. By adding block lists you &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/m0aws.co.uk\/?page_id=1494\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Build a PiHole using Apache2 Web Server&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1494","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/m0aws.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/1494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/m0aws.co.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/m0aws.co.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/m0aws.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/m0aws.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1494"}],"version-history":[{"count":52,"href":"https:\/\/m0aws.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/1494\/revisions"}],"predecessor-version":[{"id":1692,"href":"https:\/\/m0aws.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/1494\/revisions\/1692"}],"wp:attachment":[{"href":"https:\/\/m0aws.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}