How to Install and configure Apache web server on Ubuntu 13.10

By | May 25, 2023

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/

Start/Stop apache

To start or stop apache web server from command line, use the following commands.

# Start web server
$ sudo service apache2 start

# Stop the web server
$ sudo service apache2 stop

# Restart the web server
$ sudo service apache2 restart

# Reload configuration without restarting
$ sudo service apache2 reload

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"
.....

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

Fix the error message

Apache by default gives the following error message

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

To fix this, set the ServerName variable to your hostname or fully qualified domain name in the apache configuration file

$ sudo nano /etc/apache2/apache2.conf

Add the following line to the end

ServerName yourhostname

Reload Apache configuration

$ sudo service apache2 reload

Change web root directory

To change the web root directory, 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 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>
...
        #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

...
</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. Newer version of Apache (2.3+) require the "Require" directive to allow access.

The final 000-default.conf file should look like this

<VirtualHost *:80>
...
        #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

...

         # 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. It will create the necessary symlinks in the "sites-enabled" directory.

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.

The final configuration could look like this -

<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!!

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

7 Comments

How to Install and configure Apache web server on Ubuntu 13.10
  1. Tosyn

    Thanks, this tut worked for me (adding: Require all granted within Directory tag) after upgrading from mint16 to mint17 which came with Apache 2.4.7.

  2. 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

  3. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *