virtual host apache ubuntu 22.04

12

Jun

Create Virtual Host Apache 2 on Ubuntu 22.04

Expanding your web server to host multiple sites on Apache is a common need, especially during development or when managing several projects. Using Apache’s Virtual Host feature, you can easily set up multiple websites on a single server. In this guide, we’ll walk through the process of configuring a new virtual host on Apache for hosting a site called “mysite” on Ubuntu.

Create a Configuration File

Let’s create a new configuration file for our site “mysite”. Open a terminal and use your preferred text editor to create and edit the file:

sudo nano /etc/apache2/sites-available/mysite.conf

Paste the following configuration into the file:

<VirtualHost *:80>
    ServerAdmin gersonhernandez93@hotmail.com
    DocumentRoot /var/www/html/mysite/
    ServerName mysite.localhost
    ServerAlias mysite.localhost

    <Directory /var/www/html/mysite/>
       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

This configuration sets up a new virtual host listening on port 80 with the specified server name and document root.

Enable the Site

After creating the configuration file, we need to enable the site using the a2ensite command:

sudo a2ensite mysite

Restart Apache

To apply the changes, restart Apache:

sudo service apache2 restart

Test the Site

Open your web browser and navigate to http://mysite.localhost. If everything is configured correctly, you should see your website served from the specified document root.

Conclusion

By following these steps, you can easily set up and manage multiple sites on your Apache web server. Feel free to repeat these steps to add more virtual hosts for additional websites.

If you encounter any issues or have any questions, don’t hesitate to ask in the comments below. Happy hosting!

Leave a Reply

Your email address will not be published. Required fields are marked *

RELATED

Posts