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 <code>http</code> 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.
Update your packages using <code>yum</code>:
sudo yum update
Install Apache:
sudo yum install httpd
Start up Apache, so that the httpd service will start automatically on a reboot:
sudo service httpd start
Create the virtual directories for your domain:
sudo mkdir -p /var/www/coolexample.com/public_html
Change the ownership to the Apache group:
sudo chown -R apache:apache /var/www/coolexample.com/public_html
This lets Apache modify files in your web directories.
Change the directory's permissions so they can be read from the internet:
sudo chmod -R 755 /var/www/
If you have the content for the website prepped, you can upload it to the <code>/public_html</code>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).
Create the index file:
sudo vim /var/www/coolexample.com/public_html/index.html
Add some content to the file:
Save and close the file:
:wq!
We're going to copy a configuration usually used in Ubuntu/Debian and create two directories: one to store the virtual host files (<code>sites-available</code>) and another to hold symbolic links to virtual hosts that will be published (<code>sites-enabled</code>).
Create the directories:
sudo mkdir /etc/httpd/sites-available
sudo mkdir /etc/httpd/sites-enabled
Edit the main configuration file (<code>httpd.conf</code>) so that Apache will look for virtual hosts in the <code>sites-enabled</code> directory.
Open your config file:
sudo vim /etc/httpd/conf/httpd.conf
Add this line at the very end of the file:
IncludeOptional sites-enabled/*.conf
This way, we're telling Apache to look for additional config files in the <code>sites-enabled</code> directory.
We're going to build it from a new file in your <code>sites-available</code> directory.
Create a new config file:
sudo vim /etc/httpd/sites-available/coolexample.com.conf
Paste this code in, replacing your own domain for coolexample.com.conf.
Here's what the whole file could look like after your changes:
The lines <code>ErrorLog</code> and <code>CustomLog</code> are not required to set up your virtual host, but we've included them, in case you do want to tell Apache where to keep error and request logs for your site.
Enable your virtual host file with a sym link to the <code>sites-enabled</code> directory:
sudo ln -s /etc/httpd/sites-available/coolexample.com.conf /etc/httpd/sites-enabled/coolexample.com.conf
Restart Apache:
sudo service httpd restart
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
<a href="https://sg.godaddy.com/zh/help/point-your-domain-name-to-a-server-19116">Point your domain name to a server</a>
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!
To create additional sites, repeat the following sections:
Set up the virtual host
Create content for the website
Create virtual host file — but for additional virtual hosts, you will need to create new config files in <code>/etc/httpd/sites-available/</code>, for example:
/etc/httpd/sites-available/your second domain name
Point your domain name to your server