This article follows up on my previous guide on installing Apache Web Server on CentOS 7. In this guide, I'll show you how to set up virtual hosts (vHosts) in the Apache web server.
Note: CentOS 7 reached end of life in June 2024. The same steps work on its successors, such as AlmaLinux and Rocky Linux, which also use the httpd package and the same file paths.
Prerequisites
Before we get started, you'll need:
- A server running the Apache HTTP Server. I'm using a dedicated server from Spin Servers.
- A domain name pointed at your server's IP address.
- A cup of coffee or hot tea.
Step 1: Create the directories for your website
Hosting a single website on your Apache server is easy: just upload your website's files to the /var/www/html folder. Simple enough. But what if you want to host multiple websites? The answer is vHosts. Each website gets its own vHost, which tells the web server where that site's files live and how to serve it.
For every new website, start by creating a directory for its files. Replace example.com with your domain here and throughout this guide:
sudo mkdir -p /var/www/example.com/html
Next, create a folder to store the site's log files:
sudo mkdir -p /var/www/example.com/log
Give your user ownership of the website's files so you can edit them without sudo:
sudo chown -R $USER:$USER /var/www/example.com/html
Make sure the web root is readable by everyone, including Apache:
sudo chmod -R 755 /var/www
Step 2: Create a sample page
Now that the folders are set up, let's create a sample page for the vHost. Using a text editor of your choice (we'll use vi), create an index.html file:
vi /var/www/example.com/html/index.html
Once the file opens, press i to enter INSERT mode, then paste in this code:
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This vHost is successfully working! Sweet.</h1>
</body>
</html>
Save and exit by pressing Esc, typing :wq and pressing Enter.
Step 3: Create the vHost
Now create the sites-available and sites-enabled directories. sites-available stores all of your vHost files, while sites-enabled holds links to the vHosts that are active.
Create both directories with this command:
sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
Next, tell Apache to look for vHosts in the sites-enabled directory. Open Apache's main configuration file:
sudo vi /etc/httpd/conf/httpd.conf
Add this line to the end of the file:
IncludeOptional sites-enabled/*.conf
Save and exit.
Now create a vHost file for your website in the sites-available directory:
sudo vi /etc/httpd/sites-available/example.com.conf
Add the following, again replacing example.com with your domain:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/example.com/html
ErrorLog /var/www/example.com/log/error.log
CustomLog /var/www/example.com/log/requests.log combined
</VirtualHost>
Save and exit the file.
Now enable the vHost by linking it into the sites-enabled directory:
sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
Step 4: Allow Apache to write the log files (SELinux)
CentOS has SELinux enabled by default, and it won't let Apache write log files to a custom folder such as /var/www/example.com/log. If you skip this step, Apache may fail to start. Give the log folder the correct SELinux label:
sudo semanage fcontext -a -t httpd_log_t "/var/www/example.com/log(/.*)?"
sudo restorecon -R -v /var/www/example.com/log
If the semanage command isn't found, install it with sudo yum install policycoreutils-python (on AlmaLinux and Rocky Linux, the package is policycoreutils-python-utils).
Step 5: Restart Apache and test
Before restarting, check your configuration for errors:
sudo apachectl configtest
If you see Syntax OK, restart Apache:
sudo systemctl restart httpd
Your virtual host is now configured and ready to serve. Head over to http://example.com (using your own domain) and you should see the sample page we created. To host more websites, repeat these steps for each domain.
The server behind this guide
To make these how-to guides possible, I use a dedicated server from Spin Servers. My configuration is:
- Dual Intel Xeon E5-2630L (12 cores at 2.00 GHz)
- 128 GB DDR3 RAM
- 4 × 2 TB HDD in hardware RAID-10
- 10 TB bandwidth on a 10Gbps port
- Dallas, TX location
I get this configuration for $89/month with the code DUALE5FOR89, from the Spin Servers promotion on Web Hosting Talk.
If you have questions about any of our services, please contact us via email at [email protected] or by using our live chat at spinservers.com.
Spin Servers is a US-based provider of dedicated servers & affordable private cloud solutions. Our motto is to let you "spin up servers" fast, seamlessly, and affordably.
