Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Web Servers»Apache»How To Create Apache VirtualHost in Ubuntu 18.04 & 16.04 LTS

    How To Create Apache VirtualHost in Ubuntu 18.04 & 16.04 LTS

    RahulBy RahulNovember 17, 20153 Mins ReadUpdated:July 25, 2019

    Virtual Hosting refers to running multiple domains (or multiple websites) on a single server. The best uses of Virtual Hosting can be seen on shared hosting servers, where thousands of websites hosted on a single server and share the single system resources.

    This article will help you to create Virtual hosts in Apache2 server on Ubuntu, Debian & LinuxMint systems.

  • Read: Set Up Virtual Hosts in Nginx on Ubuntu
  • 1. Install Apache

    You may skip this option if you already have installed Apache server in your system. If not use the following command to install Apache server on Ubuntu system. Also as a best practice don’t forget to update repositories metadata.

    sudo apt-get update
    sudo apt-get install apache2
    

    Step 2: Create Apache VirtualHost

    In Apache on Ubuntu all the virtual host configuration files are stored under /etc/apache2/sites-available directory. With the new Apache installation you will find a default virtual host file there. Create a new Virtual Host configuration file by copying default file.

    cd /etc/apache2/sites-available/
    sudo cp 000-default.conf site1.example.com.conf
    

    Now Edit new virtual host configuration file and update as per your requirement. My site1.example.com configuration file looks like below.

    vim site1.example.com.conf
    
    <VirtualHost *:80>
            ServerAdmin [email protected]
            ServerName site1.example.com
            DocumentRoot /var/www/site1.example.com/httpdocs
    
            <Directory />
                    Options FollowSymLinks
                    AllowOverride None
            </Directory>
            <Directory /var/www/>
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride None
                    Order allow,deny
                    allow from all
            </Directory>
    
            ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
            <Directory "/usr/lib/cgi-bin">
                    AllowOverride None
                    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                    Order allow,deny
                    Allow from all
            </Directory>
    
            ErrorLog ${APACHE_LOG_DIR}/error.log
            LogLevel warn
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    Now create a directory structure in your system, as per defined in above virtual host, and assign proper ownership and file permission.

    mkdir -p /var/www/site1.example.com/httpdocs
    chmod 755 /var/www/site1.example.com/httpdocs
    chown www-data.www-data /var/www/site1.example.com/httpdocs
    

    Now upload your project files on /var/www/site1.example.com/httpdocs/ directory. For this example we have created a index.html file for testing purpose like below.

    echo "<h1>site1.example.com</h1>" > /var/www/site1.example.com/httpdocs/index.html
    

    Step 3: Enable First Virtual Host

    Now we have successfully created our first virtual host in Apache. Now use the following command to enable this Virtual Host, So that Apache can load this configuration file on next reload.

    a2ensite site1.example.com
    

    Basically this creates a soft link of Virtual Host configuration file in directory /etc/apache2/sites-enabled/. To activate the new configuration, we need to run:

    service apache2 reload
    

    Now you can access http://site1.example.com in your browser. If you don’t have dns configured for your domain, do a local mapping by adding an entry in /etc/hosts files.

     ...
     192.168.1.100  site1.example.com
     ...
    

    Step 4: Create Another Virtual Host

    If you want to add another virtual host on your server, just repeat step 2 and 3 with changing your site name like site2.example.com.

    Enjoy Virtual Hosting!

    a2ensite Apache Apache2 httpd virtualhost
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install CentOS Web Panel on CentOS, RedHat and cloudLinux
    Next Article How to Install ImageMagick on Ubuntu 18.04 & 16.04 LTS

    Related Posts

    How to Install Apache ActiveMQ on Ubuntu 22.04

    3 Mins Read

    How To Install LAMP Stack on Ubuntu 22.04 LTS

    Updated:April 20, 20225 Mins Read

    How To Disable HTTP Methods in Apache

    Updated:December 31, 20212 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    Common Apache Commands on Ubuntu & Debian

    4 Mins Read

    How to Install and Secure Apache on Debian11

    6 Mins Read

    4 Comments

    1. István Radics on October 17, 2019 5:15 pm

      Hi, Rahul,
      the second site take me to the apache default page ?
      Can you tell me what did I wrong?
      BR,
      István

      Reply
    2. Robin Chaudhary on July 25, 2019 6:22 am

      i got stuck while copying default configuration host file
      could not get the same result as above.
      can you help me?

      Reply
      • Rahul on July 25, 2019 7:04 am

        Hi Robin, What issue are you facing?

        Reply
    3. YulianD on July 25, 2018 8:46 pm

      gracias por el aporte, me ha sido de gran ayuda.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.