The apache webserver is currently (at the date of writing this (08/2018)) the most popular web server for serving content on the internet. The Functionality we'll look at in this article is called 'Virtual Hosts'.
A virtual host is essential another domain running on the same server instance of apache.
For this demonstration, I'm going to use a CentOS 7.5.
First, update your system:
[archy@websrv ~]$ sudo yum -y update
Next install apache on the server:
[archy@websrv ~]$ sudo yum -y install httpd
By default, the webserver will not start on boot so we'll change that and start it for now:
[archy@websrv ~]$ sudo systemctl enable httpd.service
[archy@websrv ~]$ sudo systemctl start httpd.service
By now your webserver is already serving the default-welcome page. This can be checked by curl or simply pointing your browser to the server's address. But since I don't have any firewall-rules set up yet, I'm going to use curl.
[archy@websrv ~]$ curl localhost
This should return the source code for the default website.
Let's create the directory structure I'm going to use. This structure was originally introduced by debian developers but it makes it easy to keep track of enabled virtual hosts and inactive ones.
[archy@websrv ~]$ mkdir -p /etc/httpd/sites-available
[archy@websrv ~]$ mkdir -p /etc/httpd/sites-eanbled
Your configs will go into /etc/httpd/sites-available and then symlinked to /etc/httpd/sites-enabled.
We'll also have to make apache aware that it should include these directories.
Open the /etc/httpd/conf/httpd.conf in your favorite text-editor and add the IncludeOptional-line at the end of the config file:
[archy@websrv ~]$ sudo vim /etc/httpd/conf/httpd.conf
IncludeOptional sites-enabled/*.conf
Apache is now configured accordingly. Next we'll have to create the virtual host's config files.
[archy@websrv ~]$ sudo vim /etc/httpd/sites-available/test1.archyslife.lan.conf
<VirtualHost *:80>
ServerAdmin webmaster@archyslife.lan
DocumentRoot /var/www/test1.archyslife.lan
ServerName test1.archyslife.lan
<Directory /var/www/test1.archyslife.lan/>
Options Indexes FollowSymlinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/httpd/test1.archyslife.lan.error.log
LogLevel warn
CustomLog /var/log/httpd/test1.archyslife.lan.access.log combined
ServerSignature on
</VirtualHost>
[archy@websrv ~]$ sudo vim /etc/httpd/sites-available/test2.archyslife.lan.conf
<VirtualHost *:80>
ServerAdmin webmaster@archyslife.lan
DocumentRoot /var/www/test2.archyslife.lan
ServerName test2.archyslife.lan
<Directory /var/www/test2.archyslife.lan/>
Options Indexes FollowSymlinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/httpd/test2.archyslife.lan.error.log
LogLevel warn
CustomLog /var/log/httpd/test2.archyslife.lan.access.log combined
ServerSignature on
</VirtualHost>
The virtual hosts are created now, let's enable them. As mentioned before, they need to be symlinked to the /etc/httpd/sites-enabled directory:
[archy@websrv ~]$ sudo ln -s /etc/httpd/sites-available/test1.archyslife.lan.conf /etc/httpd/sites-enabled/
[archy@websrv ~]$ sudo ln -s /etc/httpd/sites-available/test2.archyslife.lan.conf /etc/httpd/sites-enabled/
Next, we'll have to create the folder-structure for the virtual hosts:
[archy@websrv ~]$ sudo mkdir -p /var/www/test1.archyslife.lan
[archy@websrv ~]$ sudo mkdir -p /var/www/test2.archyslife.lan
With the folders created, let's create a basic index.html to be served:
[archy@websrv ~]$ sudo vim /var/www/test1.archyslife.lan/index.html
<html>
<head>
<title>Welcome to test1.archyslife.lan</title>
</head>
<body>
<h1>This is the page test1.archyslife.lan</h1>
</body>
</html>
[archy@websrv ~]$ sudo vim /var/www/test2.archyslife.lan/index.html
<html>
<head>
<title>Welcome to test2.archyslife.lan</title>
</head>
<body>
<h1>This is the page test2.archyslife.lan</h1>
</body>
</html>
The apache is now configured. Let's restart and test the config:
[archy@websrv ~]$ sudo systemctl restart httpd.service
Next add the server's IP address to the /etc/hosts file with the according fqdns.
[archy@websrv ~]$ sudo vim /etc/hosts
172.31.0.100 test1.archyslife.lan
172.31.0.100 test2.archyslife.lan
Run curl with the domains and you should get your index.html's sourcecode back:
[archy@websrv ~]$ curl test1.archyslife.lan
<html>
<head>
<title>Welcome to test1.archyslife.lan</title>
</head>
<body>
<h1>This is the page test1.archyslife.lan</h1>
</body>
</html>
[archy@websrv ~]$ curl test2.archyslife.lan
<html>
<head>
<title>Welcome to test2.archyslife.lan</title>
</head>
<body>
<h1>This is the page test2.archyslife.lan</h1>
</body>
</html>
Last step, adding firewall rules. To make our webserver available from public, add the port 80 to the firewall.
[archy@websrv ~]$ sudo firewall-cmd --add-service=http --permanent
[archy@websrv ~]$ sudo firewall-cmd --reload
That concludes the setup.
Feel free to comment and / or suggest a topic.
Comments
Post a Comment