CATEGORIES

TAGS

SSL

PostgreSQL

cPanel

email

Error

WHM

Linux

Bash

Console

Laravel

ARCHIVE

2019
2018

How to set up Apache virtual server on Debian system

17th December
Linux
How to set up Apache virtual server on Debian system

Before you will set up Apache virtual server you should complete some steps:

At first, I recommend you to make some coffee for you.

The second main thing that you need is a Debian server account with “sudo” privileges.

Then you need Apache installed on your system. If you want to know how to install Apache web server read this.

if you have done all these steps you should log in into your server. If you use the terminal of Debian system use the command “ssh” in Windows OS you can download “PuTTY” and use it for connecting to the server.

Step 1


Let’s create the directory structure.

You should create all your files and directories for your site into /var/www. Because it is the top-level directory which Apache search to find the content to serve. Let’s create the directory for our tutorial there. Use the command “mkdir” to create some directories like this:

Copied!

mkdir –pvar/www/domainofyourtestsite.com/public_html



but I want to create just one more directory:

Copied!

mkdir –pvar/www/domainofyourtestsite2.com/public_html



Are you interested in what is “-p”?

INFO: the command “mkdir” can create just one directory but if you want to create nested directories you should use “-p”.



Step 2


Permission.

The Irony of Fate!!! we have created the directories but we haven’t any permission to modify the files in it. The problem is that files are owned by the root user. The solution is to change the ownership of that files by using the command “chown” like this:

Copied!

sudo chown –R $USER:$USER /var/www/domainofyourtestsite.com/public_html



And

Copied!

sudo chown –R $USER:$USER /var/www/domainofyourtestsite2.com/public_html



Now let’s change your permissions to be sure that you can make changes into that files. Use the command “chmod”: there are several ways to do that I prefer the following way:

Copied!

sudo chmod 755 /var/www



step 3


creating demo page.

let’s create an index.html file for each of our domains (domainofyourtestsite.com and domainofyourtestsite2.com). In this tutorial, I will use “nano” to edit files. You may use your favorite text editor:

Copied!

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



in this file you need to create some html document like this:



Save and close that file.

INFO: to save your document with “nano” use “ctrl+o” then “ctrl+x” to exit.



you can use the command “nano” again and create the same document into “domainofyourtestsite2.com” or copy this content and make changes there like this:

Copied!

cp /var/www/domainofyourtestsite.com/public_html/index.html /var/www/domainofyourtestsite2.com/public_html/index.html



we have just copied the file, now let’s make some changes:

Copied!

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



then:



Save and close this file too. Now you have some pages to test your host configuration.

Step 4


Creating new files for virtual host.
In this step, we will create a directory by name “sites- available”. In which you need to add all your virtual host files. Apache comes with a default virtual host file by name 000-default.conf that you can use as a jumping-off point.

Copy this file for the first domain:

Copied!

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



open that file with the editor:

Copied!

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



Now copy this bunch of code there then save and close that file:



Let’s understand what it means.



these two lines are responsible for designating the content as a virtual host which is listening on port 80 (80 is the default HTTP port)



Via this two lines you say that your main server name is “www.domainofyourtestsite.com” and at the same time, you make an alias for the server to point “domainofyourtestsite.com” to. Now the request for “www.Domainofyourtestsite.com” and “domainofyourtestsite.com” will show the same content.



These three lines tell Apache where to store the error page and request logs for this particular site.

Now let’s copy this content for second virtual server and make corresponding changes for it. Use this command:

Copied!

sudo cp /etc/apache2/sites-available/domainofyourtestsite.com.conf /etc/apache2/sites-available/domainofyourtestsite2.com.conf



Open this file to with command “nano”:

Copied!

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



now you should modify that file like this:



After modifying the file save and close it.

Step 5


Enable the new virtual host files.

You have already created folders and Apache virtual host config files, but Apache will not use them because they aren’t active. Use the command “a2ensite” to activate them:

Copied!

sudo a2ensitedomainofyourtestsite.com.conf



If you see something like this all is well you can continue.

image

Now use the same command for the second site:

Copied!

sudo a2ensitedomainofyourtestsite2.com.conf



after this command you will see something similar like above:

image

Now you need to disable the default site defined in “000-default.conf” via “a2dissite” command like this:

Copied!

sudo a2dissite000-default.conf



Restart the Apache:

Copied!

sudo systemctlrestart apache2



Now if you use a real domain you can test your site but if you use your local machine or just try out this tutorial you need to do something more.

If you use Mac or Linux systems use this command:

Copied!

sudo nano/etc/osts



and:

Copied!

notpad%windir%\system32\drivers\etc\hosts



if you use Windows Command prompt with administrative privileges.

Add into this file the line that maps your server’s public IP address to each domain name like this:

Copied!

111.111.111.111domainofyourtestsite.com



Copied!

111.111.111.111domainofyourtestsite2.com



You need to do this because you need to direct any requests for domainofyourtestsite.com and domainofyourtestsite2.com on your local machine and send them to your server at 111.111.111.111 .
In this case, this site will be available only for your local machine.

step 6


Testing.

Now you can type in your browser your domain in our case the domain is “domainofyourtestsite.com”

And you will see something like this:

Success!!!!

We have done it.

That is all!!! Thanks for reading us!!!


SUBSCRIBE by email and never miss a post!