After installing the LAMP web server, most users want to set up virtual hosts. Personally, I find it inconvenient to store projects in the /var/www/
directory. Here is the simplest way to configure virtual hosts in LAMP.
Step 1: Open the Terminal and navigate to the configuration directory
cd /etc/apache2/sites-available
This directory contains configuration files for virtual hosts.
Step 2: Create a new Virtual Host configuration file
Create a new file with the name of the virtual host. In this example, we create test.local
:
sudo vi test.local
Step 3: Add Configuration settings
In the editor, add the following minimal virtual host configuration:
<VirtualHost *:80>
ServerName test.local
DocumentRoot /home/USERNAME/www/test.local/
</VirtualHost>
The DocumentRoot
line defines the path where project files are stored. Replace USERNAME
with your actual username and ensure the directories exist.
Step 4: Edit the Hosts file
To ensure the browser resolves the virtual host correctly, edit the hosts file:
sudo vi /etc/hosts
Add the following line:
127.0.0.1 test.local
Step 5: Enable the Virtual Host
You can activate the virtual host in two ways:
- Use the built-in
a2ensite
tool:
sudo a2ensite test.local
- Manually create a symbolic link from
sites-available/
tosites-enabled/
:
cd /etc/apache2/sites-enabled
sudo ln -s /etc/apache2/sites-available/test.local 001-test.local
However, managing multiple hosts with manual symbolic links can become confusing. The a2ensite
tool is recommended for simplicity.
Step 6: Restart Apache
To apply the changes, restart Apache:
sudo /etc/init.d/apache2 reload
Step 7: Set File Permissions
Sometimes, the user does not have sufficient permissions to access files in the home directory. To fix this, adjust permissions:
sudo chmod -R 755 ~/www/test.local
Conclusion
The virtual host is now configured and accessible in a browser at test.local
.
Note: This is a minimal and quick virtual host setup method.