29th November 2018
In this tutorial I'll show you how to install an Apache2 web server on your Raspberry Pi and configure secure access.
This allows you to run websites, and build web applications on your Pi to use from a browser on your computer and phone.
If you haven't recently checked for updates you should do that first. Type
sudo apt-get update && sudo apt-get upgrade -y
Now to install Apache, type:
sudo apt-get install apache2 apache2-utils
Follow the prompts to complete the installation.
Next we'll install PHP, along with some libraries.
sudo apt-get install libapache2-mod-php php php-pear php-xcache php-curl php-gd
You probably don't need all of the libraries, but they should cover you for most things you may wish to do.
We need to configure the webroot folder and add password-protection to our server.
To do this we'll modify the 000-default.conf file. We'll backup the file first in case we wreck things.
In a terminal, type:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default_old.conf
Now open the file in Nano:
sudo nano /etc/apache2/sites-available/000-default.conf
Scroll down to the line begining with 'DocumentRoot'
# The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html
Replace /var/www/html with the full path to your web folder. For example, mine looks like this:
The server will have access to any files and folders within this folder.
That is the folder done, now to password-protect the server.
You can password protect the entire folder, or create a dedicated secure area inside a new folder in the DocumentRoot folder. Creating a dedicated secure area allows you to have public and private sections. It also allows you to have an initial landing page to log in from.
Past the following code directly under the DocumentRoot line.
Require valid-userAuthType Basic AuthName "Secure Area" AuthUserFile /etc/apache2/.htpasswd
If you are protecting the entire folder, on the Directory line, change /path/to/webfolder to the same path as the DocumentRoot.
If instead you are creating a separate secure area, change /path/to/webfolder to the path for your secure folder within the DocumentRoot folder.
I'm using a dedicated secure folder called 'secure' inside my DocumentRoot folder so my line looks like:
Press CONTROL + X to close Nano. At the prompt, type y to save the file then press Enter.
The changes won't take effect until we restart Apache2, but we need to do a few more things before that.
First we need to change the ownership and permissions for you web folder to allow Apache2 access.
Substituting you DocumentRoot path for /path/to/webfolder and your Linux username for YourName, type:
sudo chown -R YourName:www-data /path/to/webfolder
For example, I type:
sudo chown -R anth:www-data /home/anth/web
Next we'll set permissions for the web folder. Once again substituting your document root for /path/to/webfolder, type:
sudo chmod 750 /path/to/webfolder
We can now create your web user name and password to access the secure area.
This is separate from your Linux user account. You can use the same name as your Linux name, or something different.
Substituting your desired name for YourName, type:
htpasswd -c /etc/apache2/.htpasswd YourName
You'll be prompted to create a password for the user.
When you visit your server from a web browser you will log in with this username and password.
Obviously to use a web server we need a web page. We'll make a basic landing page with HTML.
In the terminal, navigate into your document root folder. To create and open the file, type:
Copy and past the following code into the file:
My Web Server
Press CONTROL + X to exit nano, typing y when prompted to save.
Now you've created your page, you need to set permissions and ownership.
sudo chown YourName:www-data index.html
sudo chmod 644 index.html
Finally, we'll restart Apache.
sudo service apache2 restart
To access your new page you'll need to get the IP address for your Raspberry Pi.
If your Pi uses an Ethernet network connection, type:
If your Pi uses WIFI instead, type:
Look for the line begining with 'inet addr:'
My line looks like:
inet addr:10.0.0.101 Bcast:10.0.0.255 Mask:255.255.255.0
This means my IP address is 10.0.0.101
Open a web browser on a computer or phone and type the Pi IP address into the address bar.
If you have password-protected the entire web folder you will be prompted for the username and password we set earlier.
You should see your new landing page.