If you want to host more than one domain on an Apache Web server that has a single IP address, then 1
you have to configure something called virtual hosting. By default, Apache on Ubuntu serves files 2
to the clients from a directory called "document root" that is by default in /var/www 3
/htm This is document root. 4
So if you have a website copy it to that directory and apache will serve it to the HTTP clients. 5
To show you this, I'll create a file there and get it from Apache. 6
So I'm creating a simple file called me.txt that contains a string so:echo "Hello, that's me! " > 7
and I'm redirecting it to a file in that directory so /var 8
/www/html/me.txt and from the client, using the browser, I'm requesting the file. 9
crystalmind.academy/me .txt and it's working. 10
This works well if you host only one domain. 11
So if you have only one website; but if you want to host more websites you need a document root 12
directory that contains a different website for each domain. 13
So this is what virtual hosting means, having more Web sites on a single server. 14
Let's get started. 15
I'm creating a new director for the first website, mkdir /var/www and the name of the website, 16
for example 17
/crystalmind.academy 18
In fact, I have only one domain, so only one website, but if you have more domains, create such 19
a directory for each of them. 20
Next, I'll settle the ownership of that directly to the user under which Apache runs. 21
Let's find out the user under which Apache runs. 22
ps -ef | grep apache2 Apache2 is the process that is running. 23
And we see that by default, Appache runs as www-data user and a group with the same name, so 24
chown -R I'm changing the ownership recursively www-data, the user, the owner and 25
the group .www-data the directory /var/www/crystalmind.academy/ this is necessary if 26
you want the web server to have full access to that directory. 27
I'll also set the permissions to save 755 for that directory chmod 755 and the name 28
of the directory and the path to that directory. 29
Now I'll create a simple index file there. 30
I'm using vim as the editor, you can use any other editor: index .html 31
I'm writing just a simple text to test that the server gives us the right file. 32
Hello, this is crystalmind.academy running in a virtual host. 33
And I'm saving the fight. The next step is to configure Apache to serve this content when someone 34
is accessing the domain and it's necessary to create a new virtual host file for Apache. 35
And I'm moving to /etc/apache2 This is the contents of the directory and I'm going to create a 36
new file in sites-available 37
So sites-available 38
crystalmind_academy 39
.conf 40
And I'm writing. 41
VirtualHost *:80 42
This is the port on which it listens and I'm closing the virtual host container, it's like in html 43
and in the virtual host container, I'm writing Server name crystalmind.academy 44
And this is the name of our domain. 45
ServerAlias 46
www.crystalmind.academy this specifies other names that can be used to access the website. 47
And document root the directory where the site resides so /var/ www.crystalmind. 48
academy the directory I've just created; these directives are mandatory. 49
It basically means: if someone accesses the server using crystalmind.academy or www.crystal 50
mind.academy, then give the user the website from /var/www/crystalmind.academy directory 51
Optionally add an email address for the server admin, so ServerAdmin 52
webmaster@/crystalmind.academy; Of course, this email address should exist; and some custom 53
log files for this domain. 54
ErrorLog /var/log/crystalmind_academy_error.log 55
And CustomLog 56
var/logapache 2/crystalmind_academy_error.access.log combined There are lots 57
of details and directives, but we won't dive deeper into them. 58
I'm saving and closing the file. 59
The virtual host was created, but not enabled; to enable it run a2ensite 60
It comes from Apache2 enable site and the name of the virtual host file without the ending. 61
conf. 62
So a2ensite_crystalmind.academy 63
OK, the name of the file is crystalmind_academy 64
I can rename it or use this name. 65
I'm renaming the virtual host file. 66
This is the virtual host file, and the comment will be a2enable site crystalmind.academy 67
and the virtual host was enabled. 68
In fact, this tool has just created a sym link to the virtual host file in a specific directory, which 69
will be loaded by the server. 70
Let me show you this so that you can get familiarized with important Apache directories and files. 71
In /etc/apache2 there are two directories for virtual hosts: sites-available and sites -enabled. 72
Apache will load all the files from sites-enabled, not from sites-available. 73
So if you want to enable a virtual host, which means to load a host file into Apache, then create 74
a symlink in sites-enabled/ to that file in sites-available/. 75
And this is exactly what a2ensite did 76
Look, this is the sym link that was created. To disable a virtual host, you just remove the sym link 77
from sites-enabled without removing the host file from sites -available. 78
You can remove the sym link using a tool called a2 79
dissite. Optionally, we can disable the default site in 000- default. 80
So a2dissite and the name of the file without .conf 81
And finally, I have to reload the server systemctl reload apache2 82
Let's test the configuration! I'm connecting to the server using the domain name. 83
And it gave me the contents from the virtual host directory, so the right website. If you have 84
more domains, so more websites, you replicate the same configuration, create a directory for each 85
website, in /var/www create a virtual host file for Apache in sites- available and enable the site 86
using a2ensite and finally restart the web server. That's all! 87
Thank you.