Install Apache

First, make sure you update the package manager.

sudo apt update
sudo apt upgrade
sudo apt update

Then, install the apache2 package.

sudo apt install apache2

Now, make sure that apache has access to the webservers public folder. You may need to rerun this, when you add new folders/files to your server.

sudo chown -R pi:www-data /var/www/html/
sudo chmod -R 770 /var/www/html/

To test the installation you can run the following test.

wget -O check_apache.html
cat ./check_apache.html

Create a virtual host

Go to the Apache folder for available sites.

cd /etc/apache2/sites-available

Now create a new file for the vhost you want to create. Use <virtualhost.domain>.conf as name.

sudo nano <virtualhost.domain>.conf

As content you can use the following template and replace the ServerName and DocumentRoot.

<VirtualHost *:80>
        ServerName <virtualhost.domain>
        DocumentRoot /var/www/html/<folder>

        <Directory "/var/www/html/<folder>">
                Options FollowSymLinks
                AllowOverride All
                Order allow,deny
                Allow from all
                Require all granted

        ErrorLog ${APACHE_LOG_DIR}/<virtualhost.domain>/error.log
        CustomLog ${APACHE_LOG_DIR}/<virtualhost.domain>/access.log combined

Also make sure, that the log directories you defined are already existing. Else Apache will fail to restart in the next step!

sudo mkdir /var/log/apache2/<virtualhost.domain>

Now, enable the new vhost and restart Apache.

sudo a2ensite <virtualhost.domain>
sudo service apache2 restart

Connecting to a virtual host from a remote machine

Make sure to update the hosts file with a redirect to the raspberry for the new domain.

To do that navigate to the hosts file and open it.

cd /etc/
sudo nano hosts

Now add the following line and save the file.

<ipOfRaspberryPi>    <virtualhost.domain>

Enable mod_rewrite

By default the mod_rewrite is not enabled and thus you can not use .htaccess files to create redirects. To change this execute the following command to enable mod_rewrite for Apache.

sudo a2enmod rewrite

More information can be found here.

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht.