Raspberry Pi Apache2

Posted on Sun 07 April 2019 in Home Automation,

I use a Raspberry Pi model A to serve up a few development websites.

I do my edits locally before uploading to the interwebs.

It also hosts some static pages that are used around the house. Having a web server at home is very handy for small, non-public projects.

Install Apache2

sudo apt-get install apache2 -y

And PHP (it's handy)

sudo apt-get install php libapache2-mod-php

You need to make up a port number for your website.

If public, you will be port forwarding this from your router.

If internal, you will direct your request to the local IP and the port.

A number anywhere from 0 to 65535 will do, however there are some already assigned that you shouldn't use.

If you are unfamiliar with ports have a quick look at some well known ones, don't use them. I usually pick a random 4 digit number.

Well Known Ports: https://www.webopedia.com/quick_ref/portnumbers.asp.

For the example below I will use 1234.

Drop in to the below file and add your port number(s). Drop it under the Listen 80.

You can add as many as you need here, but we will need to reference them elsewhere too.

sudo nano /etc/apache2/ports.conf

Best give the Pi a reboot at this point.

sudo reboot

You now need to make a directory structure for your websites in /var/www/.

I make a container folder for the site /var/www/SITENAME/.

And a public facing folder /var/www/sitename/public_html. Anything in this folder will be "live".

sudo mkdir -p /var/www/SITENAME/public_html

Take ownership of the folder and change the permissions, recursively.

sudo chown -R $USER:$USER /var/www/SITENAME/public_html

sudo chmod -R 755 /var/www/

You now need to make a config file so Apache knows where the site is.

There's a template to copy, edit and save in the sites-available directory.

Once in there you, enable, the site, it copies it's self from the sites-available to the sites-enabled directory.

Always a good idea to backup files like this up before editing. Avoid the pain in the ass!

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default.conf_bkup

If you need to restore the backup for any reason:

sudo mv /etc/apache2/sites-available/000-default.conf_bkup /etc/apache2/sites-available/000-default.conf

Edit the template config file. You can remove anything with a # in front of it. Mine is below.

sudo nano /etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Now copy, rename and move this template to the sites-available directory.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/SITENAME.conf

Open it up for editing.

sudo nano /etc/apache2/sites-available/SITENAME.conf

Edit the (copied) template to point to your site and add the chosen port number.

<VirtualHost *:1234>

        ServerAdmin webmaster@localhost
        ServerName SITENAME
        ServerAlias SITENAME
        DocumentRoot /var/www/SITENAME/public_html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Enable the site.

This will copy any site.config files from the available to the enabled and make the live.

sudo a2ensite SITENAME.conf

Drop an index.html in your public_html folder.

Restart Apache2

sudo service apache2 restart

I also usually reboot the Pi at this point too

sudo reboot

Apache2 will usually run at start up, so once the Pi has network, go to your servers IP address, with the port number and see the new web site!

As I mentioned, I serve up several sites from one Pi. Each site needs:

 Its own directory
 A unique port
 A port entry in /etc/apache2/ports.conf
 A config file in /etc/apache2/sites-available/

After you add a site to available you need to enable it and restart the Apache2 service.

sudo a2ensite SITENAME.conf && sudo service apache2 restart

If you need to remove Apache2:

sudo apt-get remove --purge apache2 
sudo apt-get remove --purge apache2-utils
sudo apt-get remove php5 libapache2-mod-php5
sudo apt-get purge apache2
sudo apt-get autoremove
sudo rm -Rf "DIRs Listed in Whereis"
sudo reboot