{"id":5293,"date":"2018-03-25T09:03:31","date_gmt":"2018-03-25T13:03:31","guid":{"rendered":"http:\/\/www.linux-databook.info\/?page_id=5293"},"modified":"2018-03-25T09:20:38","modified_gmt":"2018-03-25T13:20:38","slug":"basic-apache-web-server-configuration","status":"publish","type":"page","link":"http:\/\/www.linux-databook.info\/?page_id=5293","title":{"rendered":"Basic Apache Web Server Configuration"},"content":{"rendered":"<p>I have had my own web sites for many years now. Since switching from OS\/2 to Linux over 20 years ago, I have used Apache as my server software. Apache is solid, well-known, and is quite easy to configure for a basic installation. It is not really that much more difficult to configure for a more complex setup such as multiple web sites.<\/p>\n<p>Installation and configuration of the Apache web server must be performed as root. Configuring the firewall also needs to be performed as root. Using a browser to view the results of this work should be done as a non-root user. I use the user, student, on my virtual host.<\/p>\n<h2 class=\"western\">Installation<\/h2>\n<p><i><b>Note:<\/b><\/i><i> This article was written on a virtual machine using Fedora 27 with Apache 2.4.29. <\/i><i>The commands you will need to use and the locations and content of the configuration files may be different if you have a different distribution <\/i><i>or a different release of Fedora<\/i><i>. <\/i><i>However the configuration lines that need to be modified for this article are the same.<\/i><\/p>\n<p>The Apache web server is easy to install. On my CentOS 6.x server all it takes is a simple YUM command. It installs all of the necessary dependencies if any are missing. On one of my Fedora virtual machines I used the dnf command below. The syntax for dnf and yum are the same except for the name of the command itself.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">dnf -y install httpd<\/span><\/pre>\n<p>The VM was a very basic desktop installation I am using as a test bed for writing a book. Even on this system, only six dependencies were installed in under a minute.<\/p>\n<p>All of the configuration files for Apache are located in \/etc\/httpd\/conf and \/etc\/httpd\/conf.d. The data for the web sites is located in \/var\/www by default but you can change that if you want.<\/p>\n<h2 class=\"western\">Configuration<\/h2>\n<p>The primary Apache configuration file is \/etc\/httpd\/conf\/httpd.conf. It contains a lot of configuration statements that we do not need to change for our basic installation. In fact, we only need to make a few changes to this file to get a basic web site up and running. The file is very large so rather than clutter this article with a lot of stuff we don\u2019t need, I will just show those directives that we do change.<\/p>\n<p>First, I suggest that you take a bit of time and browse through the httpd.conf file to familiarize yourself with it. One of the things I like about Red Hat versions of most configuration files is the number of comments that describe the various sections and configuration directives in the files. The httpd.conf file is no exception as it is quite well commented. Use these comments to understand what the file is configuring.<\/p>\n<p>The first item we need to change is the Listen statement which defines the IP address and port on which Apache is to listen for page requests. All we are going to do here is make this web site available to the local machine so we will use the localhost address. The line should look like this when you finish.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">Listen 127.0.0.1:80<\/span><\/pre>\n<p>With the Listen directive set to the IP address of the localhost, Apache will only listen for connections from the local host. If we wanted the web server to listen for connections from remote hosts we would use the host\u2019s external IP address.<\/p>\n<p>The DocumentRoot directive specifies the location of the HTML files that make up the pages of the web site. That line does not need to be changed because it already points to the standard location. The line should look like this.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">DocumentRoot \"\/var\/www\/html\"<\/span><\/pre>\n<p>The Apache installation RPM creates the \/var\/www directory tree. If we wanted to change the location in which the web site files are stored we would use this configuration item to do that. For example we might want to use a different name name for the www subdirectory to make the identification of the web site more explicit. That might look like this.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">DocumentRoot \"\/var\/mywebsite\/html\"<\/span><\/pre>\n<p>This is all of the Apache configuration we need to change to create a simple web site. For this little exercise we only made a single change to the httpd.conf file \u2013 the Listen directive. Everything else is already configured to produce a working web server.<\/p>\n<p>We still need to make one other change. We need to open port 80 in our firewall. I use IPTables as my firewall so I change \/etc\/sysconfig\/iptables to add a statement that allows HTTP protocol. The entire file looks like the following.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\"># sample configuration for iptables service<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\"># you can edit this manually or use system-config-firewall<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\"># please do not ask us to add additional ports\/services to this default configuration<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">\r\n*filter<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">:INPUT ACCEPT [0:0]<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">:FORWARD ACCEPT [0:0]<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">:OUTPUT ACCEPT [0:0]<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">-A INPUT -p icmp -j ACCEPT<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">-A INPUT -i lo -j ACCEPT<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\"><b>-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT<\/b><\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">-A INPUT -j REJECT --reject-with icmp-host-prohibited<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">-A FORWARD -j REJECT --reject-with icmp-host-prohibited<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">COMMIT<\/span><\/pre>\n<p>The line I added is highlighted in bold. Now I reload the altered IPTables configuration.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">[root@testvm1 ~]# <b>cd \/etc\/sysconfig\/ ; iptables-restore iptables<\/b><\/span><\/pre>\n<h2 class=\"western\">Create the index.html file<\/h2>\n<p>The index.html file is the default file that a web server will serve up when you access the web site just using the domain name and not a specific HTML file name. In the \/var\/www\/html directory, create a file with the name index.html. Add the content \u201cHello World\u201d without the quotes. You do not need to add any HTML markup to make this work. The sole job of the web server is to serve up a stream of text data and the server has no idea what the date is or of how to render it. It simple transmits the data stream to the requesting host.<\/p>\n<p>After saving the file set the ownership to apache.apache.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">[root@testvm1 html]# <b>chown apache.apache index.html<\/b><\/span><\/pre>\n<h2 class=\"western\">Start Apache<\/h2>\n<p>Apache is very easy to start. Current versions of Fedora use Systemd. Simply run the following commands to start it and then to check the status of the server.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">[root@testvm1 ~]# <b>systemctl start httpd<\/b><\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">[root@testvm1 ~]# <b>systemctl status httpd<\/b><\/span>\r\n<span style=\"font-size: 12pt;\">\u25cf <span style=\"font-family: 'Liberation Mono', monospace;\">httpd.service - The Apache HTTP Server<\/span><\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">Loaded: loaded (\/usr\/lib\/systemd\/system\/httpd.service; disabled; vendor preset: disabled)<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">Active: active (running) since Thu 2018-02-08 13:18:54 EST; 5s ago<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">Docs: man:httpd.service(8)<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">Main PID: 27107 (httpd)<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">Status: \"Processing requests...\"<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">Tasks: 213 (limit: 4915)<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">CGroup: \/system.slice\/httpd.service<\/span>\r\n<span style=\"font-size: 12pt;\">\u251c\u2500<span style=\"font-family: 'Liberation Mono', monospace;\">27107 \/usr\/sbin\/httpd -DFOREGROUND<\/span><\/span>\r\n<span style=\"font-size: 12pt;\">\u251c\u2500<span style=\"font-family: 'Liberation Mono', monospace;\">27108 \/usr\/sbin\/httpd -DFOREGROUND<\/span><\/span>\r\n<span style=\"font-size: 12pt;\">\u251c\u2500<span style=\"font-family: 'Liberation Mono', monospace;\">27109 \/usr\/sbin\/httpd -DFOREGROUND<\/span><\/span>\r\n<span style=\"font-size: 12pt;\">\u251c\u2500<span style=\"font-family: 'Liberation Mono', monospace;\">27110 \/usr\/sbin\/httpd -DFOREGROUND<\/span><\/span>\r\n<span style=\"font-size: 12pt;\">\u2514\u2500<span style=\"font-family: 'Liberation Mono', monospace;\">27111 \/usr\/sbin\/httpd -DFOREGROUND<\/span><\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">Feb 08 13:18:54 testvm1 systemd[1]: Starting The Apache HTTP Server...<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">Feb 08 13:18:54 testvm1 systemd[1]: Started The Apache HTTP Server.<\/span><\/pre>\n<p>The commands may be different on your server. On Linux systems that use SystemV start scripts, the commands would be as shown here.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">[root@testvm1 ~]# <b>service httpd start<\/b><\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">Starting httpd: [Fri Feb 09 08:18:07 2018] [ OK ]<\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">[root@testvm1 ~]# <b>service httpd status<\/b><\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">httpd (pid 14649) is running...<\/span><\/pre>\n<p>If you have a web browser like Firefox or Chrome on your host you can use the URL localhost on the URL line of the browser to display your web page, simple as it is. You could also use a text mode web browser like lynx to view the web page we have created. First install lynx if it is not already installed.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">[root@testvm1 ~]# <b>dnf -y install lynx<\/b><\/span><\/pre>\n<p>Then I use the following command to display the web page.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">[root@testvm1 ~]# <b>lynx localhost<\/b><\/span><\/pre>\n<p>The result looks like this in my terminal session. I have deleted a lot of the empty space on the page.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">Hello World <\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">\r\n&lt;snip - removed lots of blank lines&gt; <\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">\r\nCommands: Use arrow keys to move, '?' for help, 'q' to quit, '&lt;-' to go back. <\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">Arrow keys: Up and Down to move. Right to follow a link; Left to go back. <\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">H)elp O)ptions P)rint G)o M)ain screen Q)uit \/=search [delete]=history list <\/span><\/pre>\n<p>Edit your index.html file and add a bit of HTML markup so it looks like this.<\/p>\n<pre><span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">&lt;h1&gt;Hello World&lt;\/h1&gt;<\/span><\/pre>\n<p>Now refresh the browser. For lynx, use the key combination Ctrl-R. The results look just a bit different. The text is in color, which is how lynx displays headings, if your terminal supports color, and it is now centered. In a GUI browser the text would be in a large font.<\/p>\n<pre><span style=\"font-size: 12pt;\"><span style=\"font-family: 'Liberation Mono', monospace;\">Hello World <\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace; font-size: 12pt;\">\r\n&lt;snip - removed lots of blank lines&gt; <\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace;\">Commands: Use arrow keys to move, '?' for help, 'q' to quit, '&lt;-' to go back. <\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace;\">Arrow keys: Up and Down to move. Right to follow a link; Left to go back. <\/span>\r\n<span style=\"font-family: 'Liberation Mono', monospace;\">H)elp O)ptions P)rint G)o M)ain screen Q)uit \/=search [delete]=history list<\/span><\/span><\/pre>\n<h2 class=\"western\">Parting thoughts<\/h2>\n<p>As you can see from this little exercise, it is easy to set up an Apache web server. The specifics will vary depending upon your distribution and the version of Apache supplied by that distribution. In my environment this was a pretty trivial exercise.<\/p>\n<p>But there is more because Apache is very flexible and powerful. In the <a href=\"http:\/\/www.linux-databook.info\/?page_id=5300\" target=\"_blank\" rel=\"noopener\">next article<\/a> I discuss hosting multiple web sites using a single instance of Apache.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have had my own web sites for many years now. Since switching from OS\/2 to Linux over 20 years ago, I have used Apache as my server software. Apache is solid, well-known, and is quite easy to configure for&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/www.linux-databook.info\/?page_id=5293\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":3691,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5293","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/5293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5293"}],"version-history":[{"count":7,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/5293\/revisions"}],"predecessor-version":[{"id":5304,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/5293\/revisions\/5304"}],"up":[{"embeddable":true,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/3691"}],"wp:attachment":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}