How to get Apache Virtual Host working on Ubuntu?

5,088

In your config file you have:

ServerName testapp

But in your URL you are using:

http://localhost/testapp

You will need your DNS server to map testapp to the IP address of your server. Or, edit your /etc/hosts file, adding a line such as:

127.0.0.1  testapp

Replace 127.0.0.1 with your server's IP address if you are trying to reach it from a different machine.

Then, you can access the host using the url http://testapp

If you want to access the site using http://localhost/testapp, you don't need to do any of this. Instead, you can just use the default virtualhost.

Share:
5,088
Edward Tanguay
Author by

Edward Tanguay

Updated on September 18, 2022

Comments

  • Edward Tanguay
    Edward Tanguay over 1 year

    I have Apache installed on an Ubuntu virtual machine.

    Typing http://localhost in the browser brings up the Apache intro site, so I know Apache works.

    I created the file /home/test/webs/testapp/index.html:

    This is a <b>test</b>.
    

    I created the file /etc/apache2/sites-available/testapp.conf:

    <VirtualHost *:80>
     ServerAdmin webmaster@localhost
     ServerName testapp
     ServerAlias testapp
     DocumentRoot /home/test/webs/testapp
    <Directory /home/test/webs/testapp>
     Options -Indexes +FollowSymLinks
     AllowOverride All
    </Directory>
     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    I enabled the virtual host:

    sudo a2ensite testapp.conf
    

    I restarted the Apache server:

    systemctl reload apache2
    

    But when I go to http://localhost/testapp, Apache responds:

    Not Found

    The requested URL was not found on this server. Apache/2.4.41 (Ubuntu)

    Server at localhost Port 80

    What else do I have to do to get my virtual host to work on Ubuntu?

  • Edward Tanguay
    Edward Tanguay over 3 years
    I added 192.168.30.142 testapp to my /etc/hosts file, and now when I go to http://testapp (or http://testapp/index.html), it tells me "Forbidden: You don't have permission to access this resource." Where do I provide this permission for the anonymous web user?
  • Edward Tanguay
    Edward Tanguay over 3 years
    Also, @Bert what do you mean exactly by "use the default virtualhost". When I look for a default virtual host file in /etc/apache2/sites-available, I see only two files: (1) 000-default.conf which I understand is merely a template to make other .conf files, hence the 000-, and (2) default-ssl.conf which I understand is only for secure connections and so not relevant in my case.
  • Bert
    Bert over 3 years
    Put your web documents in the DocumentRoot in 000-default.conf, it's a working configuration.
  • Edward Tanguay
    Edward Tanguay over 3 years
    Yes, I know I can put my web documents under /var/www/html/testapp and they will be available at http://localhost/testapp but I am trying to set up virtual hosts so that I have can have e.g. numerous users who each have a public_html directory under their home directory which will be accessible via a separate virtual host, e.g. /home/user1/public_html available at http://192.168.30.142/user1 and /home/user2/public_html available at http://192.168.30.142/user2, etc. Your etc/hosts tipped helped, but how do I give the web user permission, see above.
  • Bert
    Bert over 3 years
    Then you should look into mod_userdir, and create a new question if you can't get it working.
  • Edward Tanguay
    Edward Tanguay over 3 years
    I reasked the question regarding the "Forbidden" error here: serverfault.com/questions/1033541/…