Linux:Powerful Server Administration
上QQ阅读APP看书,第一时间看更新

Installing and configuring the Apache web server

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.

Getting ready

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.

How to do it…

Follow these steps to install and configure the Apache web server:

  1. Install Apache2 from the Ubuntu package repository:
    $ sudo apt-get update
    $ sudo apt-get install apache2
    
  2. Check if Apache2 has installed successfully. The command wget should download the index.html file:
    $ wget 127.0.0.1
    
  3. You can also open a browser on a local machine and point it to the server IP address. You should see a default It works! page customized for Ubuntu:
  4. Now, let's proceed with creating our first virtual host. First create a directory structure. Change the directory to /var/www/ and create a new directory for the contents of our site:
    $ cd /var/www
    $ sudo mkdir example.com
    
  5. Change the ownership and group of the directory example.com:
    $ sudo chown ubuntu:www-data example.com
    
  6. Set the file permissions to secure web contents:
    $ sudo chmod 750 example.com
    
  7. Create the required directories under the example.com directory:
    $ cd example.com
    $ mkdir public_html
    
  8. Create a index.html file under the public_html directory:
    $ echo '<b>Hello World ...</b>' > public_html/index.html
    
  9. Next, we need to set up a new virtual host under the Apache configuration.
  10. Copy the default Virtual Host file under /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
    
  11. Edit example.com.conf to match it with the following example. Change the parameters as per your requirements:
  12. Save the changes and exit example.com.conf.
  13. If you are using the same port as the default VirtualHost, do not forget to disable the default one:
    $ sudo a2dissite 000-default.conf
    
  14. Finally, enable our new VirtualHost with a2ensite and reload Apache:
    $ sudo a2ensite example.com.conf
    $ sudo service apache2 reload
    
  15. Start your browser and point it to the domain or IP address of your server:

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

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: