Set up Apache with Vhosts on Ubuntu 18.04

  1. Make sure ubuntu is up to date

    sudo apt update
    
  2. Install Apache 2

    sudo apt install apache2
    
  3. You can set up the firewall (optional)

  4. Check if apache's status

    sudo systemctl status apache2
    

    Should output

    ● apache2.service - The Apache HTTP Server
    Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
    Drop-In: /lib/systemd/system/apache2.service.d
            └─apache2-systemd.conf
    Active: active (running) since Thu 2019-03-14 15:26:24 CET; 2min 20s ago
    Main PID: 12952 (apache2)
     Tasks: 55 (limit: 4915)
    CGroup: /system.slice/apache2.service
            ├─12952 /usr/sbin/apache2 -k start
            ├─12953 /usr/sbin/apache2 -k start
            └─12954 /usr/sbin/apache2 -k start
    
  5. Check if you can reach the default site with your browser Go to http://localhost. You should be created with "Apache2 Ubuntu default Page"

  6. Create your virtual host named for example: tests.js

    sudo vim /etc/apache2/sites-available/tests.js.conf
    
    • Paste these directives in the file by pressing in a sequence ", +, p
     <VirtualHost *:80>
         ServerAdmin admin@example.com
         ServerName tests.js
         ServerAlias www.tests.js
         DocumentRoot "/home/g/Documents/workspace/js/tests"
         ErrorLog ${APACHE_LOG_DIR}/error.log
         CustomLog ${APACHE_LOG_DIR}/access.log combined
         <Directory "/home/g/Documents/workspace/js/tests">
             Options Indexes FollowSymlinks
             AllowOverride All
             Require all granted
         </Directory>
         #set the file that appache will serve if directory is requested
         <IfModule dir_module>
             DirectoryIndex index.php index.html
         </IfModule>
     </VirtualHost>
    
  7. Enable the new vhost and disable the default one:

    sudo a2ensite tests.js.conf
    sudo a2dissite 000-default.conf
    
  8. Test your configuration

    sudo apachectl configtest
    > Syntax OK
    

    You may get the following Warning:

    AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
    Syntax OK
    

    The message is self explanatory: do sudo vim /etc/apache2/apache2.conf and type somewhere in a new line ServerName localhost

  9. Restart apache to load your configuration:

    sudo systemctl restart apache2