Installing and configuring the Apache web server

Install Apache2 from the Ubuntu package repository:
$ sudo apt-get update
$ sudo apt-get install apache2


ubuntu@ubuntu:/etc/init.d$ cd /var/www/
ubuntu@ubuntu:/var/www$ sudo mkdir example.com
ubuntu@ubuntu:/var/www$ groups ubuntu
ubuntu : ubuntu adm cdrom sudo dip www-data plugdev lpadmin sambashare

Change the ownership and group of the directory example.com
ubuntu@ubuntu:/var/www$ sudo chown ubuntu:www-data example.com/
ubuntu@ubuntu:/var/www$ sudo chmod 750 example.com/
ubuntu@ubuntu:/var/www$ cd example.com/
ubuntu@ubuntu:/var/www/example.com$ mkdir pub_html
ubuntu@ubuntu:/var/www/example.com$ echo '<b>Hello World</b>' > pub_html/index.html
ubuntu@ubuntu:/var/www/example.com$ cd /etc/apache2/sites-
sites-available/ sites-enabled/   
ubuntu@ubuntu:/var/www/example.com$ cd /etc/apache2/sites-available/

Copy the default Virtual Host file under
ubuntu@ubuntu:/etc/apache2/sites-available$ sudo cp 000-default.conf example.com.conf
ubuntu@ubuntu:/etc/apache2/sites-available$ sudo vi example.com.conf 
ubuntu@ubuntu:/etc/apache2/sites-available$ cat example.com.conf 
<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 example.com
ServerAlias www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www/example.com/pub_html

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

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
ubuntu@ubuntu:/etc/apache2/sites-available$ sudo a2dis
a2disconf  a2dismod   a2dissite  
ubuntu@ubuntu:/etc/apache2/sites-available$ sudo a2dissite 000-default.conf 
Site 000-default disabled.
To activate the new configuration, you need to run:
  service apache2 reload
ubuntu@ubuntu:/etc/apache2/sites-available$ sudo a2en
a2enconf  a2enmod   a2ensite  
ubuntu@ubuntu:/etc/apache2/sites-available$ sudo a2ensite 
000-default.conf  default-ssl.conf  example.com.conf  
ubuntu@ubuntu:/etc/apache2/sites-available$ sudo a2ensite example.com.conf 
Enabling site example.com.
To activate the new configuration, you need to run:
  service apache2 reload
ubuntu@ubuntu:/etc/apache2/sites-available$ sudo service apache2 reload
 * Reloading web server apache2                                                                * 
ubuntu@ubuntu:/etc/apache2/sites-available$ curl 127.0.0.1
<b>Hello World</b>


ubuntu@ubuntu:/etc/apache2/conf-available$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 ubuntu
192.168.40.128 example.com

ubuntu@ubuntu:/etc/apache2/conf-available$ curl example.com
<b>Hello World</b>

How it works…

The Apache package for Ubuntu is included in the default package repository. We need a single command to install the Apache web server. Installation creates a structure of configuration files under /etc/apache2 and a sample web page under /var/www/html.
As mentioned in the default It works! page, Apache2 does not use a single configuration file such as httpd.conf in older versions, but rather separates its configuration across multiple configuration files. These files are named after their respective uses. apache2.conf is now a main configuration file and creates a central configuration by including all other files.
conf-availablemods-available, and sites-available contain configuration snippets and other files for global configurations, modules, and virtual hosts respectively. These configurations are selectively activated under their enabled counterparts with symbolic links for each configuration to be enabled.
envvars contains all environment variables and default values for Apache to work.
ports.conf defines the ports Apache should listen on.
The default web page is created under the /var/www/html directory.
In this recipe, we have created our virtual host for the domain name example.com and hosted it under the directory /var/www/example.com. Next, we have to change the owner and default group of this directory to the user, ubuntu and group, www-data. This grants full access to the user ubuntu and allows read and execute access to the group www-data. If you have observed the contents of the envvars file, you may have noticed that the variable APACHE_RUN_GROUP is set to www-data. This means Apache process will be started as the group www-data. By setting a default group, we have allowed Apache process to read the contents of the example.com directory. We have also enabled write access to the logs directory so that Apache processes can log to this directory.
After creating the virtual host configuration and setting the respective options, all we need to do is enable a new virtual host or site. Apache2 provides the respective commands to enable or disable configurations, modules, and sites. a2ensite will be used to enable the site from options available under sites-available. Basically, this will create a symbolic link under the sites-enableddirectory to a specified site configuration. Similarly, a2dissite will disable the site by removing the symbolic link from the sites-enabled directory. Similar commands are available to work with configurations and modules.

There's more…

You may want to get rid of the warning that says Could not reliably determine the server's fully qualified domain name. This warning appears because the Apache process could not find the default FQDN for this server. You can set the default FQDN simply by creating a new configuration file and then enabling this new configuration:
  1. Create a new file under the conf-available directory:
    $ sudo vi /etc/apache2/conf-available/fqdn.conf
    
  2. Add a server name variable to this file:
    ServerName  localhost
    
  3. Save the changes and enable this configuration:
    $ sudo a2enconf fqdn
    
  4. Reload the Apache server:
    $ sudo service apache2 reload
    

HTTP VERSION 2 SUPPORT

If you are looking for HTTP2 support, Apache does provide a separate module for that. Apache version 2.4.17 ships with a module, mod_http2, that implements the latest HTTP version, HTTP2. It is still an experimental implementation and needs to be enabled manually. This version of Apache (2.4.17) is available with Ubuntu Xenial (16.04) in the default package repository. If you are using Ubuntu 14.04, you can use the external repository as follows:
$ sudo add-apt-repository -y ppa:ondrej/apache2
Once the required version of Apache is installed, you can enable mod_http2 as follows:
$ sudo a2enmod http2
Next, edit the specific virtual host file to enable the HTTP2 protocol for a specific site. Note that you need to configure your site to use an SSL/TLS connection:
<VirtualHost *:443>
    Protocols h2 http/1.1
    ...
</VirtualHost>
Finally, restart your Apache server:
$ sudo service apache2 restart
H2O, the new name in web servers, is developed around the HTTP2 protocol. It does support both HTTP 1.1 and a stable implementation of the HTTP2 protocol. You may want to check this out as your local or development server.

See also

You can read more by following the links:

Comments

Popular posts from this blog

HAproxy logging

tomcat catalina coyote jasper cluster

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all