{"id":125,"date":"2017-01-02T20:07:36","date_gmt":"2017-01-02T20:07:36","guid":{"rendered":"http:\/\/cellbiol.com\/test-bioinfo\/?page_id=125"},"modified":"2017-06-04T16:57:14","modified_gmt":"2017-06-04T16:57:14","slug":"apache-web-server-configuration","status":"publish","type":"page","link":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-2-the-linux-operating-system-setting-up-a-linux-web-server\/apache-web-server-configuration\/","title":{"rendered":"2-6: Apache Web Server Configuration"},"content":{"rendered":"<h2>Localhost and the Web Root Directory<\/h2>\n<p>The Apache web server comes with a default configuration that is suitable for immediately publishing a web site, with no special modifications.<\/p>\n<p>We have seen in the <a href=\"\/bioinformatics_web_development\/chapter-2-the-linux-operating-system-setting-up-a-linux-web-server\/installing-a-lamp-linux-apache-php-mysql-server\/\">previous section<\/a>, that when we visit &#8220;localhost&#8221;, or the equivalent IP address 127.0.0.1 on a web browser, we see the default web page provided by Apache with the famous and reassuring &#8220;It Works!&#8221; message.<\/p>\n<p>A note about the 127.0.0.1 IP address and localhost. This is also known as the server &#8220;Home&#8221;. These terms are familiar to <a href=\"http:\/\/www.wikihow.com\/Be-a-Computer-Geek\">geeks<\/a>, as you can see in the geek t-shirt represented in the figure below.<\/p>\n<figure id=\"attachment_127\" aria-describedby=\"caption-attachment-127\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-content\/uploads\/2017\/01\/localhost_t-shirt.jpeg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-127\" src=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-content\/uploads\/2017\/01\/localhost_t-shirt.jpeg\" alt=\"A localhost T-shirt\" width=\"300\" height=\"300\" srcset=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-content\/uploads\/2017\/01\/localhost_t-shirt.jpeg 300w, http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-content\/uploads\/2017\/01\/localhost_t-shirt-150x150.jpeg 150w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-127\" class=\"wp-caption-text\">Figure 2-6-1: A localhost T-shirt &#8211; Source: <a href=\"https:\/\/thinkgeek.com\">Thinkgeek<\/a><\/figcaption><\/figure>\n<p>Let&#8217;s come back to the Apache default home page. This page is a plain html page, that is a normal text file that can be edited with any text editor (more on text editors in the next chapter) including for example the nano shell editor that was discussed in <a href=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-2-the-linux-operating-system-setting-up-a-linux-web-server\/basic-linux-shell-commands\/\">section 2-3<\/a>. This text file, whose file name ends with a &#8220;.html&#8221; extension, is located on your Linux server and was created by the installer at the time of Apache installation.<\/p>\n<p>The full name of this file is &#8220;index.html&#8221;. This is a file name you will become very familiar with. More on this in a moment.<\/p>\n<p>When you visit localhost, the index.html file is loaded to the browser. The browser interprets the html code of the file, looking for example for parts of text that should be formatted and rendered in a particular way, graphically and for images to be included in the page. The browser then shows you a nicely formatted page.<\/p>\n<p>In the default Apache page for instance, the &#8220;It Works!&#8221; part is rendered in bold and is on a line of it&#8217;s own with respect to the rest of the text in the page, as shown in figure 2-5-2 of <a href=\"\/bioinformatics_web_development\/chapter-2-the-linux-operating-system-setting-up-a-linux-web-server\/installing-a-lamp-linux-apache-php-mysql-server\/\">the previous section<\/a>.<\/p>\n<p>How text formatting and page layouts can be achieved by using the HTML markup language will be discussed in depth in the next chapter.<\/p>\n<p>Instead, we will now focus on our index.html as a file of the computer. Where is this index.html file, show by default on visiting the server&#8217;s Home\/localhost\/127.0.0.1, located in the Linux filesystem?<\/p>\n<h2>Editing Apache configuration files<\/h2>\n<p>The index.html is located in a special directory of the filesystem called the <strong>Web Root<\/strong> or <strong>Document Root<\/strong>. The path of the web root directory is specified in one of the Apache configuration files, located at:<\/p>\n<p>\/etc\/apache2\/sites-enabled\/000-default<\/p>\n<p>After installing Apache you can view the contents of this file on your system by running:<\/p>\n<p>cat \/etc\/apache2\/sites-enabled\/000-default.conf <\/p>\n<p>In recent versions of Apache, the default web root directory, that contains the index.html file, is as follows:<\/p>\n<p>\/var\/www\/html <\/p>\n<p>The path was \/var\/www in older versions of Apache.<\/p>\n<p>so the index.html file&#8217;s path is:<\/p>\n<p>\/var\/www\/html\/index.html<\/p>\n<figure id=\"attachment_2651\" aria-describedby=\"caption-attachment-2651\" style=\"width: 1218px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-content\/uploads\/2017\/01\/DocumentRoot_directive_iin_apache_configuration.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-content\/uploads\/2017\/01\/DocumentRoot_directive_iin_apache_configuration.png\" alt=\"The Document Root Directive in an Apache configuration file\" width=\"1218\" height=\"412\" class=\"size-full wp-image-2651\" srcset=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-content\/uploads\/2017\/01\/DocumentRoot_directive_iin_apache_configuration.png 1218w, http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-content\/uploads\/2017\/01\/DocumentRoot_directive_iin_apache_configuration-300x101.png 300w, http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-content\/uploads\/2017\/01\/DocumentRoot_directive_iin_apache_configuration-768x260.png 768w, http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-content\/uploads\/2017\/01\/DocumentRoot_directive_iin_apache_configuration-1024x346.png 1024w, http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-content\/uploads\/2017\/01\/DocumentRoot_directive_iin_apache_configuration-1200x406.png 1200w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption id=\"caption-attachment-2651\" class=\"wp-caption-text\">Figure 2-6-2: The Document Root Directive in an Apache configuration file<\/figcaption><\/figure>\n<p>If we wanted to, we could change the web root directory by editing the configuration file. We need to use the sudo command and edit the file as root, as Apache configuration files belong to root. Therefore, as a normal user, you will not have permission to edit the configuration files.<\/p>\n<p>andrea@ubuntu:~$ sudo nano -w \/etc\/apache2\/sites-enabled\/000-default.conf<\/p>\n<p>We could then change the DocumentRoot&#8217;s directory path, save the file with Ctrl + o, exit nano with Ctrl + x, restart the Apache web server so as to re-load the edited configuration file and get the modification to work:<\/p>\n<p>andrea@ubuntu:~$ sudo service apache2 restart<\/p>\n<p>or<\/p>\n<p>andrea@ubuntu:~$ \/etc\/init.d\/apache2 restart<\/p>\n<p>However there is no special need to change the default DocumentRoot path, the default directory \/var\/www\/html is just fine for our purposes.<\/p>\n<h2>Using the UserDir Apache module to enable individual web development accounts for server&#8217;s users<\/h2>\n<p>We would now like to turn our web server in the course&#8217;s web server, where students can each have an account that they can use to practice their Bioinformatics web development skills and then, at the end of the course, build their own Bioinformatics web application.<\/p>\n<p>An account for each student can easily be created with the &#8220;adduser&#8221; shell command that was already discussed in a <a href=\"\/bioinformatics_web_development\/chapter-2-the-linux-operating-system-setting-up-a-linux-web-server\/basic-linux-shell-commands\/\">previous section<\/a>.<\/p>\n<p>We then have to enable the so called &#8220;userdir&#8221; directories Apache functionality, so that each user will automatically be able to publish web contents at a public address. Each user will have a personal folder that will act as it&#8217;s personal &#8220;Document Root&#8221; directory.<\/p>\n<p>We will configure Apache so that each user will have a public web address accessible at web URL of this kind:<\/p>\n<p>http:\/\/web-server-ip\/~username<\/p>\n<p>Please note the tilde ~ that precedes the username and indeed needs to be included in the URL for the address to work.<\/p>\n<p>The Document Root of each user will be a directory named:<\/p>\n<p>public_html<\/p>\n<p>located in each user&#8217;s home directory, at \/home\/username\/public_html<\/p>\n<p>Everything in this directory will be visible at the URL given above, for each user.<\/p>\n<p>Here&#8217;s the full procedure to enable this setup:<\/p>\n<p>We call the &#8220;a2enmod&#8221; shell command (<strong>A<\/strong>pache<strong>2<\/strong> <strong>En<\/strong>able <strong>Mod<\/strong>ule) with the name of the module we want to activate as an argument. We need to activate the &#8220;userdir&#8221; Apache module.<\/p>\n<p>andrea@ubuntu:~$ sudo a2enmod userdir<\/p>\n<p>Please note that disabling Apache2 modules is done instead with the &#8220;a2dismod&#8221; shell command.<\/p>\n<p>Create the public_html web directories in the user directories. Each user may create it&#8217;s own by going to his home directory with:<\/p>\n<p>username@ubuntu:~$ cd<\/p>\n<p>and then typing:<\/p>\n<p>username@ubuntu:~$ mkdir public_html<\/p>\n<p>Restart apache:<\/p>\n<p>andrea@ubuntu:~$ sudo service apache2 restart<\/p>\n<p>The public_html directories can now be visited with the browser at:<\/p>\n<p>http:\/\/web-server-ip\/~username<\/p>\n<p>In order for this to work, however, make sure that the users directories (\/home\/username) have a 755 permission, otherwise you may get a Forbidden message on trying to visit the users personal web directories at the address above because Apache will be unable to read\/search into them so as to reach the public_html directories.<\/p>\n<h2>Enabling PHP for users web directories<\/h2>\n<p>Still a little more configuration editing in order to enable the execution of PHP scripts in the user&#8217;s public_html directories.<\/p>\n<p>First run the following command to check out the name of the current php conf file.<\/p>\n<p>andrea@ubuntu:~$ ls \/etc\/apache2\/mods-enabled\/ | grep php<\/p>\n<p>You may get an output like this:<\/p>\n<p>php7.0.conf<br \/>\nphp7.0.load<\/p>\n<p>We are interested in the file that ends in .conf, in this case this would be php7.0.conf.<\/p>\n<p>Let&#8217;s edit this file:<\/p>\n<p>andrea@ubuntu:~$ sudo nano -w \/etc\/apache2\/mods-enabled\/php7.0.conf<\/p>\n<p>As PHP in user directories is disabled by default, we need to enable this feature by commenting or deleting the following part of the file:<\/p>\n<p>&lt;ifModule mod_userdir.c&gt;<br \/>\n&lt;Directory \/home\/*\/public_html&gt;<br \/>\nphp_admin_value engine Off<br \/>\n&lt;\/Directory&gt;<br \/>\n&lt;\/ifModule&gt;<\/p>\n<p>Commenting means adding a # before each line we wish to &#8220;inactivate&#8221;. The commented version of the code above would be:<\/p>\n<p>#&lt;ifModule mod_userdir.c&gt;<br \/>\n# &lt;Directory \/home\/*\/public_html&gt;<br \/>\n# php_admin_value engine Off<br \/>\n# &lt;\/Directory&gt;<br \/>\n#&lt;\/ifModule&gt;<\/p>\n<p>As usual, save the file (ctrl + o) and exit nano (ctrl + x).<\/p>\n<p>We can now restart the Apache web server so that the configuration files are reloaded and the modifications can become effective:<\/p>\n<p>andrea@ubuntu:~$ sudo service apache2 restart<\/p>\n<h2>Testing the setup<\/h2>\n<p>Each user can now create an index.php file in his public_html directory:<\/p>\n<p>andrea@ubuntu:~$ nano -w \/home\/username\/public_html\/index.php<\/p>\n<p>write something inside:<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\necho \"Just testing!\";\r\n\r\n?>\r\n<\/code><\/pre>\n<p>save the file with Ctrl + o, exit nano with Ctrl + x<\/p>\n<p>The result can be seen by visiting the user&#8217;s home URL at:<\/p>\n<p>http:\/\/web-server-ip\/~username<\/p>\n<p>or, if working locally:<\/p>\n<p>http:\/\/localhost\/~username<\/p>\n<p>If you have installed Linux on a machine that you will use exclusively, for example your laptop, there is no need to activate the userdir module. You can work directly at DocumentRoot, in the \/var\/www\/html folder (being the system administrator comes with some privileges). in this case we invite you, as a first exercise, to edit the default Apache index.html file:<\/p>\n<p>andrea@ubuntu:~$ sudo nano -w \/var\/www\/html\/index.html<\/p>\n<p>If you followed us up to this point and succeeded in having your first index.html page online, we expect you to post a success message below to let us know!<\/p>\n<p>Having published online the first Hello World message is great, however a few more things need to be learned in order to build a nice web page or maybe a full website. We will learn all the relevant skills in the next chapter: <a href=\"\/bioinformatics_web_development\/chapter-3-your-first-web-page-learning-html-and-css\/\">Your first webpage, Learning HTML and CSS<\/a>.<\/p>\n<div class=\"google-ad\">\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- bioinfo web dev 2 --><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:728px;height:90px\"\n     data-ad-client=\"ca-pub-0159360445983090\"\n     data-ad-slot=\"3442176918\"><\/ins><br \/>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/div>\n<h2>Chapter Sections<\/h2>\n<p>[pagelist include=&#8221;63&#8243;]<\/p>\n<p>[siblings]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Localhost and the Web Root Directory The Apache web server comes with a default configuration that is suitable for immediately publishing a web site, with no special modifications. We have seen in the previous section, that when we visit &#8220;localhost&#8221;, or the equivalent IP address 127.0.0.1 on a web browser, we see the default web &hellip; <a href=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-2-the-linux-operating-system-setting-up-a-linux-web-server\/apache-web-server-configuration\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;2-6: Apache Web Server Configuration&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":63,"menu_order":6,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-125","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":28,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/125\/revisions"}],"predecessor-version":[{"id":2664,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/125\/revisions\/2664"}],"up":[{"embeddable":true,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/63"}],"wp:attachment":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/media?parent=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}