Arch Linux

How To Set Up Apache Virtual Hosts on Arch Linux

If you’re running a website or a web application, using virtual hosts can be a game changer. Virtual hosts allow you to host multiple websites on a single server by directing requests to different directories depending on the domain name. In this blog post, we’ll guide you through the process of setting up Apache virtual hosts on Arch Linux.

Prerequisites

Before we begin, ensure that you have:

  1. A server running Arch Linux.
  2. Apache installed and running. You can install Apache using the following command:
   sudo pacman -S apache
  1. Basic knowledge of the command line.
  2. Root or sudo access to your server.

Step 1: Enable the Necessary Apache Modules

Before setting up virtual hosts, it’s essential to make sure that the required Apache modules are enabled. The primary module needed for virtual hosting is mod_vhost_alias. You can enable it by running:

sudo a2enmod vhost_alias

(Note: On Arch Linux, modules are typically included and don’t require the a2enmod command. Just ensure your Apache configuration includes the necessary directives.)

Step 2: Create Directories for Your Websites

Assuming you want to set up two websites, example1.com and example2.com, you need to create directories for these sites. For example:

sudo mkdir -p /var/www/example1.com/public_html
sudo mkdir -p /var/www/example2.com/public_html

Step 3: Set Proper Permissions

After creating the directories, set the proper ownership and permissions, so Apache can serve your websites correctly:

sudo chown -R http:http /var/www/example1.com/public_html
sudo chown -R http:http /var/www/example2.com/public_html

sudo chmod -R 755 /var/www

Step 4: Create Sample Index Pages

Next, create a sample index.html for each of your websites. This will help you verify that the virtual hosts are working correctly:

For example1.com:

echo "<h1>Welcome to Example1.com!</h1>" | sudo tee /var/www/example1.com/public_html/index.html

For example2.com:

echo "<h1>Welcome to Example2.com!</h1>" | sudo tee /var/www/example2.com/public_html/index.html

Step 5: Configure Apache Virtual Hosts

Now, let’s configure Apache to recognize these two sites. You’ll need to create a new configuration file for each virtual host in /etc/httpd/conf/extra/httpd-vhosts.conf.

Open the file with your preferred text editor:

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

And add the following configuration for each virtual host:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/var/www/example1.com/public_html"
    ServerName example1.com
    ServerAlias www.example1.com
    ErrorLog "/var/log/httpd/example1.com-error.log"
    CustomLog "/var/log/httpd/example1.com-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/var/www/example2.com/public_html"
    ServerName example2.com
    ServerAlias www.example2.com
    ErrorLog "/var/log/httpd/example2.com-error.log"
    CustomLog "/var/log/httpd/example2.com-access.log" common
</VirtualHost>

Step 6: Include the Virtual Host Configuration

Next, make sure Apache includes your virtual host configuration file. Open the main Apache configuration file:

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

Look for the line that reads:

#Include conf/extra/httpd-vhosts.conf

Uncomment it (remove the # at the beginning) to enable the virtual hosts configuration:

Include conf/extra/httpd-vhosts.conf

Step 7: Update Your Hosts File (For Testing Locally)

If you’re testing these sites locally, you’ll need to update your /etc/hosts file to point to your local server. Open the hosts file:

sudo nano /etc/hosts

And add the following lines:

127.0.0.1    example1.com
127.0.0.1    www.example1.com
127.0.0.1    example2.com
127.0.0.1    www.example2.com

Step 8: Restart Apache

Now that everything is set up, restart the Apache service to apply the changes:

sudo systemctl restart httpd

Step 9: Test Your Configuration

Open your web browser and type in your domain names. If everything is configured correctly, you should see the respective welcome pages for example1.com and example2.com.

Conclusion

Congratulations! You’ve successfully set up Apache virtual hosts on Arch Linux. This powerful feature allows you to manage multiple domains from a single server efficiently. With your new knowledge, you can continue to explore additional configurations and optimizations to tailor your server to your needs.

For more tips, tricks, and tutorials on web hosting, don’t forget to check out the Greenhost.Cloud blog.

Happy Hosting! 🌱