Skip to content

Web Server Full LAMP Stack With Virtual Hosts

Paul edited this page Sep 10, 2019 · 1 revision

If you enabled SSH in the optional section you can now configure your Raspberry Pi remotely, using an SSH client: Putty is a commonly used client for windows. MacOS and Linux have built-in clients

By configuring virtual hosts you can host multiple independent websites on the same server

If you are planning to use a Dynamic DNS provided hostname. It is recommended to go through our Dynamic DNS Setup before continuing

Install Apache, Maria-DB and PHP

The instructions below assume a basic knowledge of the vi text editor. It's use can be substituted by the editor of your choice

Apache

sudo apt-get install apache2 apache2-utils

By default, Apache listens on all IP addresses available to it and directs all requests to the primary website. To direct requests for your domain to the correct site we will configure it as a virtual host. For all steps below, replace lighting.local with your domain name.

Create a copy of the default Apache configuration file for your site:

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

Edit the new lighting.local.conf configuration file:

  • Uncomment ServerName and replace lighting.local with your site’s IP or Fully Qualified Domain Name (FQDN).
  • Enter the document root path and log directories as shown below
  • Add a Directory block before <VirtualHost>:

sudo vi /etc/apache2/sites-available/lighting.local.conf

Example:

<Directory /var/www/html/lighting.local/public_html>
       Options Indexes FollowSymLinks
       AllowOverride All
       Require all granted
</Directory>
<VirtualHost *:80>
        ServerName lighting.local
        ServerAlias www.lighting.local
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/lighting.local/public_html
        ErrorLog /var/www/html/lighting.local/logs/error.log
        CustomLog /var/www/html/lighting.local/logs/access.log combined
</VirtualHost>

The file example above has all comment sections removed for brevity; you may keep or remove the commented areas as you see fit. The ServerAlias directive allows you to include multiple domain names or subdomains for a single host. The example above allows visitors to use lighting.local or www.lighting.local to navigate to this virtual host.

Create the directories referenced above:

sudo mkdir -p /var/www/html/lighting.local/{public_html,logs}

Make sure that you do not put space after the comma between public_html and logs because it will create a folder named {public_html, and will cause an error when you reload Apache.

Change Permissions on the html directory so the pi user ID has read/write/execute permissions and the Apache server has read/execute.

sudo chmod 755 -R /var/www/html
sudo chown -R pi:www-data /var/www
sudo chmod u+rxw,g+rx-w,o-rwx /var/www

Link your virtual host file from the sites-available directory to the sites-enabled directory:

sudo a2ensite lighting.local.conf

Disable the default virtual host to minimize security risks:

sudo a2dissite 000-default.conf

Edit apache.conf to remove excessively informative server headers:

sudo vi /etc/apache2/apache2.conf

To turn off the excessive header info, add the lines below to the end of the file.

ServerSignature Off
ServerTokens Prod

Restart Apache:

sudo /etc/init.d/apache2 restart

Maria-DB

Maria-DB AKA MySQL is not needed for this project but is useful if you want to use this server for other things

sudo apt install mysql-server

Secure the Database

sudo mysql_secure_installation

  • Hit enter when prompted for the password, as there is no default root password.
  • Hit enter to set a new password
    • Type the new password, hit enter
    • Re-type the password, hit enter
  • Hit enter for each of the remaining prompts

Create a Database

Log into MySQL:

sudo mysql -u root

Create a database and a user with permissions for it:

In this example, the database is called webdata, the user: webuser, and password: password:

CREATE DATABASE webdata;
GRANT ALL ON webdata.* TO 'webuser' IDENTIFIED BY 'password';

PHP

sudo apt-get install php libapache2-mod-php php7.0-curl php-xml php-pear php7.0-mysql php7.0-cgi

Restart Apache to recognize the PHP installation

sudo /etc/init.d/apache2 restart

Installation Complete

You should now have a fully functioning webserver capable of running the TCPLightingWebInterface

The project files should be placed in /var/www/html/lighting.local/public_html

You are now ready to move on to Dynamic DNS Setup