In this recipe, we will simply install the Apache web server from the Ubuntu package repository. We will also look at the basic configuration options and set up our first web page.
You will need access to a root account or an account with sudo
privileges.
I will be using Apache to refer to the Apache web server. The Apache web server is the most popular project by the Apache Foundation and is generally known as just Apache.
Follow these steps to install and configure the Apache web server:
$ sudo apt-get update $ sudo apt-get install apache2
wget
should download the index.html
file:$ wget 127.0.0.1
/var/www/
and create a new directory for the contents of our site:$ cd /var/www $ sudo mkdir example.com
example.com
:$ sudo chown ubuntu:www-data example.com
$ sudo chmod 750 example.com
example.com
directory:$ cd example.com $ mkdir public_html
index.html
file under the public_html
directory:$ echo '<b>Hello World ...</b>' > public_html/index.html
/etc/apache2/sites-available
and use it as a starting point for our configuration:$ cd /etc/apache2/sites-available $ sudo cp 000-default.conf example.com.conf
example.com.conf
to match it with the following example. Change the parameters as per your requirements:example.com.conf
.VirtualHost
, do not forget to disable the default one:$ sudo a2dissite 000-default.conf
VirtualHost
with a2ensite
and reload Apache:$ sudo a2ensite example.com.conf $ sudo service apache2 reload
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-available
, mods-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-enabled
directory 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.
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:
conf-available
directory:$ sudo vi /etc/apache2/conf-available/fqdn.conf
ServerName localhost
$ sudo a2enconf fqdn
$ sudo service apache2 reload
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.
You can read more by following the links: