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":
- Created two folders
sudo mkdir /var/www/prj1
sudo mkdir /var/www/prj2
- 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
- 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
- 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>
- Enabled sites:
a2ensite prj1
a2ensite prj2
- 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>
- Added two lines in /etc/hosts:
sudo gedit /etc/hosts
127.0.0.1 localhost prj1
127.0.0.1 localhost prj2
- Restarted Apache:
systemctl restart apache2
Now I am using the following URL 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?
2
Answers
Step 1: Make a Directory for Each Site.
Step 2: Set Folder Permissions.
Step 3: Set up an Index Page.
Step 4: Copy the Config File for Each Site.
Step 5: Edit the Config File for Each Site.
Step 6: Enable Your Config File.
Step 7: Verify Apache Configurations.
If you are hosting more than one site on a server, then you most likely use Apache’s virtual host files to state which domain should be served out. Name based virtual hosts are one of the methods used to resolve site requests.
You’re configuring
and
and you’re accessing them as
http://prj1.localhost
(which does not find a matching virtual host, thus it takes the first one) andhttp://prj2.localhost
, which also does not find a matching one, and also defaults to the first one. Either configure the name that you use to access the hosts, or configure an alias, e.g.and