Apache HTTP, a popular open-source web server, employs modular flexibility. With virtual hosts, one instance manages multiple sites discreetly.
To set up Apache virtual hosts on Ubuntu 20.04, follow these steps:
1. Create a Directory Structure:
Begin by setting up a directory structure for organizing your website files. For instance:
sudo mkdir -p /var/www/mywebsite.com/public_html
2. Assign Permissions:
Set the appropriate permissions for the directories using the following commands:
sudo chown -R www-data:www-data /var/www/mywebsite.com
sudo chmod -R 755 /var/www
3. Create an HTML File:
Generate a simple HTML file for testing the virtual host:
echo "<html><head><title>Welcome to My Website</title></head><body><h1>Hello World!</h1></body></html>" | sudo tee /var/www/mywebsite.com/public_html/index.html
4. Create a Virtual Host Configuration File:
Craft a virtual host configuration file in the `/etc/apache2/sites-available/` directory using text editor like ‘nano ‘or ‘vim’:
sudo nano /etc/apache2/sites-available/mywebsite.com.conf
Add the following configuration:
<VirtualHost *:80>
ServerAdmin webmaster@mywebsite.com
ServerName mywebsite.com
DocumentRoot /var/www/mywebsite.com/public_html
ErrorLog ${APACHE_LOG_DIR}/mywebsite.com_error.log
CustomLog ${APACHE_LOG_DIR}/mywebsite.com_access.log combined
</VirtualHost>
Save and exit the text editor.
5. Enable the Virtual Host:
Enable the virtual host setup and reboot Apache.
sudo a2ensite mywebsite.com.conf
sudo systemctl restart apache2
6. Update Hosts File (Optional):
If you are conducting tests on your local machine, consider adding an entry to your hosts file.
sudo nano /etc/hosts
Add a line like:
127.0.0.1 mywebsite.com
Save and exit.
7. Test the Virtual Host:
Test the virtual host by opening your web browser and navigating to ‘http://mywebsite.com’. You should see the "Hello World!" message.
Well done! You have successfully configured an Apache virtual host on Ubuntu 20.04. Adjust the configurations as per your specific requirements.