How to get Apache Virtual Host working on Ubuntu?
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.
Edward Tanguay
Updated on September 18, 2022Comments
-
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 over 3 yearsI added
192.168.30.142 testapp
to my/etc/hosts
file, and now when I go tohttp://testapp
(orhttp://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 over 3 yearsAlso, @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 the000-
, and (2)default-ssl.conf
which I understand is only for secure connections and so not relevant in my case. -
Bert over 3 yearsPut your web documents in the DocumentRoot in
000-default.conf
, it's a working configuration. -
Edward Tanguay over 3 yearsYes, I know I can put my web documents under
/var/www/html/testapp
and they will be available athttp://localhost/testapp
but I am trying to set up virtual hosts so that I have can have e.g. numerous users who each have apublic_html
directory under theirhome
directory which will be accessible via a separate virtual host, e.g./home/user1/public_html
available athttp://192.168.30.142/user1
and/home/user2/public_html
available athttp://192.168.30.142/user2
, etc. Youretc/hosts
tipped helped, but how do I give the web user permission, see above. -
Bert over 3 yearsThen you should look into
mod_userdir
, and create a new question if you can't get it working. -
Edward Tanguay over 3 yearsI reasked the question regarding the "Forbidden" error here: serverfault.com/questions/1033541/…