Linux web server setup - Install and configure Apache2 in Ubuntu

Apache server is an open source web server or HTTP server widely used not only in Linux and Unix-based operating system but nowadays in Windows as well. Apache is known for it's stability and highly configurable with support of many add on modules for customization. In Linux, Apache, PHP and MySql work greats together and always be a perfect web server solution for many organizations around the world.

Ubuntu web server installation

We must first check apache2 package in Ubuntu to make sure it's already installed using dpkg and ps command. Here are dpkg and ps command usage examples:

[email protected]:~$ sudo dpkg -l | grep apache
[sudo] password for luzar:
ii apache2 2.2.8-1ubuntu0.3 Next generation, scalable, extendable web server
ii apache2-mpm-prefork 2.2.8-1ubuntu0.3 Traditional model for Apache HTTPD
ii apache2-utils 2.2.8-1ubuntu0.3 utility programs for webservers
ii apache2.2-common 2.2.8-1ubuntu0.3 Next generation, scalable,
extendable web server
ii libapache2-mod-php5 5.2.4-2ubuntu5.4 server-side, HTML-embedded
scripting language
[email protected]:~$

In the example above, we use | (pipe) grep with dpkg command to grab all apache package in our system. If we just use dpkg -l apache, dpkg won't find apache unless we specified the exact name, which is apache2.

This is an example of how to use ps command to check apache running service:

[email protected]:~$ ps aux | grep apache
root 4594 0.0 1.3 20460 6748 ? Ss Dec10 0:01 /usr/sbin/apache2 -k start
www-data 5247 0.0 0.6 20460 3376 ? S Dec10 0:00 /usr/sbin/apache2 -k start
www-data 5248 0.0 0.6 20460 3376 ? S Dec10 0:00 /usr/sbin/apache2 -k start
www-data 5249 0.0 0.6 20460 3376 ? S Dec10 0:00 /usr/sbin/apache2 -k start
www-data 5253 0.0 0.6 20460 3376 ? S Dec10 0:00 /usr/sbin/apache2 -k start
www-data 5255 0.0 0.6 20460 3376 ? S Dec10 0:00 /usr/sbin/apache2 -k start
luzar 8725 0.0 0.1 3004 752 pts/0 R+ 02:32 0:00 grep apache
[email protected]:~$

If you don't have results like the example above, that means apache has not been installed in your system. You can install apache using apt-get command like an example below:

[email protected]:~$ sudo apt-get install apache2
Reading package lists... Done
Building dependency tree
Reading state information... Done
...
...

Apache2 start, stop and restart service

Now that apache2 already installed in your system, it's time to start the service. Here is the command to start apache2 service:

[email protected]:~$ sudo /etc/init.d/apache2 start
[sudo] password for luzar:
* Starting web server apache2 [ OK ]

Here is a command to stop apache2 service:

[email protected]:~$ sudo /etc/init.d/apache2 stop
[sudo] password for luzar:
* Stopping web server apache2 [ OK ]

Here is a command to restart apache2 service:

[email protected]:~$ sudo /etc/init.d/apache2 restart
* Restarting web server apache2 [ OK ]

Starting and stopping apache2 service is important when you are testing apache2 configuration and troubleshooting. All changes you made to the configuration file only be affected once you restart apache2 service.

At this point, you probably don't have any problem starting apache2 service. The apache2 still using default configuration. You can test your new web server using lynx web browser to open localhost default website in Ubuntu command line terminal. If you don't have lynx in your system, check lynx tutorial for installation and usage guide. Here is an example of a default apache2 website opened using lynx web browser:

[email protected]:~$ lynx localhost

Example of testing apache2 web server using lynx screenshot

Ubuntu apache2 configuration

All Apache2 configuration files are located in /etc/apache2 in Ubuntu. Other than apache2 main configuration file, apache2.conf, there are many other files and directory included in the package. In the past, httpd.conf is the apache main configuration file. Don't get confused. Ubuntu has made apache2 configuration easier though you might see it complicated. Here are the complete list of files and directories in apache2 directory:

[email protected]:/etc/apache2$ ls -l
total 40
-rw-r--r-- 1 root root 10587 2008-06-25 09:49 apache2.conf
drwxr-xr-x 2 root root 4096 2008-10-19 15:17 conf.d
-rw-r--r-- 1 root root 378 2008-06-25 09:49 envvars
-rw-r--r-- 1 root root 0 2008-10-19 15:17 httpd.conf
drwxr-xr-x 2 root root 4096 2008-12-11 20:46 mods-available
drwxr-xr-x 2 root root 4096 2008-10-19 15:17 mods-enabled
-rw-r--r-- 1 root root 59 2008-06-25 09:49 ports.conf
drwxr-xr-x 2 root root 4096 2008-10-19 15:17 sites-available
drwxr-xr-x 2 root root 4096 2008-10-19 15:17 sites-enabled
[email protected]:/etc/apache2$

Here are default configuration and brief explanation of files and directories above:

/etc/apache2/apache2.conf

Apache2 main configuration file.


### Section 1: Global Environment
#
# The directives in this section affect the overall operation of Apache,
# such as the number of concurrent requests it can handle or where it
# can find its configuration files.
#

#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# NOTE! If you intend to place this on an NFS (or otherwise network)
# mounted filesystem then please read the LockFile documentation (available
# at <URL:http://httpd.apache.org/docs-2.1/mod/mpm_common.html#lockfile>);
# you will save yourself a lot of trouble.
#
# Do NOT add a slash at the end of the directory path.
#
ServerRoot "/etc/apache2"

#
# The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
#
...
...

/etc/apache2/httpd.conf

Add additional configuration parameters. By default, this file is empty.

/etc/apache2/envvars

Environment variables to tune the operation of Apache server.

# envvars - default environment variables for apache2ctl

# Since there is no sane way to get the parsed apache2 config in scripts, some
# settings are defined via environment variables and then used in apache2ctl,
# /etc/init.d/apache2, /etc/logrotate.d/apache2, etc.
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_PID_FILE=/var/run/apache2.pid

/etc/apache2/ports.conf

Port numbers that the Apache server will listen on.

Listen 80

<IfModule mod_ssl.c>
Listen 443
</IfModule>

/etc/apache2/conf.d/

Put additional Apache configuration files in this directory. By default this directory only contains an example additional apache configuration file named charset . Here is charset default configuration.

# Read the documentation before enabling AddDefaultCharset.
# In general, it is only a good idea if you know that all your files
# have this encoding. It will override any encoding given in the files
# in meta http-equiv or xml encoding tags.

#AddDefaultCharset UTF-8

/etc/apache2/mods-available/

Contains all the modules installed for your server. This is the default modules came with Ubuntu 8.04 server. For additional modules, visit apache modules page in apache official website. Here are example of modules in /mod-available directory:

[email protected]:/etc/apache2$ ls mods-available/
actions.conf authz_owner.load dir.load mem_cache.load setenvif.load
actions.load authz_user.load disk_cache.conf mime.conf speling.load
alias.conf autoindex.conf disk_cache.load mime.load ssl.conf
alias.load autoindex.load dump_io.load mime_magic.conf ssl.load
asis.load cache.load env.load mime_magic.load status.conf
auth_basic.load cern_meta.load expires.load negotiation.conf status.load
auth_digest.load cgid.conf ext_filter.load negotiation.load substitute.load
authn_alias.load cgid.load file_cache.load php5.conf suexec.load
authn_anon.load cgi.load filter.load php5.load unique_id.load
authn_dbd.load charset_lite.load headers.load proxy_ajp.load userdir.conf
authn_dbm.load dav_fs.conf ident.load proxy_balancer.load userdir.load
authn_default.load dav_fs.load imagemap.load proxy.conf usertrack.load
authn_file.load dav.load include.load proxy_connect.load version.load
authnz_ldap.load dav_lock.load info.conf proxy_ftp.load vhost_alias.load
authz_dbm.load dbd.load info.load proxy_http.load
authz_default.load deflate.conf ldap.load proxy.load
authz_groupfile.load deflate.load log_forensic.load rewrite.load
authz_host.load dir.conf mem_cache.conf setenvif.conf

/etc/apache2/mods-enabled/

As you can see from mod-available above, there are 86 modules available by default. They are not enabled yet. Create a symbolic link in this directory that refers to the module file in /mods-available above to enable it. Below are the default enabled modules:

[email protected]:/etc/apache2$ ls -l mods-enabled/
total 0
lrwxrwxrwx 1 root root 28 2008-10-19 15:17 alias.conf -> ../mods-available/alias.conf
lrwxrwxrwx 1 root root 28 2008-10-19 15:17 alias.load -> ../mods-available/alias.load
lrwxrwxrwx 1 root root 33 2008-10-19 15:17 auth_basic.load -> ../mods-available/auth_basic.load
lrwxrwxrwx 1 root root 33 2008-10-19 15:17 authn_file.load -> ../mods-available/authn_file.load
lrwxrwxrwx 1 root root 36 2008-10-19 15:17 authz_default.load -> ../mods-available/authz_default.load
lrwxrwxrwx 1 root root 38 2008-10-19 15:17 authz_groupfile.load -> ../mods-available/authz_groupfile.load
lrwxrwxrwx 1 root root 33 2008-10-19 15:17 authz_host.load -> ../mods-available/authz_host.load
lrwxrwxrwx 1 root root 33 2008-10-19 15:17 authz_user.load -> ../mods-available/authz_user.load
lrwxrwxrwx 1 root root 32 2008-10-19 15:17 autoindex.conf -> ../mods-available/autoindex.conf
lrwxrwxrwx 1 root root 32 2008-10-19 15:17 autoindex.load -> ../mods-available/autoindex.load
lrwxrwxrwx 1 root root 26 2008-10-19 15:17 cgi.load -> ../mods-available/cgi.load
lrwxrwxrwx 1 root root 26 2008-10-19 15:17 dir.conf -> ../mods-available/dir.conf
lrwxrwxrwx 1 root root 26 2008-10-19 15:17 dir.load -> ../mods-available/dir.load
lrwxrwxrwx 1 root root 26 2008-10-19 15:17 env.load -> ../mods-available/env.load
lrwxrwxrwx 1 root root 27 2008-10-19 15:17 mime.conf -> ../mods-available/mime.conf
lrwxrwxrwx 1 root root 27 2008-10-19 15:17 mime.load -> ../mods-available/mime.load
lrwxrwxrwx 1 root root 34 2008-10-19 15:17 negotiation.conf -> ../mods-available/negotiation.conf
lrwxrwxrwx 1 root root 34 2008-10-19 15:17 negotiation.load -> ../mods-available/negotiation.load
lrwxrwxrwx 1 root root 27 2008-10-19 15:17 php5.conf -> ../mods-available/php5.conf
lrwxrwxrwx 1 root root 27 2008-10-19 15:17 php5.load -> ../mods-available/php5.load
lrwxrwxrwx 1 root root 31 2008-10-19 15:17 setenvif.conf -> ../mods-available/setenvif.conf
lrwxrwxrwx 1 root root 31 2008-10-19 15:17 setenvif.load -> ../mods-available/setenvif.load
lrwxrwxrwx 1 root root 29 2008-10-19 15:17 status.conf -> ../mods-available/status.conf
lrwxrwxrwx 1 root root 29 2008-10-19 15:17 status.load -> ../mods-available/status.load

Here is an example to enable another module:

[email protected]:/etc/apache2$ cd mods-enabled/
[email protected]:/etc/apache2/mods-enabled$ ln -s ../mods-available/rewrite.load
rewrite.load

[sudo] password for luzar:
[email protected]:/etc/apache2/mods-enabled$ ls -l |grep rewrite.load
lrwxrwxrwx 1 root root 30 2008-12-12 03:37 rewrite.load ->
../mods-available/rewrite.load

To disable a module, just delete the symbolic link.

/etc/apache2/sites-available/

Stores all the configuration files for the web sites serviced by Apache server. By default, only one file available, a default virtual host configuration file:

NameVirtualHost *
<VirtualHost * >
ServerAdmin [email protected]

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
ServerSignature On

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>

/etc/apache2/sites-enabled/

Create a symbolic link to enable sites in /etc/apache2/sites-available.

As you can see from the testing web server example previously, the default apache2 configuration is already working. Perhaps you want to change document root directory from /var/www to /srv/www because you want to keep certain server in a dedicated directory. Perhaps also because you want to avoid future problems where logs files taking all the available space. You can change the document root directory by editing /sites-available/default file configuration like an example below:

First you make a backup of the original file:

[email protected]:/etc/apache2/sites-available$ sudo cp default default.ori
[sudo] password for luzar:
[email protected]:/etc/apache2/sites-available$ ls
default default.ori

Open /sites-available/default file with text editor and change document root directory like the example below:

[email protected]:/etc/apache2/sites-available$ sudo vim default
NameVirtualHost *
<VirtualHost *>
ServerAdmin [email protected]

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

Then, copy the /var/www directory to /srv/ directory:

[email protected]:~$ sudo cp -r /var/www /srv/www

Create a new html file to test new document root directory. For example, I created a file named test.html in /srv/www directory. Here is the content:


<html>
<head>
<title>My testing website</title>
</head>

<body>
<h1>This is a testing website</h1>
<p>It works alright!</p>
</body>
</html>

Now we can restart apache2 service again before we test the new configuration. Here is the command:

[email protected]:~$ sudo /etc/init.d/apache2 restart

Finally we can test our new configuration. Here is the command:

[email protected]:~$ lynx localhost/test.html

Here is the result:

New document root testing website screenshot

So our apache2 web server works fine. We can now put our website in /srv/www directory.

Add new comment