How to Configure Apache Virtual Hosts on Ubuntu 20.04?

1. Create a Directory Structure: Start by creating a directory structure to store your website files. For example: sudo mkdir -p /var/www/mywebsite.com/public_html 2. Assign Permissions: Set the appropriate permissions for the directories: sudo chown -R www-data:www-data /var/www/mywebsite.com sudo chmod -R 755 /var/www 3. Create an HTML File: Create a simple HTML file to test the virtual host: echo "

Hello World!

" | sudo tee /var/www/mywebsite.com/public_html/index.html 4. Create a Virtual Host Configuration File: Create a new virtual host configuration file in the `/etc/apache2/sites-available/` directory. You can use a text editor like `nano` or `vim`: sudo nano /etc/apache2/sites-available/mywebsite.com.conf Add the following configuration: 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 Save and exit the text editor. 5. Enable the Virtual Host: Enable the virtual host configuration and restart Apache: sudo a2ensite mywebsite.com.conf sudo systemctl restart apache2 6. Update Hosts File (Optional): If you're testing on your local machine, you might want to add 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: Open your web browser and navigate to `http://mywebsite.com`. You should see the "Hello World!" message. That's it! You've successfully configured an Apache virtual host on Ubuntu 20.04. Adjust the configurations according to your specific needs.


Was this article helpful?

mood_bad Dislike 1
mood Like 1
visibility Views: 7980