Linux.com

Home Linux Community Community Blogs General Linux Install Apache web server on Ubuntu 13.10

Install Apache web server on Ubuntu 13.10

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

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.

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 : 3rd November 2013
Read more at Binary Tides
 

Comments

Subscribe to Comments Feed

Upcoming Linux Foundation Courses

  1. LFD320 Linux Kernel Internals and Debugging
    03 Nov » 07 Nov - Virtual
    Details
  2. LFS416 Linux Security
    03 Nov » 06 Nov - Virtual
    Details
  3. LFS426 Linux Performance Tuning
    10 Nov » 13 Nov - Virtual
    Details

View All Upcoming Courses


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board