Table of Contents
Most likely, you utilize Apache’s virtual host files to specify which domain should be served out if you are hosting multiple websites on a single server. One way to handle site requests is through name-based virtual hosts. This implies that when a user visits your website, a request is sent to the server, which uses the domain name to decide which site’s files to provide. You can host numerous websites on a single server using the same IP by using this strategy. In this guide
Step 1: Make a Directory for Each Site
You’ll create a directory for each site that you’ll be hosting, within the /var/www folder. This location newly created location is also dubbed the document root location; you’ll need to set this path later in the configuration file. Sub the domain.com and domain2.com for your domain names.
mkdir -p /var/www/yoursite.com/public_html
mkdir -p /var/www/yoursite2.com/public_html
Step 2: Set Folder Permissions
chmod -R 755 /var/www
Step 3: Set up an Index Page
To see a home page you’ll want to make sure the index.html file is created for each domain. Something as simple as “testing for yoursite.com” can be set within this file.
vim /var/www/domain.com/public_html/index.html
testing for domain.com
Save and quit by hitting the Escape button and typing :wq
Repeat the steps for your second domain, using the command below.
vim /var/www/domain2.com/public_html/index.html
Step 4: Copy the Config File for Each Site
Copy the default configuration file for each site, this will also ensure that you always have a default copy for future site creation.
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/yoursite.com.conf
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/yoursite2.com.conf
Step 5: Edit the Config File for Each Site
At the bare minimum, you’ll adjust and add the highlighted lines within the <VirtualHost *:80> and </VirtualHost> tags.
vim /etc/apache2/sites-available/domain.com.conf
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName yoursite.com
ServerAlias www.yoursite.com
DocumentRoot /var/www/yoursite.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Quit and Save with :wq. Repeat this process for your domain2.com.conf file, be sure to update your ServerName, ServerAlias and DocumentRoot for your second domain. This is all part of the setup for you Apache server name or names.
Step 6: Enable Your Config File
Out of the box, your server (your Apache server name) is set to read the default 000-default.conf file. But, in our previous step we made a new config file for each domain. So, we will need to disable the default file.
a2dissite 000-default.conf
To have your server mapped to your domains you’ll need to enable each of your newly made .conf files.
a2ensite yoursite.com.conf
a2ensite yoursite2.com.conf
We restart the Apache service to register our changes.
sudo systemctl restart apache2
Step 7: Verify Apache Configurations for your Apache Server Name
After starting Apache (your specific Apache server name) you now can view that the configurations are working by either editing your /etc/host file on your computer or by editing your domain’s DNS.
After either one of these aspects are set, you’ll be able to visit your website in a browser to see the index.html pages set in Step 3. Congratulations on learning how to host multiple sites on one server!
it’s Working Thanks For Knowledge
I’m not sure where you’re getting your information, but
great topic. I needs to spend some time learning much more or understanding more.
Thanks for great information I was looking for this information for my mission.
Simply want to say your article is as astounding. The clearness to your submit is simply spectacular and
that i could assume you’re a professional in this subject.
Well together with your permission let me to grab your
feed to stay updated with imminent post. Thank you a million and please continue the rewarding work.
I every time emailed this weblog post page to all my associates.
Every weekend i used to visit this web page, because
i wish for enjoyment, for the reason that this this web page conations really nice funny material too.
Magnificent web site. A lot of useful information here. I’m sending it to a few friends ans also sharing in delicious. And naturally, thanks for your sweat!