Apache 2 sites on the same servers

We are going to use Virtual Hosts to have the sites on our server. we are going to explain very briefly the DNS for the domains and how the sites are stored in the public directory on apache.

In the context of web servers, a virtual host is a method of hosting multiple domains or websites on a single physical server. Virtual hosts allow you to serve content for different websites with different domain names or IP addresses, using a single server machine.

When a request comes to the server, the web server software checks the requested domain name and serves the content associated with that domain name. This is possible because each virtual host has its own set of configuration files that determine which files are served and how requests are handled.

Virtual hosts are commonly used in shared hosting environments, where multiple users share the same server resources. They can also be used in enterprise environments to host multiple applications or websites on a single server.

I will not explain how to setup the DNS records to point to the domains on your server you can look for how to setup DNS records on your registered domain site that you are using, however in short you have to put the records to the Ip Address where you are going to have your sites depending on the domain Apache configuration it will know to which site should go.

Prerequisites

Before you begin this tutorial, You will also need to have Apache installed in order to work through these steps. If you haven’t already done so, you can get Apache installed on your server through apt-get:

sudo apt-get update

sudo apt-get install apache2

After these steps are complete, we can get started. I am assuming you are using a Debian-based system however you can look for the specific commands on the OS that you use or the matching commands of it.

For the purposes of this guide, our configuration will make a virtual host for example.com and another for test.com. These will be referenced throughout the guide, but you should substitute your own domains or values while following along.

First Step — Create Directory Structure

Our document root (the top-level directory that Apache looks at to find content to serve) will be set to individual directories under the `/var/www` directory. We will create a directory here for both of the virtual hosts we plan on making.

Within each of these directories, we will create a `public_html` folder that will hold our actual files. This gives us some flexibility in our hosting.

Creating a public_html folder is a common practice in web hosting to separate the publicly accessible files from other system files on the server. This folder serves as the document root for the web server, which means that all web content that needs to be publicly accessible should be placed in this folder.

By placing your website’s files in the public_html folder, you ensure that they can be accessed by visitors to your website using their web browser. If you don't create a public_html folder, the web server may not be able to find the files needed to serve your website, and visitors won't be able to access your content.

Additionally, creating a public_html folder also helps to maintain security on your server, as it limits the files that are accessible to the public, reducing the risk of sensitive system files being accidentally exposed or compromised.

For instance, for our sites, we’re going to make our directories like this:

sudo mkdir -p /var/www/`example.com`/public_html

sudo mkdir -p /var/www/`test.com`/public_html

The portions in red represent the domain names that we are wanting to serve from our VPS.

Step Two — Grant Permissions

Now we have the directory structure for our files, but they are owned by our root user. If we want our regular user to be able to modify files in our web directories, we can change the ownership by doing this:

sudo chown -R $USER:$USER /var/www/example.com/public_html

sudo chown -R $USER:$USER /var/www/test.com/public_html

The chown and chmod commands are used in Linux/Unix-based operating systems to modify file and directory permissions.

The chown command changes the ownership of a file or directory. The ownership of a file or directory determines which user or group has control over it. By default, when a file or directory is created, it is owned by the user who created it. However, there may be situations where it is necessary to change ownership to another user or group. For example, if you have a website with multiple contributors, you may want to change ownership of certain files or directories to a group that all the contributors belong to, so that they can all edit those files.

The chmod command changes the file permissions, which determine who can access the file and what they can do with it. There are three types of permissions: read (r), write (w), and execute (x). Permissions can be set for three types of users: the owner of the file (u), members of the group that owns the file (g), and everyone else (o). The chmod command can be used to set permissions in different ways, such as setting all permissions at once, setting specific permissions for each user type, or using a numeric code to set permissions.

For example, the command chmod 755 file.txt sets the permissions for the file "file.txt" so that the owner can read, write, and execute it, while members of the group and everyone else can only read and execute it. The command chown user1:group1 file.txt changes the ownership of "file.txt" to "user1" and "group1".

It is important to be careful when using these commands, as changing permissions and ownership can potentially cause security vulnerabilities or prevent users from accessing necessary files. It is recommended to only modify permissions and ownership when it is necessary and to understand the consequences of the changes made.

The `$USER` variable will take the value of the user you are currently logged in as when you press Enter. By doing this, our regular user now owns the `public_html` subdirectories where we will be storing our content.

We should also modify our permissions a little bit to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages can be served correctly:

sudo chmod -R 755 /var/www

Your web server should now have the permissions it needs to serve content, and your user should be able to create content within the necessary folders.

Step Three — Create Demo Pages for Each Virtual host

We’re just going for a demonstration, so our pages will be very simple. We’re just going to make an index.html page for each site.

Let’s start with example.com. We can open up an index.html file in our editor by typing:

nano /var/www/example.com/public_html/index.html

In this file, create a simple HTML document that indicates the site it is connected to. My file looks like this:

