Apache Virtual Hosts allow you to host multiple websites on a single Apache server. This guide will show you how to set up virtual hosts on Arch Linux.

1. Install Apache

First, ensure that Apache is installed on your system:

sudo pacman -Syu apache

2. Enable and Start Apache

Enable and start the Apache service:

sudo systemctl enable httpd
sudo systemctl start httpd

3. Configure Virtual Hosts

Edit the Apache configuration file to set up your virtual hosts. Create a new configuration file for your virtual hosts, or edit the default configuration file:

sudo nano /etc/httpd/conf/extra/httpd-vhosts.conf

Add your virtual host configurations. For example:



    ServerAdmin [email protected]
    DocumentRoot /srv/http/example1
    ServerName example1.com
    ServerAlias www.example1.com
    ErrorLog /var/log/httpd/example1_error.log
    CustomLog /var/log/httpd/example1_access.log combined



    ServerAdmin [email protected]
    DocumentRoot /srv/http/example2
    ServerName example2.com
    ServerAlias www.example2.com
    ErrorLog /var/log/httpd/example2_error.log
    CustomLog /var/log/httpd/example2_access.log combined

    

Save and close the file (press Ctrl+X, then Y, and Enter).

4. Include Virtual Hosts Configuration

Ensure that Apache includes the virtual hosts configuration file. Edit the main Apache configuration file:

sudo nano /etc/httpd/conf/httpd.conf

Find the line that includes the virtual hosts configuration file and make sure it is uncommented:


Include conf/extra/httpd-vhosts.conf
    

Save and close the file.

5. Create Document Roots

Create the directories for your virtual host document roots:

sudo mkdir -p /srv/http/example1
sudo mkdir -p /srv/http/example2

Set appropriate permissions for these directories:

sudo chown -R http:http /srv/http/example1
sudo chown -R http:http /srv/http/example2

6. Test Apache Configuration

Test your Apache configuration for syntax errors:

sudo httpd -t

If there are no errors, restart Apache to apply the changes:

sudo systemctl restart httpd

7. Configure DNS (Optional)

If you are using domain names, ensure that DNS records for example1.com and example2.com point to your server's IP address. You can test using the /etc/hosts file for local testing:

sudo nano /etc/hosts

Add entries like:


127.0.0.1 example1.com
127.0.0.1 example2.com
    

Save and close the file.

8. Verify Virtual Hosts

Open a web browser and navigate to http://example1.com and http://example2.com to verify that your virtual hosts are working correctly.

9. Troubleshooting

If you encounter issues, check the Apache error logs for more details:

sudo tail -f /var/log/httpd/error_log

10. Further Configuration

For more advanced configurations, refer to the Apache Virtual Hosts documentation.

Was this answer helpful? 0 Users Found This Useful (0 Votes)