-1

If I create one site, I can access it with http://localhost ...

But if I create multiple virtual sites, with apache, on the same computer, http://localhost won't work.

How can I access it? (on the same computer, aka localhost)?

1
  • 1
    The technology you are looking for is called 'name based virtual hosting'. There are hundreds of Q&A about this on ServerFault. Commented Dec 19, 2021 at 9:18

3 Answers 3

1

To create a virtual site, try out the following commands; just replace "newsite" with the name of your site:

NOTE

  • Tested on Ubuntu 20.04, using Apache 2.4.41 and Firefox 95.0.
  • All commands were run from the home (~/) directory.
  • You must create your own index.html file in the home directory.
# Add the new site to the default Apache directory sudo mkdir --parents /var/www/newsite # Create your web page and place it in the directory: sudo cp ~/index.html /var/www/newsite/index.html sudo chmod 755 /var/www/newsite/index.html # Copy and modify a virtual host configuration file sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/newsite.conf sudo sed --in-place "s/webmaster@localhost/webmaster@newsite/g" /etc/apache2/sites-available/newsite.conf sudo sed --in-place "s/DocumentRoot \/var\/www\/html/DocumentRoot \/var\/www\/newsite/g" /etc/apache2/sites-available/newsite.conf sudo sed --in-place "/webmaster@newsite/ a ServerName newsite" /etc/apache2/sites-available/newsite.conf # Enable the new virtual host file sudo a2ensite newsite.conf # Modify hosts file sudo sed --in-place "\$a127\.0\.0\.1 newsite" /etc/hosts # Restart Apache sudo systemctl reload apache2 # Open the website xdg-open http://newsite 

Output:

enter image description here

You can also turn this into a shell script.

0

You can assign multiple aliases for 127.0.0.1 in /etc/hosts

127.0.0.1 localhost site1 site2 site3 

Configure appropriate VirtualHosts site1, site2, site3 in Apache configs and access them as http://site1/, http://site2/, http://site3/

2
  • you mean as ServerAlias site1 ? Commented Dec 19, 2021 at 7:42
  • Can you please elaborate? I'm very new at this... an example ? Commented Dec 19, 2021 at 8:10
0

When you create virtual sites, you will use a directive like ServerName inside the VirtualHost container, to distinguish that virtual site. Something like: <VirtualHost *:80> ServerName my.best.server.biz ....... The DNS name of my.best.server.biz should resolve to the ip of your apache server, which can be 127.0.0.1 in your case. Most likely for this you will add entries in your hosts file, like pointed in the previous answer.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.