Port Based Hosting on Apache – PART 4

Port based hosting on Apache Web server

The default port number for HTTP is 80. However, most web servers can be configured to operate on almost any port number, provided the port number is not in use by any other program on the server.

For example, a server may host the website www.ittroubleshooter.in.  however, if the owner wishes to operate a second site, and does not have access to the domain name configuration for their domain name, and/or owns no other IP addresses which could be used to serve the site from, they could instead use another port number, for example;  www.ittroubleshooter.in:2526 for port 2526, www.ittroubleshooter.in:2527 for port 2527.

Previously we have seen various articles related to Apache Web server, refer below links;

Setting up Simple Web Server on RHEL7/Centos7

Create An Alias Web Site on Apache Web Server

Redirect the Website on Apache Web server

Steps to configure a Port based Web hosting

1) Make a directories and create html files for port based hosting.

In our demonstration we are creating two directories for port based web hosting in document root (i.e /var/www/).

[root@server1 ~]# cd /var/www/
[root@server1 www]# mkdir port2526
[root@server1 www]# mkdir port2527
[root@server1 www]#

Navigate to “port2526″ directory and create an index.html file.

[root@server1 www]# cd port2526
[root@server1 port2526]# vim index.html

Edit index.html  #vim index.html

<html>
<body>
<h1>Port 2526 Web hosting</h1>
##### Welcome to IT Troubleshooter ####
</body>
</html>

~
~
:wq

Similarly, we create the index.html file in port2527 directory.

[root@server1 www]# cd port2527
[root@server1 port2527]# vim index.html

Edit index.html #vim index.html

<html>
<body>
<h1>Port 2527 Web hosting</h1>
##### Welcome to IT Troubleshooter ####
</body>
</html>

~
~
:wq

Check the context using below command for both index.html files

[root@server1 ~]# ls -Z /var/www/port2526/index.html 
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/port2526/index.html
[root@server1 ~]# ls -Z /var/www/port2527/index.html 
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/port2527/index.html
[root@server1 ~]#

2) Edit the configuration file.

Go to ““/etc/httpd/conf.d”” directory, We already created (On previous articles) the  “ittroubleshooter.conf” file  and add a some lines on bottom, as shown below;

[root@server1 ~]# cd /etc/httpd/conf.d/
[root@server1 conf.d]# vim ittroubleshooter.conf

Edit ittroubleshooter.conf file # vim ittroubleshooter.conf

########## Port Based Hosting for port2526 Dir###########
<VirtualHost 192.168.1.10:2526>
ServerAdmin     root@ittroubleshooter.in
DocumentRoot    /var/www/port2526
ServerName      server1.ittroubleshooter.in
Errorlog        logs/server1.ittroubleshooter.in-error_log
CustomLog       logs/server1.ittroubleshooter.in-access_log common
</VirtualHost>  

######### Port Based Hosting for port2527 Dir############
<VirtualHost 192.168.1.10:2527>
ServerAdmin     root@ittroubleshooter.in
DocumentRoot    /var/www/port2527
ServerName      server1.ittroubleshooter.in
Errorlog        logs/server1.ittroubleshooter.in-error_log
CustomLog       logs/server1.ittroubleshooter.in-access_log common
</VirtualHost>

~
~
:wq

3) Add port on httpd.conf file.

Search for the “Listen 80” by using “/” and paste your port under it.
#vim /etc/httpd/conf/httpd.conf

#
#Listen 12.34.56.78:80
Listen 80
Listen 2526
Listen 2527
#
# Dynamic Shared Object (DSO) Support
#

Now we have to check the syntax for config files, using “httpd -t” command.

[root@server1 ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using server1.ittroubleshooter.in. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@server1 ~]#

You might probably faced the same following error while you were restarting the httpd service or check the syntax for config files. To fix above problem, edit the httpd.conf file and simply add the following line to the file.

#vim /etc/httpd/conf/httpd.conf

#
#ServerName www.example.com:80
ServerName localhost

#

Save the file and exit. finally check the syntax, using below command.

[root@server1 ~]# httpd -t
Syntax OK
[root@server1 ~]#

4) Restart the httpd Service.

[root@server1 ~]# systemctl restart httpd
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.
[root@server1 ~]#

Note: Before restart the service of httpd we need to add 2526 and 2527 ports on SELinux Policy Management port mapping tool.

[root@server1 ~]# semanage port -a -t http_port_t -p tcp 2526
[root@server1 ~]# semanage port -a -t http_port_t -p tcp 2527
[root@server1 ~]# semanage port -l | grep http_port_t | head -1
http_port_t                    tcp      2527, 2526, 80, 81, 443, 488, 8008, 8009, 8443, 9000
[root@server1 ~]#

Now retry to restart the service of Apache Web server.

[root@server1 ~]# systemctl restart httpd
 [root@server1 ~]# systemctl status httpd
  httpd.service - The Apache HTTP Server
 Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
 Active: active (running) since Sun 2016-11-27 02:49:11 EST; 10s ago
 Docs: man:httpd(8)
 man:apachectl(8)
 Process: 2659 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
 Main PID: 2675 (httpd)
 Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
 CGroup: /system.slice/httpd.service
 ├─2675 /usr/sbin/httpd -DFOREGROUND
 ├─2676 /usr/sbin/httpd -DFOREGROUND
 ├─2677 /usr/sbin/httpd -DFOREGROUND
 ├─2678 /usr/sbin/httpd -DFOREGROUND
 ├─2679 /usr/sbin/httpd -DFOREGROUND
 └─2680 /usr/sbin/httpd -DFOREGROUND

Nov 27 02:49:11 server1.ittroubleshooter.in systemd[1]: Starting The Apache HTTP Server...
 Nov 27 02:49:11 server1.ittroubleshooter.in systemd[1]: Started The Apache HTTP Server.
 [root@server1 ~]#

Check the listening status of both ports using netstat command.

[root@server1 ~]# netstat -lpn  | grep 2526
tcp6       0      0 :::2526                 :::*                    LISTEN      1609/httpd          
[root@server1 ~]# netstat -lpn  | grep 2527
tcp6       0      0 :::2527                 :::*                    LISTEN      1609/httpd          
[root@server1 ~]#

5) Allow both ports on firewall of the server.

If your Firewall service is running, then allow ports on it.

[root@server1 ~]# firewall-cmd --permanent --add-port=2526/tcp
success
[root@server1 ~]# firewall-cmd --permanent --add-port=2527/tcp
success
[root@server1 ~]# firewall-cmd --reload
success
[root@server1 ~]#

6) Open the  web browser and type the following url.

Open the firefox web browser and type http://192.168.1.10:2526

port2526_ittroubleshooter-in

Similarly for 2527 port the url is; http://192.168.1.10:2527

port2527_ittroubleshooter-in

To open the website from command line use the following command.

#elinks www.ittroubleshooter.in:2526

elinksport_ittroubleshooter-in

Hope this post will help Linux/Unix beginners. Please share you feedback and Comments.!!!

 

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz