Score:0

How to run multiple web-sites on localhost with Apache

ga flag

Please help a newbie. I've recently installed LAMP on Ubuntu 22.04. Apache version: Apache/2.4.52

What I am trying to do is to enable multiple web-sites on my localhost. I’ve read and followed a few posts on this topic however I can’t make it work.

So this is what I’ve done so far to create two sites "prj1" and "prj2":

  1. Created two folders
sudo mkdir /var/www/prj1
sudo mkdir /var/www/prj2
  1. Created two index files in the folders:
sudo gedit /var/www/prj1/index.html
sudo gedit /var/www/prj2/index.html

They are simple, the first states:

Web prj 1. Location:

/var/www/prj1

The second:

Web prj 2. Location:

/var/www/prj2
  1. Set permissions:
sudo chown -R www-data:www-data /var/www
sudo chown -R 775 /var/www
sudo chown -R www-data:www-data /var/www/prj1
sudo chown -R www-data:www-data /var/www/prj2
  1. Created .conf files for each project: sudo gedit /etc/apache2/sites-available/prj1.conf
<VirtualHost *:80>
     ServerAdmin admin@prj1
     ServerName prj1
     
     DocumentRoot /var/www/prj1/
     <Directory />
        Options FollowSymLinks
        AllowOverride None
     </Directory>     
     <Directory /var/www/prj1/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/prj1.webdock.io_error.log
     CustomLog ${APACHE_LOG_DIR}/prj1.webdock.io_access.log combined
</VirtualHost>

sudo gedit /etc/apache2/sites-available/prj2.conf

<VirtualHost *:80>
     ServerAdmin admin@prj2
     ServerName prj2
     
     DocumentRoot /var/www/prj2/
     <Directory />
        Options FollowSymLinks
        AllowOverride None
     </Directory>     
     <Directory /var/www/prj2/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/prj2.webdock.io_error.log
     CustomLog ${APACHE_LOG_DIR}/prj2.webdock.io_access.log combined
</VirtualHost>
  1. Enabled sites:
a2ensite prj1
a2ensite prj2
  1. Edited apache2.conf: sudo gedit /etc/apache2/apache2.conf

I’ve added the following:

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

DocumentRoot "/var/www/prj1"
<Directory "/var/www/prj1">
            Options FollowSymLinks
            AllowOverride All
            Require all granted
</Directory>

DocumentRoot "/var/www/prj2"
<Directory "/var/www/prj2">
            Options FollowSymLinks
            AllowOverride All
            Require all granted
</Directory>
  1. Added two lines in /etc/hosts: sudo gedit /etc/hosts
127.0.0.1 localhost prj1
127.0.0.1 localhost prj2
  1. Restarted Apache:
systemctl restart apache2

Now I am using the following URI in the browser:

http://prj1.localhost/

this works as expected, it shows:

Web prj 1. Location:

/var/www/prj1

However if I use http://prj2.localhost/ it still shows the first site

Web prj 1. Location:

/var/www/prj1

What do I do wrong?

Thank you, Alex

waltinator avatar
it flag
In your step 3, "`sudo chown -R 775 /var/www`" doesn't make sense. Also, this seems to be about Apache, not Ubuntu.
cadol_askUbu avatar
ga flag
yeah, at first I didn't use the line, then I found it in one of the posts and tried. The whole thing didn't work in either case...
Score:1
ga flag

I've found what the problem was. In the prj2.conf the port for prj2 was 81 not 80. Honestly I don't know how that happened, I can't even say it was a typo since the whole text was copy/pasted. Anyways, after I changed it to 80 both sites are now working.

In the file /etc/host the lines should be

127.0.0.1 prj1

127.0.0.1 prj2

The URLs are to be http://prj1 and http://prj2. The only thing is that the browser tells that connection to the sites "Not secure" (I don't know why).

I thank all the contributors to the post.

Regards, Alex

raj avatar
cn flag
raj
Good that you solved it. The browser says "not secure" because the connection is over plain HTTP and not HTTPS. That's what modern browsers do if they encounter a plain HTTP connection. Nothing to worry about.
cadol_askUbu avatar
ga flag
Thank you for this clarification Raj!
Score:0
nc flag

Step 7 should be:

127.0.0.1 prj1.localhost
127.0.0.1 prj2.localhost
cadol_askUbu avatar
ga flag
Hi Parto, thank you for your suggestion. Just tried, no luck...
Score:0
cn flag
raj

According to documentation, /etc/hosts file needs to have one line per IP address. So you should put all hostnames that refer to 127.0.0.1 in one line, like:

127.0.0.1 localhost prj1 prj2

Also I'm not sure why you are using plain hostnames prj1 and prj2 both in your hosts file and in ServerName directives in Apache config, but are using prj1.localhost and prj2.localhost in the URLs. I would rather try just http://prj1/ and http://prj2/.

cadol_askUbu avatar
ga flag
Hi Raj, thank you for stepping in. I tried putting it into one line - still no result. As to URLs, honestly, I don't know what URL is correct, I tried a few combinations including the one you suggested. The only difference when using prj2 instead of prj2.localhost is that the browser warns that the connection is not secure
raj avatar
cn flag
raj
It would be nice to know what request is exactly sent by your browser. Maybe it messes something up. Can you get it using Developer tools window in your browser? You can also try to manually connect via telnet to port 80 and simulate the HTTP request, sending hostname `prj1` or `prj2` - if you get the correct site then server part is working properly, and the issue is definitely on the client (browser) side - in that case it may be impossible to make it work properly
I sit in a Tesla and translated this thread with Ai:

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.