Apache ssL proxy how to basic face

Apache2 installation and VirtualHost configuration under Ubuntu 14.04

Although the Apache web server is facing increasing competition, e.g. from Nginx ("Engine X"), it is still the most popular web server for Linux. This is not least due to the simple configuration. A web server including PHP and MySQL can be set up in a very short time - this makes Apache particularly suitable for beginners who need the web server for their first attempts with PHP, for example.

In a small series I want to explain how you can get a working web server with all possible extensions like PHP, MySQL and SSL step by step. The series starts here - with the basic configuration of Apache and the setup of VirtualHosts.

In order to be able to start with the configuration at all, the web server has to be installed first. In addition, the following steps are carried out as the root user.

sudo -s apt-get update && apt-get install apache2

After the installation, the web server is started for the first time. The sample page can then be viewed through a web browser by entering the server's IP address. But now to the configuration ...

VirtualHosts make the web server flexible ...

An Apache web server can consist of one or more so-called VirtualHosts. These hosts can be viewed like other small, virtual “sub-servers”. Assume a web server with a certain IP should serve several domains or subdomains: example.de, keine-ahnung.de and test.keine-ahnung.de. ... then a VirtualHost would have to be created in the configuration for each of the three domains.

A basic VirtualHost configuration looks like this:

<VirtualHost *:80> ServerName beispiel.de DocumentRoot /var/www/beispiel.de/beispiel.de </VirtualHost>

The settings for each VirtualHost are written between a "" pair. The first line also specifies the port for which the defined VirtualHost is to be responsible. In the example, the VirtualHost only listens to connections that reach the web server via port 80 (standard HTTP). For example, it does not accept requests via SSL (port 443).

Then the domain for which the VirtualHost is to be responsible is defined with ServerName. In the example he is only responsible for example.de. DocumentRoot defines the root directory in which the HTML or PHP files are located. In this directory there should be an index.html or an index.php file. This is then automatically loaded when calling up example.de and displayed to the user.

VirtualHost directives can contain many other settings that will not be discussed further at first.

The VirtualHost definitions are written in configuration files in / etc / apache2 / sites-available / and loaded from the web server on request. Why on request? It is common not to pack all VH definitions into a single file, but rather to store them in different files depending on the domain. For example.de a file /etc/apache2/sites-available/beispiel.de.conf is created and for the two VHs for “keine-ahnung.de” a file “keine-aahme.de.conf”. Each domain gets its VirtualHosts written in its own file. The definitions for the domain itself and its subdomains (test.keine-ahnung.de) are then collected in this file. In this way you can keep order and have the option to switch certain domains inactive.

The files in / etc / apache2 / sites-available / are actually ignored by the web server. The files are only activated when they are in / etc / apache2 / sites-enabled / or have been linked there. For this reason, newly created VH configuration files must first be activated before the web server reads them. Then they are linked in sites-enabled and are thus active.

Simplify the existing VH

In / etc / apache2 / sites-available there is already a configuration file “000-default.conf”, which is already active. When the IP of the web server is called up in the web browser, it becomes active because it defines a VirtualHost to which no server name has been assigned. In this way, the VH becomes active when no one else is (default / fallback). For all other VHs, however, a server name must be specified!

The file already contains a lot of content, which for the sake of simplicity is deleted and replaced by a new configuration:

> /etc/apache2/sites-available/000-default.conf nano /etc/apache2/sites-available/000-default.conf

New content of the file:

<VirtualHost *:80> DocumentRoot /var/www/html </VirtualHost>

This VirtualHost still points to the demo page that appears when the server IP address is called - only comment lines have been removed so that the configuration looks easier.

Create a new VirtualHost

Now a new VirtualHost is to be created for a specific domain. I assume that you own a domain that already points to the IP address of your web server. A separate folder is first created for the domain within the web server directory / var / www:

mkdir /var/www/beispiel.de

You can now put an example page in this folder, which should be displayed later.

Next, a new, suitable VirtualHost is required, which responds to the domain and loads the index.html in the folder that has just been created. For this purpose, a new file "example.de" is created in / etc / apache2 / sites-available:

nano /etc/apache2/sites-available/beispiel.de.conf

... and entered the following configuration:

<VirtualHost *:80> ServerName beispiel.de DocumentRoot /var/www/beispiel.de </VirtualHost>

In order to push the new configuration under the web server, two further steps are required: First, the configuration for example.de must be activated:

a2ensite example.de.conf

... and then the configurations are read in again:

service apache2 reload

The new VirtualHost is now active and is used as soon as someone surfs on your server by entering “example.de”. The index.html / index.php in /var/www/beispiel.de/ is automatically loaded and displayed. If you were to put a second file "test.html" in the directory, this would be accessible via the page "example.de/test.html".

Incidentally, a VirtualHost can be set up using the command

a2dissite example.de.conf

deactivate again. In this case, too, it is necessary to reload the web server using a "service apache reload".

The web server is still relatively boring ...

... but that can be changed. The following articles continue: