Apache Apache

So Why do I want a web server at home?

I can browse the web anytime I want, why would I want to create my own web pages?

Well maybe you wouldn’t. But then again, why not learn how to create a web on your own. Have you ever considered how you might use a web to share everything from pictures, to books inside your own home.

But before you can explore creating your own web, you need to know how to create web pages. Additionally you need to know to link web pages together. So tonight we are going to learn how to setup our Ubuntu Linux box as a web server and add web pages.

Setting up an Apache web server.

I am going to show you how to setup the Apache2 web server and php5 on Ubuntu Linux. If you are using another Linux distribution, the setup will be similar.

The first step is to start up the Ubuntu Software Center. In the search box enter Apache2, select it from the list and click on install.

Ubuntu Software Center Ubuntu Software Center

Next you should search for php5. This will be used later, but we might as well install it now.

If everything has gone well you can nos open a web browser and enter URL http://localhost. If everything went well, you should now see a page something like: Localhost. This is a default web page for the Apache2 server.

Lets do a little more setup to make things easier to start with. For this step you will need to enter commands in a terminal. First I want you to create a location in your home directory to hold web pages. Since I log in as john I will create the director mkdir ~/public_html. Next I am going to change the permissions on the directory with the command: chmod 755 ~/public_html. This makes this directory accessible to the web server.

Next we are going to enable the userdir to host web pages. Execute the command sudo a2enmod userdir to configure the web server to look in user directories. Then the command sudo service apache2 restart to restart the web server.

Next we are going to create a simple web page named test1.html in the directory ~/public_html like this:

    <html>
    <body>
    <h1> Hello John </h1>
    </body>
    </html>

This is a simple page which says Hello John. You can view the page by entering the URL: http://localhost/~john/test1.html. Congratulations, you have created your first web page.

Writing HTML Writing HTML

Web Creation Tutorial

Lets spend some time exploring what I think is one of the best tutorials about creating web pages. It is also one of the oldest. It was originally written to explain how to create web pages for teachers. The only tools you need are a simple editor and a web browser.

In order to make this simple all the web pages will be created in your local directory ~/public_html. The URL for any of the page you create is http://localhost/~($USER)/(page), where $USER is your login name, and page is the web page you just created. So lets get started.

Writing HTML. You can also download the tutorial to your local machine if you like.

OK, now that we know how to create web pages, lets look at how to add them to the regular web directories.

Apache Docroot

One of the first things we need to know is where the web pages are stored on our computer. Again I am working under Ubuntu, but the locations are similar on other versions of Linux.

The first thing we need to know is where does Apache2 place it’s web pages. The value is stored in the file: /etc/apache2/sites-available/default. For Ubuntu, the value is /var/www. Lets take a quick look at parts of this config file.

      <VirtualHost *:80>
          ServerAdmin webmaster@localhost

         DocumentRoot /var/www 
         <Directory />
             Options FollowSymLinks
             AllowOverride None
         </Directory>
         <Directory /var/www/>
             Options Indexes FollowSymLinks MultiViews
             AllowOverride None
             Order allow,deny
             allow from all
         </Directory>

         ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
         <Directory "/usr/lib/cgi-bin">
             AllowOverride None
             Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
             Order allow,deny
             Allow from all
         </Directory>

         ErrorLog /var/log/apache2/error.log

         # Possible values include: debug, info, notice, warn, error, crit,
         # alert, emerg.
         LogLevel warn

         CustomLog /var/log/apache2/access.log combined

     Alias /doc/ "/usr/share/doc/"
     <Directory "/usr/share/doc/">
         Options Indexes MultiViews FollowSymLinks
         AllowOverride None
         Order deny,allow
         Deny from all
         Allow from 127.0.0.0/255.0.0.0 ::1/128
     </Directory>

     </VirtualHost>

