Install Apache web server on Ubuntu 13.10

8 Flares Filament.io 8 Flares ×

Install Apache Web server

A while back I updated my ubuntu to 13.10 and then the apache php installation got messed up. So I had to reinstall it quickly to continue working on my php projects.

Apache is there in the ubuntu repositories so can be installed without much effort. Here is the quick command you need to fire at the terminal.

$ sudo apt-get install apache2

Apache by default configures itself quickly so that you can open up it from the browser with the localhost url

http://localhost/

The default web root directory is /var/www. So whatever files are put in this directory are accessible from the localhost url. Later we shall check how to change the default web root directory

To check what version of apache is installed use the apache2 command with the v option/

$ apache2 -v
Server version: Apache/2.4.6 (Ubuntu)
Server built:   Aug  9 2013 14:28:56

Locate configuration files

To get more information about how exactly apache is configure on your system, use the apache2ctl command.

$ apache2ctl -V
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Server version: Apache/2.4.6 (Ubuntu)
Server built:   Aug  9 2013 14:28:56
Server's Module Magic Number: 20120211:23
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/apache2"
 -D SUEXEC_BIN="/usr/lib/apache2/suexec"
 -D DEFAULT_PIDLOG="/var/run/apache2.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"
enlightened@enlightened:~$

It tells the name of the configuration file, the server mpm being used and lots of other details. These are useful when configuring apache further.

The main configuration file is located at /etc/apache2/apache2.conf
Just prepend the HTTPD_ROOT with SERVER_CONFIG_FILE to get the actual location of the configuration file.







Or there is another option S which also reports various configuration details about apache

$ apache2ctl -S
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server localhost (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost localhost (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost localhost (/etc/apache2/sites-enabled/000-default.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/lock/apache2" mechanism=fcntl 
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used

All that information is quite useful when setting up apache. Saves time which would otherwise be spend on guessing.

There are lots of configuration files involved with apache. The main configuration file is "apache2.conf" as mentioned above. This configuration file has instructions to load further configuration files along. Here is the line that does it.

# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf

# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf

Change web root directory

To change the web root, we need to change the setting in the sites-enabled configuration files. As a standard practice a separate configuration file is created inside sites-enabled directory for each vhost or virtual host. A virtual host is a domain. So you can have multiple domains served by apache. In this example however we just use the default configuration file.

There should be a file called 000-default.conf inside the /etc/apache2/sites-enabled directory. If its not there then copy it from /etc/apache2/sites-available. The file looks like this initially.

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Change the DocumentRoot path to the new web root that you want to use. Save the file and restart apache.

$ sudo service apache2 restart

Now try accessing localhost. If it does not work, then check the error log located at

$ cat /var/log/apache2/error.log

As of Apache 2.4.3 there is a security feature that prevents apache from accessing other directories. And then the localhost url would show "Forbidden" error message and the error log would contain a message like this

[Sun Nov 03 11:25:24.521491 2013] [authz_core:error] [pid 6950] [client 127.0.0.1:41078] AH01630: client denied by server configuration: /var/www/phpinfo.php

The error means that apache is not able to access the web root directory due to some configuration. To fix this, add the following to the vhost configuration

<Directory "/var/www2">
   Order allow,deny
   Allow from all
   # New directive needed in Apache 2.4.3: 
   Require all granted
</Directory>

The "Require" directive fixes the problem. The final 000-default.conf file should look like this

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www2

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf

         # New directive needed in Apache 2.4.3: 
          <Directory "/var/www2">
   Order allow,deny
   Allow from all
   # New directive needed in Apache 2.4.3: 
   Require all granted
</Directory>
</VirtualHost>

Now restart apache again and the new web root directory should finally work.

Adding more vhosts or domains

To add more virtual hosts just create more configuration files like 000-default.conf and name it anything but with a conf extension. Set a unique ServerName in each file and give each host a separate web root directory.

Alternatively create a configuration file in sites-available directory and use the a2ensite command to enable the site. Here is the explanation from the man pages

a2ensite  is a script that enables the specified site (which contains a
       <VirtualHost> block) within the apache2 configuration.  It does this by
       creating   symlinks   within   /etc/apache2/sites-enabled.    Likewise,
       a2dissite disables a site by removing those symlinks.   It  is  not  an
       error  to  enable  a  site  which is already enabled, or to disable one
       which is already disabled.

       The default site is handled specially: The resulting  symlink  will  be
       called 000-default in order to be loaded first.

So first create a configuration file by copying the default configuration file.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysite.conf

Edit the configuration file as needed. The important thing to change is the document root for the new site.
Then enable it using the a2ensite command

$ sudo a2ensite mysite

Restart apache. And the new site should work fine.

Enable htaccess file

The htaccess file allows to modify various apache configuration parameters on per directory basis at runtime. By default apache is not configured to use the htaccess file. To make it do so, simply add the following line in the Directory section shown above.

AllowOverride FileInfo

The above will tell apache to start reading htaccess files and parse the configuration instructions written in them.

<Directory "/var/www2">
   Order allow,deny
   Allow from all
   AllowOverride FileInfo
   # New directive needed in Apache 2.4.3: 
   Require all granted
</Directory>

After installing apache, you might want to install other things like php, mysql, phpmyadmin depending on what all you need. So go ahead and enjoy!!

Last Updated On : 4th November 2013

Subscribe to get updates delivered to your inbox

About Silver Moon

Php developer, blogger and Linux enthusiast. He can be reached at m00n.silv3r@gmail.com. Or find him on

  • Alex Kleinubing

    I try it on Ubuntu 14.04, apache 2.4.7, but i noticed Forbidden 403 access permission error…

    In log:
    [Sun Apr 27 20:27:58.461208 2014] [core:error] [pid 18004] (13)Permission denied: [client 127.0.0.1:56374] AH00035: access to /info.php denied (filesystem path ‘/media/alexandre/Dados’) because search permissions are missing on a component of the path

    what wrong? :S

  • Jignesh Jain

    thanks a lot :)

  • Ralph Rothschild

    I used your instructions but still got the 403 error. I thought it
    didn’t work but it did. However, there was this error in
    /var/log/apache2/error.log

    “[Thu Jan 09 19:45:34.843888 2014]
    [autoindex:error] [pid 12607] [client 127.0.0.1:37120] AH01276: Cannot
    serve directory /home/n3rve/www/: No matching DirectoryIndex
    (index.html,index.cgi,index.pl,index.php,index.xhtml,index.htm) found,
    and server-generated directory index forbidden by Options directive”

    My issue was that I didn’t have any files in the root of localhost, just directories.
    After snooping around, I found that there’s another file needing a change.

    /etc/apache2/apache2.conf

    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted

    “/home/n3rve/www”
    will most likely be “/var/www” on your computer, change it to the
    updated directory you saved in your 000-default.conf and restart apache.
    I prefer having my ‘www’ directory under my /home directory so it
    doesn’t get missing during an OS upgrade or re-install, considering that
    my /home directory sits on a different partition.

  • Vasyliovych Roman

    Just used this tut. Thanks

8 Flares Twitter 0 Facebook 3 Google+ 5 LinkedIn 0 StumbleUpon 0 Filament.io 8 Flares ×