Setup and Configuration Multi Domain Apache2 on Ubuntu

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, avr: 5.00)

Ubuntu Server is a variant of Ubuntu as the Operating System. it’s developed by Canonical and open source for free to use. Setup configuration multi-domain Ubuntu can be used for Networks and various services.

Apache web server is designed to have the ability to host one or more HTTP-based websites. Notable features include the ability to support multiple programming languages.

Following below steps tutorial for basic installation and setup guides

Prerequisites

You have domains or subdomain name pointing to your public server IP. on the following tutorial we will use lab.axfon.comand lab2.axfon.com


Step 1: Install Apache2 as Web Server

To install Apache2 stable version by default repository Ubuntu, use the following single command to update and install Apache2

sudo apt update && sudo apt install apache2

Once Apache2 already installed, to check and verify Apache service status type the command below

sudo service apache2 status

If it is running and working properly you will see the output Apache2 active (running) like below

● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-01-02 17:58:26 UTC; 1min 54s ago
 Main PID: 14259 (apache2)
    Tasks: 55 (limit: 665)
   Memory: 6.1M
   CGroup: /system.slice/apache2.service
           ├─14259 /usr/sbin/apache2 -k start
           ├─14261 /usr/sbin/apache2 -k start
           └─14262 /usr/sbin/apache2 -k start
Jan 02 17:58:26 axfon-lab systemd[1]: Starting The Apache HTTP Server...
Jan 02 17:58:26 axfon-lab systemd[1]: Started The Apache HTTP Server.

To exit Apache2 service status just press q on the keyboard


Step 2: Configuration the Firewall with UFW

UFW (Uncomplicated Firewall) is an Iptable interface to easily configure a firewall on your system. If it does not yet enable, it’s recommended to enable and setup the rule for Apache

For the first, we have to add rule for SSH, let’s se the command line below

sudo ufw allow OpenSSH

Add the rule receive HTTP to Apache2

sudo ufw allow 'Apache Full'

Then you will see on terminal

Rule added
Rule added (v6)'

Now enable ufw for Firewall.

sudo ufw enable

If prompted just pres Y to accept and continue and to check UFW status type the command below

sudo ufw status

You will see the output UFW active (running) like below

Status: active

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

Apaceh2 web server now is ready on Ubuntu, now you may go to your web browser and visit your domain or IP. If you have not configured domain name yet and don’t know your IP, use the following command find out

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

If everything is OK you will be presented on your browser with the default Apache2 landing page as below


Step 3: Create Directory and Grant Permission

On this guides, we are going to create the directory which contains the website files for domain lab.axfon.com and lab2.axfon.com. (you may change these with your own domain name).

To create directory in the location /var/www use the following command

sudo mkdir -p /var/www/lab.axfon.com/public_html
sudo mkdir -p /var/www/lab2.axfon.com/public_html

Set permission ownership for current user $(whoami) to be able to modify any files in these directories.

sudo chown -R $(whoami):$(whoami) /var/www/lab.axfon.com/public_html
sudo chown -R $(whoami):$(whoami) /var/www/lab2.axfon.com/public_html

Grant Directory Permission and its contents.

sudo chmod -R 755 /var/www

Step 4: Configuration Virtual Hosts on Apache2

If you wish to host multiple or one sites/domain, the way should to do is setting up virtual hosts. There are several ways to set up virtual hosts on Apache. The recommended method is the following steps outline will make things a lot easier for you.

The Virtual Host files are located in /etc/apache2/sites-available/  to allow web server respond to various domain requests.

To create a configuration file, copy default file to new files for our domain lab.axfon.com andlab2.axfon.com(replace it with your domain name)

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/lab.axfon.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/lab2.axfon.com.conf

Open these files to edit and configure using nano editor with the following command

sudo nano /etc/apache2/sites-available/lab.axfon.com.conf

Enter the document root path (change with your own) and log directories as shown below, and add Directory  block before <VirtualHost>:

<Directory /var/www/>
        Require all granted
        Options Indexes FollowSymLinks
        AllowOverride All
</Directory>
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName lab.axfon.com
        ServerAlias www.lab.axfon.com
        DocumentRoot /var/www/lab.axfon.com/public_html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Press Ctrl/Cmd + X and then press Y and ENTER to save changes and close nano

Do the same way as above for the second virtual host for a second domain

sudo nano /etc/apache2/sites-available/lab2.axfon.com.conf

Enter the document root path(change with your own) and log directories as shown below, and add Directory  block before <VirtualHost>:

<Directory /var/www/>
        Require all granted
        Options Indexes FollowSymLinks
        AllowOverride All
</Directory>
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName lab2.axfon.com
        ServerAlias www.lab2.axfon.com
        DocumentRoot /var/www/lab2.axfon.com/public_html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Press Ctrl/Cmd + X and then press Y and ENTER to save changes and close nano


Step 5: Create Testing File

Now we will create a file into each domain root directory

sudo echo "Thanks Axfon One" > /var/www/lab.axfon.com/public_html/index.html
sudo echo "Thanks Axfon Two" > /var/www/lab2.axfon.com/public_html/index.html

Step 6: Restart Apache2

Changes made in the Apche2 configuration file will not be applied until the command to Restart and Before testing on the Browser we have to verify and make sure no error.
To restart Apache2 use the following the command line

sudo service apache2 restart

Step 7: Disable Default Virtual Host (Optional)

For any other security reasons, we may disable default virtual host

sudo nano /etc/apache2/sites-available/000-default.conf

Add this anywhere within the <VirtualHost *:80> block.

<Location />
    Deny from all
    Options None
    ErrorDocument 403 Forbidden.
</Location>

Now restart again Apache2 web server

sudo service apache2 restart

Step 8: Testing Webpage on the Browser

You can now view this page in your web browser by visiting your server’s domain name or public IP address …Congrats..!!


If this tutorial could help you, please rate above Star button rating and share to help others find it! Feel free to leave a comment below.

Recommended For You

Axfon

About the Author: Axfon

Axfon.com is a site Online publisher for collection of guides and tutorials about Internet Technology