Now I don’t expect you to read this file, but I suggest you take note of two lines: DocumentRoot /var/www tells you where the root of the web server is located. Alias /doc/ “/usr/share/doc/” tells you the root for your local documentation. We will come back to the doc alias later.

So the root of the web server is /var/www . This means that any file in that directory does not need a path statement. Lets take a look the contents of this directory. Initially you will see one file index.html.

      <html><body><h1>It works!</h1>
      <p>This is the default web page for this server.</p>
      <p>The web server software is running but no content has been added,
      yet.</p>
      </body></html>
 
    

Now that we know where web pages live we can start creating web pages at this location. I am not going to go into detail about how to use the placement of files. This should become obvious from the tutorial above.

Now that we have explored static web pages, lets have a look at more active web pages. For this part of the talk, lets focus on PHP.

PHP

PHP Logo
PHP Logo

Lets start with a definition of what is PHP

PHP: Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document.

Wikipedia PHP

I am going to show you some ideas with php to give you a view into server side scripts. PHP is one type, the other older type is named CGI scripts. These are programs which can perform operations on the server and display the results on the web server.

PHP has become quite popular lately because it allows you to create web pages on the fly. It has the ability to access databases, and construct many complex web pages. I am going to show you two web pages using php. There are a number of applications which use PHP to give you access to applications on your computer. You can explore them yourself.

As a first step lets make a very simple file to show you what a PHP file looks like. Copy the following code into the file: /var/www/info.php . You will need to be root to save the program in that directory. Once you have the program entered enter this URL in you web browser. http://localhost/info.php to see the output.

      <?php
      phpinfo();
      ?>

This will display the information about the php program options included in the application. It will also list the configuration used. Now that is a lot more information than you probably want but it gives you some ideas.

If you would like to learn more about php I have two sources for you. The first one is from wikibooks PHP Programming. This is a book discussing PHP and how to use it. The second source is from the Wikiversity at Topic:PHP. This is a whole course on using PHP.

Now for a little fun, I want you to install some programs from the Ubuntu software center. They are fortune and the following list dwww, debtags, doc-debian, dpkg-www, antiword, tagcoll, grep-dctrl, man2html. The second install we will discuss later.

The fortune program is a simple fortune cookie program. Every time you execute it, it selects a fortune cookie from it’s database and displays it to you. I have my login set to give me a fortune cookie every time I log in. But fortune is a command line program, and most of you probably don’t use the command line much. So we are going to use PHP to make a web page which gives you a fortune cookie.

This time I want you to type the following code into the file: /var/www/fortune.php . Once you have the script saved, execute it to see what you get.

      <?php
      echo '<p> Todays Forturne Cookie says: </p>' ;
      echo '<hr /><pre>' ;

      system( '/usr/games/fortune' );

      echo '</pre><hr />' ;

      ?>

You can get another fortune cookie by just refreshing the web page. Each time it is executed it will find another cookie. That is the beauty of server side scripts.

Debian Debian Logo

Ubuntu Documentation on Localhost

We have talked repeatedly in this group about documentation. But have you ever explored the documentation available on your Linux system? It is truly amazing when you find it. I have often wondered if anyone ever created a tool to locate and present the documentation that comes with your Linux distribution. Well it seems that someone did.

Here is the description of this tool.

A typical Linux system has documentation in many formats (manual pages, info files, READMEs, and so on). dwww makes it possible to access all of these via the same interface, a WWW browser. This makes it easier to use the documentation.

— dwww documentation

The tool is available locally at Debian Documentation

I don’t think this tool needs much more of an explanation. It is using the type of web applications we discussed before.

Troubleshooters.com

This is just a quick plug for a web site I find interesting. Steve Litt has create a really interesting web site and written extensively about Linux. Please explore the web site Troubleshooters.Com.

One particularly interesting section is the Linux Library where he discusses a considerable list of topics.


Written by John F. Moore

Last Revised: Sun Nov 6 14:01:16 EST 2016

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
HTML5 Powered with CSS3 / Styling, and Semantics