acy
Posts: 3
Joined: Thu Feb 01, 2018 5:59 pm

configuring multiple virtual hosts in apache2

Wed Apr 11, 2018 2:16 pm

I can create virtual hosts in apache2 on (Apple) OSX 10, but have yet to achieve the same in apache2 on the pi. I have followed the instructions (as they appear in the pi forum and from articles on the web) but with no success.
I appreciate that on the pi, the virtual host declarations are created in individual .conf file under /etc/apache1/sites-enabled. However although the privileges of the DocumentRoot are as that of /var/www/, when I try to access the the relevant web pages remotely , I always get the message that the requested url is not found.

A typical .conf file contains:

<VirtualHost blahblah.com>
DocumentRoot /mnt/data1/public/Sites/html/blahblah
Servername localhost
ServerAlias www.blahblah.com
<VirtualHost>

Any comments/suggestions/help would be greatly appreciated.

knute
Posts: 473
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: configuring multiple virtual hosts in apache2

Thu Apr 12, 2018 2:53 pm

acy wrote:
Wed Apr 11, 2018 2:16 pm
A typical .conf file contains:

<VirtualHost blahblah.com>
DocumentRoot /mnt/data1/public/Sites/html/blahblah
Servername localhost
ServerAlias www.blahblah.com
<VirtualHost>

Any comments/suggestions/help would be greatly appreciated.
Try <VirtualHost *:80> and ServerName blahblah.com

MaxVMH
Posts: 114
Joined: Mon Mar 19, 2018 1:26 pm

Re: configuring multiple virtual hosts in apache2

Thu Apr 12, 2018 3:17 pm

acy wrote:
Wed Apr 11, 2018 2:16 pm
I appreciate that on the pi, the virtual host declarations are created in individual .conf file under /etc/apache1/sites-enabled.
You should create them in /etc/apache2/sites-available (eg. /etc/apache2/sites-available/nameofthewebsite.conf) and enable them with (this will create an alias in /etc/apache2/sites-enabled/):

Code: Select all

sudo a2ensite nameofthewebsite

I'm not sure if the changes go into effect immediately so you might want to restart apache afterwards (it will show you errors if something went wrong):

Code: Select all

sudo service apache2 restart

This is an example of a virtualhost .conf file like I use them (@others feel free to say how I can improve them):

Code: Select all

<VirtualHost *:80>
        ServerName www.example.com
        ServerAlias example.com
        
        DocumentRoot /home/myusername/websites/example.com/public_html
        <Directory "/home/myusername/websites/example.com/public_html">
                Options FollowSymLinks
                AllowOverride All
                Order allow,deny
                Allow from all
                Require all granted
        </Directory>
        
        ErrorLog ${APACHE_LOG_DIR}/example.com/error.log
        CustomLog ${APACHE_LOG_DIR}/example.com/access.log combined
</VirtualHost>
(Please use logs like in this example - it's useful to look for suspicious traffic, eg. on your phpmyadmin or WordPress admin login page, but best is to disable these from public access.)
Webserver stack: 2x Pi 3B | 1x Pi 3B+ ||| Pi Zero W for fooling around

Working on a soccer prediction web app:
https://github.com/MaxVMH/mijnscore
Attempt at writing up-to-date tutorials on making a Raspberry Pi webserver:
http://blog.mijnscore.be/

acy
Posts: 3
Joined: Thu Feb 01, 2018 5:59 pm

Re: configuring multiple virtual hosts in apache2

Thu Apr 12, 2018 9:17 pm

I have reloaded apache via sudo system to reload apache2 with no errors reported and sudo acachectl configtest returns ‘ok’. Will add some logs as a further measure
Thanks

acy
Posts: 3
Joined: Thu Feb 01, 2018 5:59 pm

Re: configuring multiple virtual hosts in apache2

Fri Apr 13, 2018 4:28 pm

Problem resolved, somewhat embarrassing, but I forgot to update the hosts file on the remote computer to point the virtualhosts to the pi that was running the apache server.
In hosts file : raspberry pi's : IP address www.blahbah.com
raspberry pi's : IP address blahbah.com

Apologies for wasting all your time. But your comments did help by making me rethink through my problem!

Return to “General discussion”