{"id":81,"date":"2020-08-01T07:24:15","date_gmt":"2020-08-01T07:24:15","guid":{"rendered":"https:\/\/iste.umldb.com\/web-server-management\/?p=81"},"modified":"2020-08-01T07:25:55","modified_gmt":"2020-08-01T07:25:55","slug":"multiple-sites-on-different-ports","status":"publish","type":"post","link":"https:\/\/iste.umldb.com\/web-server-management\/2020\/08\/01\/multiple-sites-on-different-ports\/","title":{"rendered":"Multiple Sites on Different Ports"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">TCP\/IP Ports<\/h2>\n\n\n\n<p>A port in the world of TCP is usually used to determine which applications will be handed the data when a TCP\/IP message shows up.  For example if a computer receives a packet with the destination of port 80, the computer will usually hand this to the web server because that is usually what is listening on port 80.<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Multiple Apache Sites on Different Port Numbers\" width=\"960\" height=\"720\" src=\"https:\/\/www.youtube.com\/embed\/Vn5PWMKq-wg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Multiple Ports for Apache<\/h2>\n\n\n\n<p>While the Apache2 server is initially set up to listen only on Port 80, you can configure it to listen on a different port and even multiple ports and serve up different websites depending on the port configuration.<\/p>\n\n\n\n<p>I tend to use this  multiple port feature for testing new versions of software or for internal services for which I use another front end server to act as a reverse proxy (covered much later in this website).   This multi-port approach is often more convenient than using IP addresses but the URLs that you need to type into the browser are less than ideal, especially for an outside facing system.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Are You Listening?<\/h2>\n\n\n\n<p>If you look as the output of &#8220;ss&#8221;, you will see that after a standard apache2 installation on Ubuntu you will see the following:<\/p>\n\n\n<div class=\"wp-block-advanced-gutenberg-blocks-code\">\n  <header class=\"wp-block-advanced-gutenberg-blocks-code__header\">\n    <div class=\"wp-block-advanced-gutenberg-blocks-code__lang is-lang-shell\">\n      Shell    <\/div>\n    <div class=\"wp-block-advanced-gutenberg-blocks-code__file\">\n          <\/div>\n  <\/header>\n  <textarea \n    class=\"wp-block-advanced-gutenberg-blocks-code__source\" \n    name=\"codemirror-611560397\" \n    id=\"codemirror-611560397\"\n  >root@wsmdemo:\/etc\/apache2\/sites-enabled# ss -nta\nState        Recv-Q        Send-Q               Local Address:Port                 Peer Address:Port        Process       \nLISTEN       0             4096                 127.0.0.53%lo:53                        0.0.0.0:*                         \nLISTEN       0             128                        0.0.0.0:22                        0.0.0.0:*                         \nESTAB        0             0                    192.168.68.12:22                 192.168.68.103:57630                     \nLISTEN       0             511                              *:80                              *:*                         \nLISTEN       0             128                           [::]:22                           [::]:*       <\/textarea>\n  <script>\n    CodeMirror.fromTextArea( document.getElementById('codemirror-611560397'), {\n      mode: 'shell',\n      readOnly: true,\n      theme: 'hopscotch', \n      lineNumbers: false,\n      firstLineNumber: 1,\n      matchBrackets: true,\n      indentUnit: 4,\n      tabSize: 4,\n      lineWrapping: false,\n    } ); \n  <\/script>\n<\/div>\n\n\n\n<p>The interesting line here is second from the bottom where the Local Address:Port shows &#8220;*:80&#8221; that shows that Apache is listening on port 80.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Make Apache Listen<\/h2>\n\n\n\n<p>To make apache listen to another port, you need to get a &#8220;Listen&#8221; directive put into the configuration.  I will show you first the *standard* place where I think they intend for us to put things and I&#8217;ll also show you how I tend to organize multiple ports (for those times where I use them).<\/p>\n\n\n\n<p>The file \/etc\/apache2\/ports.conf contains entries that will make apache listen on port 80 and, if modules are enabled, also on port 443.   Adding additional ports is simple, just add new listen directives.<\/p>\n\n\n<div class=\"wp-block-advanced-gutenberg-blocks-code\">\n  <header class=\"wp-block-advanced-gutenberg-blocks-code__header\">\n    <div class=\"wp-block-advanced-gutenberg-blocks-code__lang is-lang-plain\">\n      Plain Text    <\/div>\n    <div class=\"wp-block-advanced-gutenberg-blocks-code__file\">\n      \/etc\/apache2\/ports.conf    <\/div>\n  <\/header>\n  <textarea \n    class=\"wp-block-advanced-gutenberg-blocks-code__source\" \n    name=\"codemirror-2107776353\" \n    id=\"codemirror-2107776353\"\n  >Listen 80\nListen 2000\nListen 2100\n\n&lt;IfModule ssl_module&gt;\n        Listen 443\n&lt;\/IfModule&gt;\n\n&lt;IfModule mod_gnutls.c&gt;\n        Listen 443\n&lt;\/IfModule&gt;<\/textarea>\n  <script>\n    CodeMirror.fromTextArea( document.getElementById('codemirror-2107776353'), {\n      mode: '',\n      readOnly: true,\n      theme: 'hopscotch', \n      lineNumbers: true,\n      firstLineNumber: 1,\n      matchBrackets: true,\n      indentUnit: 4,\n      tabSize: 4,\n      lineWrapping: true,\n    } ); \n  <\/script>\n<\/div>\n\n\n\n<p>Here, I&#8217;ve added listen directives for port 2000 and 2100.<\/p>\n\n\n\n<p>If you reload the configuration file and check the ports under &#8220;ss&#8221;, you will see now that port 2000 and 2100 are added to the list and you can even visit your site using the different port numbers but unfortunately you will probably always see the same page.  In order to show different content based on different port numbers you need to configure the VirtualHost entries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configure the site file<\/h2>\n\n\n\n<p>In the sites-available folder we can change the matching port number for the virtual host.  I&#8217;ve show this just for the &#8220;red&#8221; site now configured to be served up when requests are received on port 2100.<\/p>\n\n\n<div class=\"wp-block-advanced-gutenberg-blocks-code\">\n  <header class=\"wp-block-advanced-gutenberg-blocks-code__header\">\n    <div class=\"wp-block-advanced-gutenberg-blocks-code__lang is-lang-plain\">\n      Plain Text    <\/div>\n    <div class=\"wp-block-advanced-gutenberg-blocks-code__file\">\n      \/etc\/apache2\/sites-available\/red.conf    <\/div>\n  <\/header>\n  <textarea \n    class=\"wp-block-advanced-gutenberg-blocks-code__source\" \n    name=\"codemirror-1118464724\" \n    id=\"codemirror-1118464724\"\n  >&lt;VirtualHost *:2100&gt;\n\tServerAdmin webmaster@localhost\n\tDocumentRoot \/var\/www\/red\n\n\tErrorLog ${APACHE_LOG_DIR}\/error.log\n\tCustomLog ${APACHE_LOG_DIR}\/access.log combined\n&lt;\/VirtualHost&gt;<\/textarea>\n  <script>\n    CodeMirror.fromTextArea( document.getElementById('codemirror-1118464724'), {\n      mode: '',\n      readOnly: true,\n      theme: 'hopscotch', \n      lineNumbers: true,\n      firstLineNumber: 1,\n      matchBrackets: true,\n      indentUnit: 4,\n      tabSize: 4,\n      lineWrapping: true,\n    } ); \n  <\/script>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/iste.umldb.com\/web-server-management\/wp-content\/uploads\/sites\/2\/2020\/08\/image-1024x275.png\" alt=\"\" class=\"wp-image-82\" width=\"450\" height=\"121\" srcset=\"https:\/\/iste.umldb.com\/web-server-management\/wp-content\/uploads\/sites\/2\/2020\/08\/image-1024x275.png 1024w, https:\/\/iste.umldb.com\/web-server-management\/wp-content\/uploads\/sites\/2\/2020\/08\/image-300x81.png 300w, https:\/\/iste.umldb.com\/web-server-management\/wp-content\/uploads\/sites\/2\/2020\/08\/image-768x206.png 768w, https:\/\/iste.umldb.com\/web-server-management\/wp-content\/uploads\/sites\/2\/2020\/08\/image.png 1042w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/figure>\n\n\n\n<p>The important thing to remember is that just because you add the :2100 to the VirtualHost, it does not cause the server to magically start listening on this port.  I often troubleshoot student machines and they have left out the &#8220;Listen&#8221; command.  I usually get them to type &#8220;ss -nta&#8221; to show me if something is listening to the port.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">My Preferred Approach<\/h2>\n\n\n\n<p>Since I am often just enabling sites on non-standard port numbers temporarily I usually put the Listen command inside the site configuration file such as:<\/p>\n\n\n<div class=\"wp-block-advanced-gutenberg-blocks-code\">\n  <header class=\"wp-block-advanced-gutenberg-blocks-code__header\">\n    <div class=\"wp-block-advanced-gutenberg-blocks-code__lang is-lang-plain\">\n      Plain Text    <\/div>\n    <div class=\"wp-block-advanced-gutenberg-blocks-code__file\">\n      \/etc\/apache2\/sites-available\/red.conf    <\/div>\n  <\/header>\n  <textarea \n    class=\"wp-block-advanced-gutenberg-blocks-code__source\" \n    name=\"codemirror-446730904\" \n    id=\"codemirror-446730904\"\n  >Listen 2100\n&lt;VirtualHost *:2100&gt;\n\tServerAdmin webmaster@localhost\n\tDocumentRoot \/var\/www\/red\n\n\tErrorLog ${APACHE_LOG_DIR}\/error.log\n\tCustomLog ${APACHE_LOG_DIR}\/access.log combined\n&lt;\/VirtualHost&gt;<\/textarea>\n  <script>\n    CodeMirror.fromTextArea( document.getElementById('codemirror-446730904'), {\n      mode: '',\n      readOnly: true,\n      theme: 'hopscotch', \n      lineNumbers: true,\n      firstLineNumber: 1,\n      matchBrackets: true,\n      indentUnit: 4,\n      tabSize: 4,\n      lineWrapping: true,\n    } ); \n  <\/script>\n<\/div>\n\n\n\n<p>I do need to remove the &#8220;Listen&#8221; directive from the ports.conf file since we cannot have two listen commands for the same port.<\/p>\n\n\n\n<p>The reason that I like this is because I can now switch off the red site (and the listening port) in a single a2dissite command without having to remember to disable the site and then modify the ports.conf file.<\/p>\n\n\n\n<p>For the standard Port 80 and Port 443, I think it is best to just leave things where they are since the same port number is likely to be used multiple times.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TCP\/IP Ports A port in the world of TCP is usually used to determine which applications will be handed the data when a TCP\/IP message shows up. For example if a computer receives a packet with the destination of port 80, the computer will usually hand this to the web server because that is usually [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/iste.umldb.com\/web-server-management\/wp-json\/wp\/v2\/posts\/81"}],"collection":[{"href":"https:\/\/iste.umldb.com\/web-server-management\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iste.umldb.com\/web-server-management\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iste.umldb.com\/web-server-management\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iste.umldb.com\/web-server-management\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":3,"href":"https:\/\/iste.umldb.com\/web-server-management\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/iste.umldb.com\/web-server-management\/wp-json\/wp\/v2\/posts\/81\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/iste.umldb.com\/web-server-management\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iste.umldb.com\/web-server-management\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iste.umldb.com\/web-server-management\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}