How to Host Multiple Websites on One Server
With a growing business, you may want to expand & add new websites. It is economical if you use a single server for all your domains.
Virtual hosting allows you to host many domains on one server. A server may have extensive resources like HDD space, CPU, RAM, and so on.
You can use the same server resources for different sites.
It lets you host multiple websites on a single web server instance. Define it in a conf file with the URL & VPS IP addresses.
After a request is made, it will serve the traffic from the Document Root
.
Steps to Host Multiple Websites on One Server -Apache
1. Create the Directory Structure
The directory structure stores the site data. It is then served to the visitors.
The Document root
is the top-level directory. Apache searches content from document root to serve it to visitors.
The document root is set to individual directories /var/www. You can create a directory for each website you want to host.
Within each of these directories, create a public_html
folder.
It will store the actual site files for more flexibility.
In this step, create the /var/www folder, which will be the document root location. Sub the domain names such as domain.com
and domain2.com
mkdir -p /var/www/domain.com/public_html
mkdir -p /var/www/domain2.com/public_html
2. Set Up Permissions
After creating the directories, grant the proper permissions. The root user may own the directories.
A regular user should be able to modify the files in the web directories. To change the permissions, allow the following rules:
chmod -R 755 /var/www
3. Set up an Index Page
To see some content, you can create a demo index page. Make an index.html file for each valid domain for various websites.
vim /var/www/domain.com/public_html/index.html
Within the file, you can set something such as- example testing for domain.com
.
Example testing for domain.com
Save and close the file to exit by using the ESC tab & typing:wp
Repeat the steps for the second domain. You can use the following command:
vim /var/www/domain2.com/public_html/index.html
4. Copy the Config File for Each Site
Apache offers a default virtual host file called the 000-default.conf
You have to copy it to create a virtual host file for each website. It ensures that you have a default copy for websites.
cp/etc/apache2/sites-available/000-default.conf/etc/apache2/sites-available/domain.com.conf
cp/etc/apache2/sites-available/000-default.conf/etc/apache2/sites-available/domain2.com.conf
5. Edit the Config File for Each Site
You can modify the items in the first domain. You can also add more directives.
Add two directives such as ServerName
. It helps establish the base domain.
The second is Server Alias
, which is an alternative name to the base domain.
It helps match the hosts you defined, such as www
:
ServerName: domain.com
Server Alias: www.domain.com
vim /etc/apache2/sites-available/domain.com.conf
After completion, the Apache virtual host file would look like this:
ServerAdmin admin@example.com
ServerName domain.com
Server Alias www.domain.com
DocumentRoot /var/www/domain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save changes & exit with :wq. You then repeat the steps for the domain2.com.conf file.
Add the ServerName
, Server Alias
, and DocumentRoot
for your second domain.
6. Enable the Config File
You can now enable the virtual host configuration files with the following steps.
Firstly, you disable the default site defined in 000-default.conf
:
a2dissite 000-default.conf
Apache includes tools to enable the new virtual host files. The a2ensite docs help to enable the new .conf files.
a2ensite domain.com.conf
a2ensite domain2.com.conf
Restart the configured Apache service to see the changes made. You can use the systemctl status to verify the changes.
systemctl restart apache2
7. Test / Verify Apache Configurations
As you restart Apache, you check if the configurations are working.
You can do that by editing the /etc/host
file or by editing the domain.
The browser should show the index.html
pages that are set up in the previous steps.
If both sites are working correctly, you have completed the steps. You have now configured two virtual hosts on the same server.
Steps to Host Multiple Websites on One Server -NGINX
The steps are similar for the NGINX server. We will show you the steps to configure two virtual hosts on the same server to modify nginx configuration for multisite.
1. Create Document Root Directories
You will make two individual document root folders for each domain.
$ sudo mkdir /var/www/html/example1.com
$ sudo mkdir /var/www/html/example2.com
The requests made to the first example domain are served from /var/www/html/example1.com
.
The requests made, for example2 site will be served from /var/www/html/example2.com
2. Create index.html files
For the next step, create index.html files for each of the domains. Starting with the example1 website, add the following command.
$ sudo vi /var/www/html/example1.com/index.html
<title>www.example1.com</title>
<h1>Welcome to www.example1.com Website</h1>
</html>
Save and close the file to save the changes.
Repeat the same steps, for example2.com
website.
3. Open NGINX Configuration file
Open the NGINX configuration file at /etc/nginx/nginx.conf
You can do it using this command:
$ sudo vi /etc/nginx/nginx.conf
4. Create Server Blocks
Create two server blocks for each domain.
You can modify the values for server_name
and root. It lets you add specific domain names. Also, add the document root locations for each domain.
listen 80;
root /var/www/html/example1.com;
index index.html;
server_name example1.com;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
root /var/www/html/example2.com;
index index.html;
server_name example2.com;
location / {
try_files $uri $uri/ =404;
}
5. Restart the NGINX Server
Restart the NGINX server to apply the changes. Use the following command:
$ sudo nginx -t
$ sudo systemctl restart nginx
Check the web browser & type example1.com
to see the index page. Test the same changes, for example2.com
domain.
You have configured two virtual hosts on one server using NGINX.
Conclusion
With virtual hosting, you can use server resources efficiently and get the best hosting for multiple websites. The VPS server provides a combined virtualhost for websites and multi domain hosting.
You can host an unlimited number of websites for your business on one server. It will serve the requests from the defined URLs.
The tutorial covered steps on hosting two websites. You can do these using Apache or NGINX.
With these steps, you can successfully set up the sites on one server. Ensure that the server has enough resources for both domains.
You can manage the server resources using CloudPanel. It is a free server control panel. To learn more about web hosting & server management, check out the CloudPanel blog.