Main.InstallingApacheOnUbuntu


<< Installing Ubuntu 7 | Tutorials | Installing PmWiki on Ubuntu >>

Installing the Apache Web Server on Ubuntu


The Apache server is mature, battle-tested HTTP server used on majority of Internet sites today. It is free and open-source software that enables secure, fast serving of all kinds of data.

This guide will take you through the steps of installing a copy of Apache on your Ubuntu 7.10 machine.

Requirements


You will need:

  • An Ubuntu computer. I am using Ubuntu 7.10 - but the process should not be vastly different for other versions.
  • A working Internet connection.
  • The "apt" package management system, which comes by default on all Ubuntu setups.
  • Access to a command console and administrator priveledges.

Download Apache


The latest version of Apache as of writing is version 2 (or "apache2"). Open up a command console and enter the following:

	sudo apt-get install apache2

After pressing <Enter>, giving your password and then confirming the download, the apt-get program will retrieve, install, preconfigure and actually start the apache web server software with no further involvement from you. The power of repository-based package management in action!

Check the server is running


At the command console, enter the following to list running processes on your machine:

	ps -Al

"ps" stands for "process snapshot". It returns a momentary glimpse of running programs and services at the current time. The "-Al" switch asks for a "long-format" listing of ALL processes, not just the processes running under the current session or user.

You should see around 4 processes called "apache" (probably towards the end of the list). These are the running server programs. From now on, Apache should start up when the computer boots.

If you try to open a web-browser and enter the address "http://localhost", you should see either a directory listing with one item, "apache2-default", or a blank page with just the words "It Works!" letting you know that all is well.

If you get error messages about not being able to find a host or page - something went wrong. Double check the messages you got from apt.

To do a basic test, create a file called "index.html" in the "/var/www/" directory (which is created during the installation of apache). You can do this with the following command:

	sudo gedit /var/www/index.html &"

You will probably have to enter the administrator password to continue after pressing <Enter>.

The above command breaks down like so:

  • The "sudo" part requests administrator rights when performing the file creation.
  • The "gedit" part calls up the standard GNOME text editor called gedit (which is available on all standard Ubuntu installations.
  • The "/var/www/index.html" part names the file you are about to edit and create.
  • The "&" sign at the end request that the editing process not block further work at the commandline.

Once the text editor opens up, edit the new file to contain the following:

	<html>
		<head>
			<title>My test page</title>
		</head>
		<body>
			<p>Hello - my first test html page</p>
		</body>
	</html>

Then refresh your browser (make sure it's still pointed at "http://localhost") and you should see the contents of the <p> tags shown in the browser window.

	Hello - my first test html page

You have successfully installed the Apache web server and served a webpage! Well done. Although, things are not quite ready yet for a production server setup. Read on...

Server Control


Here are some tips detailing how to manipulate your new server installation:

The Apache server is configured primarily through a single (well documented) text file. It is called "apache2.conf" and it is found in "etc/apache2/". A large percentage of Unix programs are configured and controlled by scripts and files within the "/etc" directory heirarchy. You can edit this file by issuing the following command:

	sudo gedit /etc/apache2/apache2.conf

You may need to enter your password to continue. The configuration file contains options following this form:

	someoption somevalue

as well as configuration groups or directives that look like this:

	<Something>
		someoption somevalue
		anotheroption anothervalue
	</Something>

Each is very well documented and set up with useful defaults. Note that lines starting with "#" are comments and are ignored by Apache when the file is read.

Starting and Stopping the Server


On Ubuntu it is possible to start, stop and restart the server using the scripts in the "/etc/apache2" directory. If you make changes to the apache2.conf file, you must restart the server for the changes to take effect.

Starting apache (if it is not already running):

	sudo /etc/init.d/apache2 start

Stopping apache (if it is currently running):

	sudo /etc/init.d/apache2 stop

Restarting apache (if it is currently running):

	sudo /etc/init.d/apache2 restart

On non-Ubuntu Linux machines, a control program called "apache2ctl" performs a similar function. The /etc/init.d/apache2 script on Ubuntu is actually a simple wrapper for apache2ctl.

Serving dynamic pages with PHP


PHP is a powerful, ubiquitous scripting language that can be run within a web browser, allowing it to perform specialized tasks and serve dynamic web pages and even run on-line applications like GMail, Wikipedia or Web Forums. If you are going to continue on with installing things like Wiki or forum/bulletin board software, you most likely will need PHP to be installed into your Apache. Install it by running the following command through your command console:

	sudo apt-get install php5

You might need to enter your password and confirm the download. After that installation and basic setup should complete automatically.

Next, you need to inform Apache that it must process any files with names ending in ".php" as actual PHP scripts, rather than simple text files. This is a very important step. If left out, important and sensitive scripting code could be downloadable from your server by anyone.

You will need to edit the apache configuration file to add .php files as script types:

	sudo gedit /etc/apache2/apache2.conf &

After entering your password, the "gedit" text editor should load up with the apache config file ready to be edited. Add the following line in a place that makes sense to you (I like to put it near the <Files> or AccessFileName directives).

	AddType application/x-httpd-php .php .phtml

This lines means that files ending in ".php" and .phtml" are to be interpreted as PHP application scripts.

Save the file, quit the text editor and restart the web server to apply this change.

	sudo /etc/init.d/apache2 restart

Now, if you place any PHP scripts within your public web accessible directories, Apache will run the code contained within when the pages are accessed, rather than simply serve the code itself, which might be a dire mistake.

Next up


For some possible next steps, see the following guides:

Standards:


To see the various web standards that Apache implements, see:

	http://httpd.apache.org/docs/2.2/misc/relevant_standards.html

Default Configuration


Prefork MPM

	MaxClients: 150

For production server,

	check the ServerTokens directive