/var/www/example.com/public_html/index.html

<html>

<head>

<title>Welcome to Example.com!</title>

</head>

<body>

<h1>Success! The example.com virtual host is working!</h1>

</body>

</html>

Save and close the file when you are finished.

We can copy this file to use as the basis for our second site by typing:

cp /var/www/example.com/public_html/index.html /var/www/test.com/public_html/index.html

We can then open the file and modify the relevant pieces of information:

nano /var/www/test.com/public_html/index.html

/var/www/test.com/public_html/index.html

<html>

<head>

<title>Welcome to Test.com!</title>

</head>

<body> <h1>Success! The test.com virtual host is working!</h1>

</body>

</html>

Save and close this file as well. You now have the pages necessary to test the virtual host configuration.

Step Four — Create New Virual Host files

Create the First Virtual Host File

Start by copying the file for the first domain:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

Open the new file in your editor with root privileges:

sudo nano /etc/apache2/sites-available/example.com.conf

The file will look something like this (I’ve removed the comments here to make the file more approachable):

/etc/apache2/sites-available/example.com.conf

<VirtualHost *:80>

ServerAdmin webmaster@localhost

DocumentRoot /var/www/html

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

As you can see, there’s not much here. We will customize the items here for our first domain and add some additional directives. This virtual host section matches any requests that are made on port 80, the default HTTP port.

First, we need to change the ServerAdmin directive to an email that the site administrator can receive emails through.

ServerAdmin admin@example.com

After this, we need to add two directives. The first, called ServerName, establishes the base domain that should match for this virtual host definition. This will most likely be your domain. The second, called ServerAlias, defines further names that should match as if they were the base name. This is useful for matching hosts you defined, like www:

ServerName example.com

ServerAlias www.example.com

The only other thing we need to change for a basic virtual host file is the location of the document root for this domain. We already created the directory we need, so we just need to alter the DocumentRoot directive to reflect the directory we created:

`DocumentRoot /var/www/example.com/public_html`

In total, our virtualhost file should look like this:

/etc/apache2/sites-available/example.com.conf

<VirtualHost *:80>

ServerAdmin admin@example.com

ServerName example.com

ServerAlias www.example.com

DocumentRoot /var/www/example.com/public_html

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Save and close the file.

Copy First Virtual Host and Customize for Second Domain

Now that we have our first virtual host file established, we can create our second one by copying that file and adjusting it as needed.

Start by copying it:

sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/test.com.conf

Open the new file with root privileges in your editor:

sudo nano /etc/apache2/sites-available/test.com.conf

You now need to modify all of the pieces of information to reference your second domain. When you are finished, it may look something like this:

/etc/apache2/sites-available/test.com.conf

<VirtualHost *:80>

ServerAdmin admin@test.com

ServerName test.com

ServerAlias www.test.com

DocumentRoot /var/www/test.com/public_html

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Save and close the file when you are finished.

Step Five — Enable the New Virtual Host Files

We can use the a2ensite tool to enable each of our sites like this:

sudo a2ensite example.com.conf

sudo a2ensite test.com.conf

Next, disable the default site defined in `000-default.conf`:

sudo a2dissite 000-default.conf

When you are finished, you need to restart Apache to make these changes take effect:

sudo systemctl restart apache2

In other documentation, you may also see an example using the service command:

sudo service apache2 restart

You need to add both sites to the a2ensite to work if you only add one the other should not be included when you run the restart

Step Six — Set Up Local Hosts

This will intercept any requests for the domains that you configured and point them to your VPS server, just as the DNS system would do if you were using registered domains. This will only work from your computer though and is simply useful for testing purposes.

Make sure you are operating on your local computer for these steps and not your VPS server. You will need to know the computer’s administrative password or otherwise be a member of the administrative group.

If you are on a Mac or Linux computer, edit your local file with administrative privileges by typing:

sudo nano /etc/hosts

If you are on a Windows machine, you can find instructions on altering your hosts file here.

The details that you need to add are the public IP address of your VPS server followed by the domain you want to use to reach that VPS.

For the domains that I used in this guide, assuming that my VPS IP address is 111.111.111.111, I could add the following lines to the bottom of my hosts file:

/etc/hosts

127.0.0.1 localhost

127.0.1.1 guest-desktop

111.111.111.111 example.com

111.111.111.111 test.com

This will direct any requests for example.com and test.com on our computer and send them to our server at 111.111.111.111. This is what we want if we are not actually the owners of these domains in order to test our virtual hosts.

Save and close the file.

This if you didn't read is saying that the domain needs to be registered so when is been looked outside of the server the computer looks at the local server, in this case, apache and shows the virtual host that you create for that domain.

I take most of the information from [here] but I removed some sections that I feel are not needed.

--

--

Francisco Daniel Salazar Aguirre

Full stack developer , that can be named jack of all trades master of none but oftentimes better than a master of one. Still looking for improving all trades.