Score:-1

how to connect to one of multiple virtual sites (apache 2.4)

ye flag

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)?

user9517 avatar
cn flag
The technology you are looking for is called 'name based virtual hosting'. There are hundreds of Q&A about this on ServerFault.
Score:1
cn flag

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.

Score:0
ua flag

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.

Score:0
jp flag

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/

ye flag
you mean as ServerAlias site1 ?
ye flag
Can you please elaborate? I'm very new at this... an example ?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.