Configure Apache Virtual Hosts - Arch Linux

Difficulty: 2
Time: 15 minutes

Want to host websites on your server? Using Apache? Great. This article will show you how to do exactly that using Apache’s “virtual hosts.”

In Apache, you can use virtual hosts to direct http traffic for a given domain name to a particular directory (i.e. the root directory of the website for the domain in the request). This feature is commonly used to host multiple websites, but we recommend using it for every website on your server including the first.

Throughout this article, we'll use an example domain - coolexample.com - but you should replace it with the domain name or subdomain you want to host on your server.

Install the Apache web server

To get Apache on your server, you can either install it as part of a LAMP stack, or you can install Apache by itself:

  • Install Apache using pacman:
    sudo pacman -Syu apache

Set up the virtual host

  • Create the virtual directories for your domain:
    sudo mkdir -p /srv/http/coolexample.com

Create content for the website

If you have the content for the website prepped, you can upload it to the /public_html folder you created in the last section.

If you don't have content ready to upload, you can create a sample home page (also known as an index file, which is the first page that loads when visitors come to your domain).

  1. Create the index file:
    sudo vim /var/www/coolexample.com/public_html/index.html
  2. Add some content to the file:
    <html> 
    <head> 
    <title>Welcome to my site!</title> 
    </head> 
    <body> 
    <h1>Hooray! Your virtual host is working!</h1> 
    </body> 
    </html>
    
  3. Save and close the file:
    :wq!

Enable virtual hosts

Enable virtual hosts by making a quick change in the Apache config file.

  1. Open the httpd.conf file to edit:
    sudo vim /etc/httpd/conf/httpd.conf
  2. Find this line and uncomment it:
    Include conf/extra/httpd-vhosts.conf
  3. Save and close the file:
    :wq!

Configure your virtual host

  1. Open the virtual hosts file:
    sudo vim /etc/httpd/conf/extra/httpd-vhosts.conf
  2. In the file, replace the first virtual host example with your own domain information. DocumentRoot, ServerName, and ServerAlias are the lines that must be updated:
    
        ServerAdmin your email address<
        DocumentRoot "/srv/http/coolexample.com" 
        ServerName www.coolexample.com     
        ServerAlias coolexample.com 
        ErrorLog "/srv/http/coolexample.com/error.log" 
        CustomLog "/srv/http/coolexample.com/requests.log" common
    
      
    Updating the other lines ServerAdmin, ErrorLog, and CustomLog are not required to set up your virtual host, but you can go ahead and update ErrorLog and CustomLog to your domain and ServerAdmin to your email.
  3. Save and close the file:
    :wq!
  4. Run a config test to check your changes:
    apachectl configtest
    You're looking for a Syntax OK message. If you got an error, go back to your config file and check your changes. Make sure your DocumentRoot entry is in quotes.

Put your domain name in the hosts file

If you're using fully qualified domain names (FQDNs), define your domain name in the /etc/hosts file.

  1. Open the file:
    sudo vim /etc/hosts
  2. Fill in your domain information for the first IP. Your changes might look something like this:
    #      
    127.0.0.1         coolexample.com      your host name (this is optional)
    ::1             localhost.localdomain   localhost
  3. Restart Apache:
    sudo systemctl restart httpd

Point your domain name to your server

If your domain name isn't currently loading another website, you should point it to your server to test your new config.

How you do this depends on where your domain name is registered and whose server you're using:

Domain registered? Server hosted? Do this...
GoDaddy GoDaddy Point your domain name to a server
Another company GoDaddy Find a server's public IP address and then update your domain name's primary ("@") A record.
GoDaddy Another company Find your server's IP address, and then change your domain's IP address to use it.
Another company Another company Find your server's IP address, and then change your domain's IP address to use it.

Changes to your domain can take up to 48 hours to display across the internet. However, once they do, you can visit your domain name and view the test page you created earlier!

Adding additional virtual hosts

To create additional sites, repeat the following sections:

  1. Set up the virtual host
  2. Create content for the website
  3. Configure your virtual host — but for additional virtual hosts, you will need to create new Virtual Host *:80 sections.
  4. Point your domain name to your server

For your reference, here's an example of what our virtual host config file (/etc/httpd/conf/extra/httpd-vhosts.conf) looks like after adding coolexample.net as a second virtual host:

<VirtualHost *:80>
    ServerAdmin your email address
    DocumentRoot "/srv/http/coolexample.com" 
    ServerName www.coolexample.com    
    ServerAlias coolexample.com
    ErrorLog "/srv/http/coolexample.com/error.log" 
    CustomLog "/srv/http/coolexample.com/requests.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin your email address
    DocumentRoot "/srv/http/coolexample.net" 
    ServerName www.coolexample.net   
    ServerAlias coolexample.net 
    ErrorLog "/srv/http/coolexample.net/error.log" 
    CustomLog "/srv/http/coolexample.net/requests.log" common
</VirtualHost>

Next steps

With Apache set up to work with your domain name, you can now do things like set up things like WordPress or Drupal.


Bài này có hữu ích không?
Cảm ơn về phản hồi của bạn. Để nói chuyện với một đại diện của dịch vụ khách hàng, vui lòng sử dụng số điện thoại hỗ trợ hoặc tùy chọn trò chuyện ở trên.
Rất vui vì chúng tôi đã giúp được bạn! Chúng tôi có thể làm gì thêm cho bạn?
Rất tiếc về điều đó. Vui lòng cho chúng tôi biết điều gì làm cho bạn bối rối và vì sao giải pháp không khắc phục được vấn đề này.