Md Toy Blog

Set up Apache with Vhosts on Ubuntu 18.04


  1. Make sure ubuntu is up to date
   sudo apt update
  1. Install Apache 2
   sudo apt install apache2
  1. You can set up the firewall (optional)
  2. 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
   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
  1. Check if you can reach the default site with your browser Go to http://localhost. You should be created with "Apache2 Ubuntu default Page"
  2. 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>
        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
        #set the file that appache will serve if directory is requested
        <IfModule dir_module>
            DirectoryIndex index.php index.html
  1. Enable the new vhost and disable the default one:
   sudo a2ensite tests.js.conf
   sudo a2dissite 000-default.conf
  1. 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 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

  1. Restart apache to load your configuration:
   sudo systemctl restart apache2