Installing Apache on Ubuntu 18.04 Server with Virtual Hosts

Installing Apache on Ubuntu 18.04 with Multiple Domains

Apache is the most widely-used web server in the world with approximately 45 percent of active sites running on it.  In this guide we will install and configure the Apache2 HTTP web server on Ubuntu 18.04 Server (Bionic Beaver). You can use this guide for a single domain website or multiple domains using Virtual Hosts.


You should use a non-root user account with sudo privileges. Please see the Initial server setup for Ubuntu 18.04 guide for more details.

1. Install Apache

Let’s begin by installing Apache from the Ubuntu repository. Press y and ENTER if prompted to install.

sudo apt install apache2

Installation may take a few minutes. Once installed, continue to Step 2 to configure the firewall.

2. Configure Firewall

It is highly recommended that you configure a firewall for added security.

We’ll start by adding a firewall rule for SSH because if you are configuring your server remotely, you don’t want to get locked out when enabling the firewall! You may have already done this in the our Initial server setup for Ubuntu 18.04 guide, but it’s no harm adding it again just in case. If the rule already exists, the command will just skip it.

sudo ufw allow OpenSSH

Now we can add the firewall rules for Apache.

sudo ufw allow in "Apache Full"

Now enable the firewall if it isn’t already.

sudo ufw enable

Press y if you see a message “Command may disrupt existing ssh connections”.

If the firewall was activated correctly, you should see “Firewall is active and enabled on system startup“.

You can also check the current firewall status with:

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)

Above we can see the firewall is active and has two rules per service. v6 is short for IPv6. This is the new Internet Protocol, which was introduced to deal with the long-anticipated problem of IPv4 address exhaustion.

3. Test Apache

To see if Apache installed correctly, we can check the current Apache service status.

sudo service apache2 status

If it is up and running, you should see a green active state.

 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 Sat 2018-03-31 08:44:04 CEST; 15min ago
Main PID: 5727 (apache2)
Tasks: 55 (limit: 4915)
CGroup: /system.slice/apache2.service
├─5727 /usr/sbin/apache2 -k start
├─5728 /usr/sbin/apache2 -k start
└─5729 /usr/sbin/apache2 -k start

Mar 31 08:44:04 ubuntu1804 systemd[1]: Starting The Apache HTTP Server...
Mar 31 08:44:04 ubuntu1804 apachectl[5675]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 
Mar 31 08:44:04 ubuntu1804 systemd[1]: Started The Apache HTTP Server.

If you get the above error about a fully qualified domain name, you can ignore it.

You may need to press q to exit the server status.

Now that the Apache service is up and running, you should be able to view the test Apache web page through your web browser. Enter the IP address of your server in the address bar and hit ENTER.

If you don’t know your IP, you can find out with the following command.

ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v ''

You’re all set! You can find this Apache default welcome page in the folder /var/www/html. To edit this file:

sudo nano /var/www/html/index.html

Press CTRL + X to exit the nano text editor.

Your Apache web server is ready to go. You can now add your own html files and images the the /var/www/html directory as you please. However, you should acquaint yourself with and set up at least one Virtual Host for Apache in the next step as most of our Ubuntu 18.04 guides are written with Virtual Hosts in mind.

Virtual Hosts allow you to host multiple web sites/domains on one server. Even you only ever intend on hosting one website or one domain, it’s still a good idea to configure at least one Virtual Host.

If you wish to instead go straight to setting up PHP on your new Apache web server, please see guide Installing PHP for Apache on Ubuntu 18.04.

4. Set Up Virtual Hosts

If you wish to host multiple sites/domains on Apache, you should now set up your directory structures and Virtual Hosts. Even if you only want to host one site/domain, it’s a good idea to set up a directory and Virtual Host now because if you ever need to add a new domain later, it will makes things a lot easier for you.

For the purposes of this guide, we will make a virtual host for and another for You can substitute these with your own registered domains, or if you don’t have any domains yet, you can still follow this guide and add and to your hosts file to trick your OS into resolving these domains in the browser. We explain how to do this at the end of the guide.

