{"id":76,"date":"2023-04-15T12:27:07","date_gmt":"2023-04-15T12:27:07","guid":{"rendered":"https:\/\/koner.mywire.org\/?page_id=76"},"modified":"2023-04-15T12:32:23","modified_gmt":"2023-04-15T12:32:23","slug":"how-to-setup-a-raspberry-pi-apache-web-server","status":"publish","type":"page","link":"https:\/\/koner.mywire.org\/index.php\/how-to-setup-a-raspberry-pi-apache-web-server\/","title":{"rendered":"How to Setup a Raspberry Pi Apache Web Server"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">In this Raspberry Pi Apache project, we will be showing you how to install and setup an Apache web server on the Raspberry Pi.<\/h4>\n\n\n\n<p>By itself, Apache can serve HTML files over the HTTP and HTTPS web protocols. Alongside additional modules such as PHP, Apache is also able to serve dynamic content. <\/p>\n\n\n\n<p>Apache is one of the most popular web servers available for the Raspberry Pi. Apache alone accounts for\u00a044% of all web servers\u00a0in the world.<\/p>\n\n\n\n<p>Within this Raspberry Pi Apache tutorial, we will be walking you through the steps to setting up Apache, installing PHP, and also creating your first basic Apache VirtualHost file.<\/p>\n\n\n\n<p>In this tutorial, we touch on all the basics of Apache, however, do not touch on setting up\u00a0MYSQL\u00a0and\u00a0PHPMyAdmin. We also don\u2019t cover setting up\u00a0WordPress on the Raspberry Pi. These are all topics covered in separate tutorials.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Equipment List<\/h3>\n\n\n\n<p>Below are all the bits and pieces that I used for this Raspberry Pi Apache Web Server tutorial.<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"recommended_equipment\">\n<li>Raspberry Pi<\/li>\n\n\n\n<li>Micro SD Card<\/li>\n\n\n\n<li>Power Supply<\/li>\n\n\n\n<li>Ethernet Cable\u00a0or\u00a0Wi-Fi<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">How to Install the Apache Web Server on the Raspberry Pi<\/h3>\n\n\n\n<p><strong>1.<\/strong>&nbsp;Before we install Apache to our Raspberry Pi, we must first ensure the package list is up to date by running the following two commands.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get update\nsudo apt-get upgrade<\/code><\/pre>\n\n\n\n<p><strong>2.<\/strong>&nbsp;First, we will need to install the Apache2 package on our Raspberry Pi.<\/p>\n\n\n\n<p>For those who don\u2019t know&nbsp;<a href=\"https:\/\/www.apache.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">what Apache is<\/a>, it is a server software that serves the HTML files from a computer to the web browser.<\/p>\n\n\n\n<p>To install apache2 on your Raspberry Pi, enter the following command into the terminal.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install apache2 -y<\/code><\/pre>\n\n\n\n<p><strong>3.<\/strong>&nbsp;With Apache2 installed to our Raspberry Pi, we now have an extremely basic web server up and running. The server will be able to provide non-dynamic content such as HTML files.<\/p>\n\n\n\n<p>In the next section, we will be extending this basic Apache web server by installing PHP to the Raspberry Pi.<\/p>\n\n\n\n<p>To check that Apache is up and running on your Raspberry Pi, you can enter the Raspberry Pi\u2019s IP address into a\u00a0web browser. The server should return a webpage with some simple text on it.<\/p>\n\n\n\n<p>If you do not know the IP, you can enter the\u00a0hostname command\u00a0into the terminal to retrieve it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hostname -I<\/code><\/pre>\n\n\n\n<p><strong>4.<\/strong>\u00a0In a web browser, enter your\u00a0Raspberry Pi\u2019s IP Address, it should connect and load a page like the one below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"676\" height=\"1024\" src=\"https:\/\/koner.mywire.org\/wp-content\/uploads\/2023\/04\/apache2def_page-676x1024.png\" alt=\"\" class=\"wp-image-84\" srcset=\"https:\/\/koner.mywire.org\/wp-content\/uploads\/2023\/04\/apache2def_page-676x1024.png 676w, https:\/\/koner.mywire.org\/wp-content\/uploads\/2023\/04\/apache2def_page-198x300.png 198w, https:\/\/koner.mywire.org\/wp-content\/uploads\/2023\/04\/apache2def_page-768x1163.png 768w, https:\/\/koner.mywire.org\/wp-content\/uploads\/2023\/04\/apache2def_page.png 844w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/figure>\n\n\n\n<p><strong>5.<\/strong>\u00a0To be able to make changes to the files within the\u00a0<code>\/var\/www\/html<\/code>\u00a0without using root we need to setup\u00a0some permissions.<\/p>\n\n\n\n<p>Firstly, we add the user&nbsp;<code>pi<\/code>&nbsp;(our user) to the&nbsp;<code>www-data<\/code>&nbsp;group, the default group for Apache2.<\/p>\n\n\n\n<p>Secondly, we give ownership to all the files and folders in the&nbsp;<code>\/var\/www\/html<\/code>&nbsp;directory to the&nbsp;<code>www-data<\/code>&nbsp;group.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo usermod -a -G www-data pi\nsudo chown -R -f www-data:www-data \/var\/www\/html<\/code><\/pre>\n\n\n\n<p>Once you have run that command, you will need to logout and then log back in for the changes to take effect.<\/p>\n\n\n\n<p><strong>6.<\/strong>&nbsp;You can now make changes to the default web page by running the following command.<\/p>\n\n\n\n<p>This command will use the\u00a0nano text editor\u00a0to modify the\u00a0<code>index.html<\/code>\u00a0file.<\/p>\n\n\n\n<p>The web server will serve all files within the&nbsp;<code>\/var\/www\/html\/<\/code>&nbsp;directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo nano \/var\/www\/html\/index.html<\/code><\/pre>\n\n\n\n<p>Apache is a basic web server and is great if you want to learn HTML, JS, or CSS.<\/p>\n\n\n\n<p>However, if you are after PHP (Used for dynamic web pages) then continue to the next section of our Raspberry Pi Apache tutorial.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setting up PHP7 for Apache<\/h3>\n\n\n\n<p><strong>1.<\/strong>\u00a0Please note that before you start this section, you should be\u00a0<strong>running at least Raspberry Pi OS Bullseye<\/strong>. <\/p>\n\n\n\n<p>To start this section, we will need to go ahead and install PHP 7.4 and several other packages to our Raspberry Pi. The additional packages we are installing are ones that are commonly used by PHP applications.<\/p>\n\n\n\n<p>Lucky for us installing all the packages we need is a simple process as PHP 7.4 is available in the Raspberry Pi OS Bullseye package repository.<\/p>\n\n\n\n<p>Run the following command to install all the PHP packages to your Raspberry Pi.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo apt install php7.4 libapache2-mod-php7.4 php7.4-mbstring php7.4-mysql php7.4-curl php7.4-gd php7.4-zip -y<\/code><\/pre>\n\n\n\n<p>While these packages are available on Raspberry Pi OS Bullseye, installing these on older versions is possible. To install this PHP version on older releases you will&nbsp;<a href=\"https:\/\/pimylifeup.com\/raspberry-pi-latest-php\/\">need to add a third-party PHP repository<\/a>.<\/p>\n\n\n\n<p><strong>2.<\/strong>&nbsp;Now that PHP is installed to our Raspberry Pi, we can test it to make sure it\u2019s working.<\/p>\n\n\n\n<p>We don\u2019t have to worry about modifying any configuration files as this is automatically done when Apache is detected.<\/p>\n\n\n\n<p>We can test to see PHP is working by creating a PHP file within the&nbsp;<code>\/var\/www\/html\/<\/code>&nbsp;directory. Creating a file in this directory will allow it to be processed and displayed when you open it in a web browser.<\/p>\n\n\n\n<p>For our example, we will be creating a PHP file called&nbsp;<code>example.php<\/code>. We can create this file by issuing the following command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/var\/www\/html\/example.php<\/code><\/pre>\n\n\n\n<p><strong>3.<\/strong>&nbsp;In this file, we need to add the following lines on PHP code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&lt;?php\necho \"Today's date is \".date('Y-m-d H:i:s');<\/code><\/pre>\n\n\n\n<p>The code above is just an incredibly simple PHP script that prints out today\u2019s date retrieved using PHP\u2019s&nbsp;<code>date()<\/code>&nbsp;function. It will be enough to tell us that PHP is, in fact, up and running.<\/p>\n\n\n\n<p><strong>4.<\/strong>\u00a0Now save the file by pressing\u00a0<kbd>Ctrl + X<\/kbd>\u00a0then pressing\u00a0<kbd>Y<\/kbd>\u00a0and hitting\u00a0<kbd>ENTER<\/kbd>.<\/p>\n\n\n\n<p><strong>5.<\/strong>&nbsp;In your web browser, go to&nbsp;<code>http:\/\/192.168.1.103\/example.php<\/code>.<\/p>\n\n\n\n<p>Make sure you replace&nbsp;<code>192.168.1.103<\/code>&nbsp;with your Raspberry Pi\u2019s IP Address.<\/p>\n\n\n\n<p>Going to the following URL should display something like the following.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Today's date is 2019-06-28 21:30:45<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Setting up an Apache Virtual Host<\/strong><\/h3>\n\n\n\n<p>Virtual hosts are an essential part of the way Apache works.&nbsp;<a href=\"https:\/\/httpd.apache.org\/docs\/2.4\/vhosts\/examples.html\" target=\"_blank\" rel=\"noreferrer noopener\">Apache uses these Virtualhost files<\/a>&nbsp;so that it knows how to handle an individual site.<\/p>\n\n\n\n<p>Within this section, we will show you how to create a basic virtual host file on your Raspberry Pi Apache web server. Virtual hosts are Apache\u2019s way of handling multiple websites with each Virtual Host file setting up and configuring a particular domain.<\/p>\n\n\n\n<p><strong>1.<\/strong>&nbsp;Begin by running the following command to create a basic virtual host file called&nbsp;<code>example.com.conf<\/code>&nbsp;within the&nbsp;<code>\/etc\/apache2\/sites-available<\/code>&nbsp;folder.<\/p>\n\n\n\n<p>If you plan on using this for an actual domain name, make sure you swap out&nbsp;<code>example.com<\/code>&nbsp;with the domain name.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/apache2\/sites-available\/example.com.conf<\/code><\/pre>\n\n\n\n<p><strong>2.<\/strong>&nbsp;Within this file, enter the following text.<\/p>\n\n\n\n<p>We will explain each part of the virtual host file as we go along so you can have an idea on how to set up a very basic virtual host.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;VirtualHost *:80&gt;<\/code><\/pre>\n\n\n\n<p>This line designates the start of the virtual host and that this virtual host should listen on port 80.<\/p>\n\n\n\n<p>For those who do not know&nbsp;<strong>port 80<\/strong>&nbsp;is the default port for&nbsp;<strong>http<\/strong>. Likewise, the port utilized for&nbsp;<strong>https<\/strong>&nbsp;is&nbsp;<strong>443<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ServerName example.com\nServerAlias www.example.com<\/code><\/pre>\n\n\n\n<p>Here we add two directives to our virtual host. The first of these directives called&nbsp;<code>ServerName<\/code>&nbsp;designates the base domain. This server name is used to match the VirtualHost to a domain name.<\/p>\n\n\n\n<p>The second directive,&nbsp;<code>ServerAlias<\/code>, defines additional domain names that will be matched as if they were the base domain name.<\/p>\n\n\n\n<p>This directive is useful for matching additional names such as a&nbsp;<code>www.<\/code>&nbsp;subdomain.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DocumentRoot \/var\/www\/example.com\/public_html<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>DocumentRoot<\/code>&nbsp;directive defines the directory where all the files will be served from by Apache.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ErrorLog ${APACHE_LOG_DIR}\/example.com_error.log\nCustomLog ${APACHE_LOG_DIR}\/example.com_access.log combined<\/code><\/pre>\n\n\n\n<p>In these final two directives&nbsp;<code>ErrorLog<\/code>&nbsp;and&nbsp;<code>CustomLog<\/code>&nbsp;we specify the locations where we want that log files to be kept.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;\/VirtualHost&gt;<\/code><\/pre>\n\n\n\n<p>Finally, we close off the VirtualHost section.<\/p>\n\n\n\n<p><strong>3.<\/strong>&nbsp;With everything complete, the code should end up looking like what we have below. Of course, using your domain name and not&nbsp;<code>example.com<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;VirtualHost *:80&gt;\n      ServerName example.com\n      ServerAlias www.example.com\n      DocumentRoot \/var\/www\/example.com\/public_html\n      ErrorLog ${APACHE_LOG_DIR}\/example.com_error.log\n      CustomLog ${APACHE_LOG_DIR}\/example.com_access.log combined\n&lt;\/VirtualHost&gt;<\/code><\/pre>\n\n\n\n<p>Once done, save the file by pressing&nbsp;<kbd>CTRL + X<\/kbd>&nbsp;followed by&nbsp;<kbd>Y<\/kbd>&nbsp;then&nbsp;<kbd>ENTER<\/kbd>.<\/p>\n\n\n\n<p><strong>4.<\/strong>&nbsp;Let\u2019s now create the folder where we will be storing our HTML files. We will take ownership of this folder for the&nbsp;<code>www-data<\/code>&nbsp;group as well.<\/p>\n\n\n\n<p>Run the following command to create the folder we need and take ownership of it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mkdir -p \/var\/www\/example.com\/public_html\nsudo chown -R www-data:www-data \/var\/www\/example.com\/public_html<\/code><\/pre>\n\n\n\n<p><strong>5.<\/strong>&nbsp;Now that we have created our VirtualHost and the folder for it, let\u2019s go ahead and now activate it by running the following command.<\/p>\n\n\n\n<p>This command creates a symlink for our config file between the&nbsp;<code>\/etc\/apache2\/sites-available\/<\/code>&nbsp;and&nbsp;<code>\/etc\/apache2\/sites-enabled\/<\/code>&nbsp;directories.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2ensite example.com.conf<\/code><\/pre>\n\n\n\n<p><strong>6.<\/strong>&nbsp;Finally, for our new virtual host file to be loaded in, we need to tell the Apache2 service to reload its configuration.<\/p>\n\n\n\n<p>This can be done simply by running the command below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo systemctl reload apache2<\/code><\/pre>\n\n\n\n<p>You can now point a domain name server (DNS) to the Raspberry Pi\u2019s public IP and have it serve files for the requested domain name. You will need to&nbsp;<a href=\"https:\/\/pimylifeup.com\/raspberry-pi-port-forwarding\/\">setup port forwarding<\/a>&nbsp;to have this work correctly.<\/p>\n\n\n\n<p>By now you should have the Apache web server running on your Raspberry Pi. You should also have a decent idea on how to setup PHP and virtual hosts on Apache.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Raspberry Pi Apache project, we will be showing you how to install and setup an Apache web server on the Raspberry Pi. By itself, Apache can serve HTML files over the HTTP and HTTPS web protocols. Alongside additional modules such as PHP, Apache is also able to serve dynamic content. Apache is one [&hellip;]<\/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-76","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/pages\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":23,"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/pages\/76\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/pages\/76\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/media?parent=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}