SOHO : Small Office Home Office
Freeware - Opensource software tips, tricks, tweaks & fixes for managing, securing, improving the performance of SOHO Desktop, Laptop, Networks

Monday, September 14, 2020

Apache2 : Setup named virtual hosts

A server is setup on local lan which acts as a development server, backup server, a secure domain to manage database servers and host other important data on local lan. The setup is same for a valid FQDN which can be accessed from WAN.

Server : Ubuntu 18.04 LTS
Application server :  LAMP stack
Main site path : /var/www/html
domain name : myserver.lan
Virtual host path :  /var/www/vhosts                 
domain name1 : your domain name. 
Example : dev.myserve.lan
domain name2 : your domain name. 
Example : secure.myserver.lan
  • Create config files for vhosts 
run command :             
cd /etc/apache2/sites-available
sudo cp  000-default.conf  dev-myserver-lan.conf
sudo cp 000-default.conf  secure-myserver-lan.conf

  • Edit config files:
sudo nano /etc/apache2/sites-available/secure-myserver-lan.conf
# Note: you can add ip as <VirtualHost 192.168.1.1:80> to respond to a specific ip.
<VirtualHost *:80>
    ServerName secure.myserver.lan
    #ServerAlias www.myserver.lan 
    ServerAdmin secure_myserver_lan@localhost
    DocumentRoot /var/www/secure
# This site can be accessed from local lan  and ip range (192.168.*.*) only
    <Directory /var/www/secure/>
        require host localhost
        require ip 127.0.0.1
        require ip 192.168
    </Directory> 
# very important set logs for each site (personal preference)
    ErrorLog ${APACHE_LOG_DIR}/secire_myserver_lan-error.log
 
    CustomLog ${APACHE_LOG_DIR}/secure_myserver_lan-access.log combined

<VirtualHost>

  • Enable virtual host
# sudo a2ensite your-domain-conf
sudo a2ensite secure-myserver-lan.conf
sudo systemctl restart apache2

  • Disable virtualhost
# sudo a2ensite your-domain-conf
sudo a2dissite secure-myserver-lan.conf
sudo systemctl restart apache2

  • Routing : If the domains are on local lan, you can forward the requests from your router or add to local host file
sudo nano /etc/hosts
[...]
192.168.1.251    secure.myserver.lan
192.168.1.251    dev.myserver.lan
[...]

  • To access the site from a browser use the http or https tag without fail or you may be redirected to websearch
Browser address bar

Wrong : dev.myserver.lan

Right : http://dev.myserver.lan

Right : https://dev.myserver.lan 

No comments:

Post a Comment