4.1. Create Directories and Set Permissions

Let’s create two new directories in the /var/www/ directory for our two domains.

sudo mkdir -p /var/www/
sudo mkdir -p /var/www/

If we want our regular non-root user to be able to modify files in these directories, we must change the ownership.

sudo chown -R $(whoami):$(whoami) /var/www/
sudo chown -R $(whoami):$(whoami) /var/www/

The $(whoami) variable will take the value of the user you are currently logged in as.

We must also change the permissions for the general web directory /var/www and its contents so that pages can be served correctly.

sudo chmod -R 755 /var/www

4.2. Create Test Web Pages

We’ll now create a simple index.html web page for each domain using the echo command.

Don’t forget to replace with your own domain if you have one.

sudo echo "Welcome to!" > /var/www/

Now do the same for

sudo echo "Welcome to!" > /var/www/

4.3. Create New Virtual Host Files

The Virtual Host files located in /etc/apache2/sites-available/ are used to tell the Apache web server how to respond to various domain requests.

Let’s create a new virtual host file for our domain.

sudo nano /etc/apache2/sites-available/

In nano, paste in the block below. To paste into nano, press the right mouse button.

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Be sure to change all instances of to your own domain if you have one.

Save and close nano (Press CTRL + X and then press y and ENTER to save changes)

We can now repeat the above process for

sudo nano /etc/apache2/sites-available/

In nano, paste in the block below. To paste into nano, press the right mouse button.

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Be sure to change all instances of to your own domain if you have one.

Save and close nano (Press CTRL + X and then press y and ENTER to save changes)

4.4. Enable the New Virtual Host Files

Now that we have our two virtual host files in place, we need to use the a2ensite tool to enable them.

Remember again to replace and with your own domains.

sudo a2ensite
sudo a2ensite

Almost done! You must now reload the Apache service to activate the new configuration.

sudo systemctl reload apache2

Assuming you have already configured DNS on your domain registrar to point your domains to the IP of your Apache server, you should now be able to view these test webpages in the web browser. If you don’t have your own domains and just want to test, continue to Step 4.5 to edit your hosts file.

Apache web page test

4.5 Edit Hosts file (optional)

If you do not have any domains registered and instead just want to load and as a test, you can edit the hosts file in your OS to point these domains to your server.

To edit hosts file in Linux or Mac, run sudo nano /etc/hosts. In Windows, follow this guide to edit hosts. Once hosts files is open, enter two new lines


Replace x.x.x.x with your web server’s IP.

If you don’t know your web server’s IP, you can find out with:

ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v ''

Once you’ve saved you hosts file, you should be able to access and in your browser.

5. Configure Apache (Optional)

Now that you have Apache up and running, there may be some common configuration changes that will be useful to you.

5.1. Enable AllowOverride

You will find that .htaccess will be ignored by default in Apache. If this is something you will need, we can enable it by altering the Apache configuration file.

Firstly, backup the configuration file.

sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak

Open the config file.

sudo nano /etc/apache2/apache2.conf

Scroll down the the following section.

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted

AllowOverride None means that .htaccess will be ignored. Change it to AllowOverride All.

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted

Save and exit (press CTRL + X, press Y and then press ENTER)

Restart Apache.

sudo systemctl restart apache2

5.2. Enable mod_rewrite

If you want to later configure some rules in .htaccess, you will most likely need to enable mod_rewrite.

sudo a2enmod rewrite

Restart Apache.

sudo systemctl restart apache2

What Next?

Now that you have your Apache server tested and working, the next step is to install PHP and MySQL, both required for most popular web applications.

Let me know in the comments if this helped. Follow me @DevAnswers or buy me a beer 🍺


Your email address will not be published. Required fields are marked *

We use Markdown to style comments, like on Github and Reddit.
To do a line break, type two spaces after the sentence.
You can add inline code by wrapping it in backticks: `code here`

    To do an entire block of code  
    type four spaces before the line
    and it will appear in a block like this.
    <-- four empty spaces